-
Notifications
You must be signed in to change notification settings - Fork 121
/
ContactsDatabaseHelper.java
4403 lines (3870 loc) · 192 KB
/
ContactsDatabaseHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.providers.contacts;
import com.android.common.content.SyncStateContentProviderHelper;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.CharArrayBuffer;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDoneException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteStatement;
import android.location.CountryDetector;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.BaseColumns;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract;
import android.provider.ContactsContract.AggregationExceptions;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.SipAddress;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Contacts.Photo;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.Directory;
import android.provider.ContactsContract.DisplayNameSources;
import android.provider.ContactsContract.FullNameStyle;
import android.provider.ContactsContract.Groups;
import android.provider.ContactsContract.PhoneticNameStyle;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.Settings;
import android.provider.ContactsContract.StatusUpdates;
import android.provider.ContactsContract.StreamItemPhotos;
import android.provider.ContactsContract.StreamItems;
import android.provider.SocialContract.Activities;
import android.provider.VoicemailContract;
import android.provider.VoicemailContract.Voicemails;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.text.util.Rfc822Token;
import android.text.util.Rfc822Tokenizer;
import android.util.Log;
import java.util.HashMap;
import java.util.Locale;
/**
* Database helper for contacts. Designed as a singleton to make sure that all
* {@link android.content.ContentProvider} users get the same reference.
* Provides handy methods for maintaining package and mime-type lookup tables.
*/
/* package */ class ContactsDatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "ContactsDatabaseHelper";
/**
* Contacts DB version ranges:
* <pre>
* 0-98 Cupcake/Donut
* 100-199 Eclair
* 200-299 Eclair-MR1
* 300-349 Froyo
* 350-399 Gingerbread
* 400-499 Honeycomb
* 500-549 Honeycomb-MR1
* 550-599 Honeycomb-MR2
* 600-699 Ice Cream Sandwich
* </pre>
*/
static final int DATABASE_VERSION = 607;
private static final String DATABASE_NAME = "contacts2.db";
private static final String DATABASE_PRESENCE = "presence_db";
public interface Tables {
public static final String CONTACTS = "contacts";
public static final String RAW_CONTACTS = "raw_contacts";
public static final String STREAM_ITEMS = "stream_items";
public static final String STREAM_ITEM_PHOTOS = "stream_item_photos";
public static final String PACKAGES = "packages";
public static final String MIMETYPES = "mimetypes";
public static final String PHONE_LOOKUP = "phone_lookup";
public static final String NAME_LOOKUP = "name_lookup";
public static final String AGGREGATION_EXCEPTIONS = "agg_exceptions";
public static final String SETTINGS = "settings";
public static final String DATA = "data";
public static final String GROUPS = "groups";
public static final String PRESENCE = "presence";
public static final String AGGREGATED_PRESENCE = "agg_presence";
public static final String NICKNAME_LOOKUP = "nickname_lookup";
public static final String CALLS = "calls";
public static final String STATUS_UPDATES = "status_updates";
public static final String PROPERTIES = "properties";
public static final String ACCOUNTS = "accounts";
public static final String VISIBLE_CONTACTS = "visible_contacts";
public static final String DIRECTORIES = "directories";
public static final String DEFAULT_DIRECTORY = "default_directory";
public static final String SEARCH_INDEX = "search_index";
public static final String VOICEMAIL_STATUS = "voicemail_status";
/**
* For {@link ContactsContract.DataUsageFeedback}. The table structure itself
* is not exposed outside.
*/
public static final String DATA_USAGE_STAT = "data_usage_stat";
public static final String DATA_JOIN_MIMETYPES = "data "
+ "JOIN mimetypes ON (data.mimetype_id = mimetypes._id)";
public static final String DATA_JOIN_RAW_CONTACTS = "data "
+ "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)";
public static final String DATA_JOIN_MIMETYPE_RAW_CONTACTS = "data "
+ "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
+ "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id)";
// NOTE: This requires late binding of GroupMembership MIME-type
public static final String RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS = "raw_contacts "
+ "LEFT OUTER JOIN settings ON ("
+ "raw_contacts.account_name = settings.account_name AND "
+ "raw_contacts.account_type = settings.account_type) "
+ "LEFT OUTER JOIN data ON (data.mimetype_id=? AND "
+ "data.raw_contact_id = raw_contacts._id) "
+ "LEFT OUTER JOIN groups ON (groups._id = data." + GroupMembership.GROUP_ROW_ID
+ ")";
// NOTE: This requires late binding of GroupMembership MIME-type
public static final String SETTINGS_JOIN_RAW_CONTACTS_DATA_MIMETYPES_CONTACTS = "settings "
+ "LEFT OUTER JOIN raw_contacts ON ("
+ "raw_contacts.account_name = settings.account_name AND "
+ "raw_contacts.account_type = settings.account_type) "
+ "LEFT OUTER JOIN data ON (data.mimetype_id=? AND "
+ "data.raw_contact_id = raw_contacts._id) "
+ "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
public static final String DATA_JOIN_MIMETYPES_RAW_CONTACTS_CONTACTS = "data "
+ "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
+ "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
+ "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
public static final String DATA_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_GROUPS = "data "
+ "JOIN mimetypes ON (data.mimetype_id = mimetypes._id) "
+ "JOIN raw_contacts ON (data.raw_contact_id = raw_contacts._id) "
+ "LEFT OUTER JOIN packages ON (data.package_id = packages._id) "
+ "LEFT OUTER JOIN groups "
+ " ON (mimetypes.mimetype='" + GroupMembership.CONTENT_ITEM_TYPE + "' "
+ " AND groups._id = data." + GroupMembership.GROUP_ROW_ID + ") ";
public static final String GROUPS_JOIN_PACKAGES = "groups "
+ "LEFT OUTER JOIN packages ON (groups.package_id = packages._id)";
public static final String ACTIVITIES = "activities";
public static final String ACTIVITIES_JOIN_MIMETYPES = "activities "
+ "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id)";
public static final String ACTIVITIES_JOIN_PACKAGES_MIMETYPES_RAW_CONTACTS_CONTACTS =
"activities "
+ "LEFT OUTER JOIN packages ON (activities.package_id = packages._id) "
+ "LEFT OUTER JOIN mimetypes ON (activities.mimetype_id = mimetypes._id) "
+ "LEFT OUTER JOIN raw_contacts ON (activities.author_contact_id = " +
"raw_contacts._id) "
+ "LEFT OUTER JOIN contacts ON (raw_contacts.contact_id = contacts._id)";
public static final String NAME_LOOKUP_JOIN_RAW_CONTACTS = "name_lookup "
+ "INNER JOIN raw_contacts ON (name_lookup.raw_contact_id = raw_contacts._id)";
}
public interface Views {
public static final String DATA = "view_data";
public static final String RAW_CONTACTS = "view_raw_contacts";
public static final String CONTACTS = "view_contacts";
public static final String ENTITIES = "view_entities";
public static final String RAW_ENTITIES = "view_raw_entities";
public static final String GROUPS = "view_groups";
public static final String DATA_USAGE_STAT = "view_data_usage_stat";
}
public interface Clauses {
final String MIMETYPE_IS_GROUP_MEMBERSHIP = MimetypesColumns.CONCRETE_MIMETYPE + "='"
+ GroupMembership.CONTENT_ITEM_TYPE + "'";
final String BELONGS_TO_GROUP = DataColumns.CONCRETE_GROUP_ID + "="
+ GroupsColumns.CONCRETE_ID;
final String HAVING_NO_GROUPS = "COUNT(" + DataColumns.CONCRETE_GROUP_ID + ") == 0";
final String GROUP_BY_ACCOUNT_CONTACT_ID = SettingsColumns.CONCRETE_ACCOUNT_NAME + ","
+ SettingsColumns.CONCRETE_ACCOUNT_TYPE + "," + RawContacts.CONTACT_ID;
final String RAW_CONTACT_IS_LOCAL = RawContactsColumns.CONCRETE_ACCOUNT_NAME
+ " IS NULL AND " + RawContactsColumns.CONCRETE_ACCOUNT_TYPE + " IS NULL";
final String ZERO_GROUP_MEMBERSHIPS = "COUNT(" + GroupsColumns.CONCRETE_ID + ")=0";
final String OUTER_RAW_CONTACTS = "outer_raw_contacts";
final String OUTER_RAW_CONTACTS_ID = OUTER_RAW_CONTACTS + "." + RawContacts._ID;
final String CONTACT_IS_VISIBLE =
"SELECT " +
"MAX((SELECT (CASE WHEN " +
"(CASE" +
" WHEN " + RAW_CONTACT_IS_LOCAL +
" THEN 1 " +
" WHEN " + ZERO_GROUP_MEMBERSHIPS +
" THEN " + Settings.UNGROUPED_VISIBLE +
" ELSE MAX(" + Groups.GROUP_VISIBLE + ")" +
"END)=1 THEN 1 ELSE 0 END)" +
" FROM " + Tables.RAW_CONTACTS_JOIN_SETTINGS_DATA_GROUPS +
" WHERE " + RawContactsColumns.CONCRETE_ID + "=" + OUTER_RAW_CONTACTS_ID + "))" +
" FROM " + Tables.RAW_CONTACTS + " AS " + OUTER_RAW_CONTACTS +
" WHERE " + RawContacts.CONTACT_ID + "=" + ContactsColumns.CONCRETE_ID +
" GROUP BY " + RawContacts.CONTACT_ID;
final String GROUP_HAS_ACCOUNT_AND_SOURCE_ID = Groups.SOURCE_ID + "=? AND "
+ Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?";
public static final String CONTACT_VISIBLE =
"EXISTS (SELECT _id FROM " + Tables.VISIBLE_CONTACTS
+ " WHERE " + Tables.CONTACTS +"." + Contacts._ID
+ "=" + Tables.VISIBLE_CONTACTS +"." + Contacts._ID + ")";
}
public interface ContactsColumns {
public static final String LAST_STATUS_UPDATE_ID = "status_update_id";
public static final String CONCRETE_ID = Tables.CONTACTS + "." + BaseColumns._ID;
public static final String CONCRETE_TIMES_CONTACTED = Tables.CONTACTS + "."
+ Contacts.TIMES_CONTACTED;
public static final String CONCRETE_LAST_TIME_CONTACTED = Tables.CONTACTS + "."
+ Contacts.LAST_TIME_CONTACTED;
public static final String CONCRETE_STARRED = Tables.CONTACTS + "." + Contacts.STARRED;
public static final String CONCRETE_CUSTOM_RINGTONE = Tables.CONTACTS + "."
+ Contacts.CUSTOM_RINGTONE;
public static final String CONCRETE_SEND_TO_VOICEMAIL = Tables.CONTACTS + "."
+ Contacts.SEND_TO_VOICEMAIL;
public static final String CONCRETE_LOOKUP_KEY = Tables.CONTACTS + "."
+ Contacts.LOOKUP_KEY;
}
public interface RawContactsColumns {
public static final String CONCRETE_ID =
Tables.RAW_CONTACTS + "." + BaseColumns._ID;
public static final String CONCRETE_ACCOUNT_NAME =
Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_NAME;
public static final String CONCRETE_ACCOUNT_TYPE =
Tables.RAW_CONTACTS + "." + RawContacts.ACCOUNT_TYPE;
public static final String CONCRETE_SOURCE_ID =
Tables.RAW_CONTACTS + "." + RawContacts.SOURCE_ID;
public static final String CONCRETE_VERSION =
Tables.RAW_CONTACTS + "." + RawContacts.VERSION;
public static final String CONCRETE_DIRTY =
Tables.RAW_CONTACTS + "." + RawContacts.DIRTY;
public static final String CONCRETE_DELETED =
Tables.RAW_CONTACTS + "." + RawContacts.DELETED;
public static final String CONCRETE_SYNC1 =
Tables.RAW_CONTACTS + "." + RawContacts.SYNC1;
public static final String CONCRETE_SYNC2 =
Tables.RAW_CONTACTS + "." + RawContacts.SYNC2;
public static final String CONCRETE_SYNC3 =
Tables.RAW_CONTACTS + "." + RawContacts.SYNC3;
public static final String CONCRETE_SYNC4 =
Tables.RAW_CONTACTS + "." + RawContacts.SYNC4;
public static final String CONCRETE_CUSTOM_RINGTONE =
Tables.RAW_CONTACTS + "." + RawContacts.CUSTOM_RINGTONE;
public static final String CONCRETE_SEND_TO_VOICEMAIL =
Tables.RAW_CONTACTS + "." + RawContacts.SEND_TO_VOICEMAIL;
public static final String CONCRETE_LAST_TIME_CONTACTED =
Tables.RAW_CONTACTS + "." + RawContacts.LAST_TIME_CONTACTED;
public static final String CONCRETE_TIMES_CONTACTED =
Tables.RAW_CONTACTS + "." + RawContacts.TIMES_CONTACTED;
public static final String CONCRETE_STARRED =
Tables.RAW_CONTACTS + "." + RawContacts.STARRED;
public static final String DISPLAY_NAME = RawContacts.DISPLAY_NAME_PRIMARY;
public static final String DISPLAY_NAME_SOURCE = RawContacts.DISPLAY_NAME_SOURCE;
public static final String AGGREGATION_NEEDED = "aggregation_needed";
public static final String CONCRETE_DISPLAY_NAME =
Tables.RAW_CONTACTS + "." + DISPLAY_NAME;
public static final String CONCRETE_CONTACT_ID =
Tables.RAW_CONTACTS + "." + RawContacts.CONTACT_ID;
public static final String CONCRETE_NAME_VERIFIED =
Tables.RAW_CONTACTS + "." + RawContacts.NAME_VERIFIED;
}
public interface DataColumns {
public static final String PACKAGE_ID = "package_id";
public static final String MIMETYPE_ID = "mimetype_id";
public static final String CONCRETE_ID = Tables.DATA + "." + BaseColumns._ID;
public static final String CONCRETE_MIMETYPE_ID = Tables.DATA + "." + MIMETYPE_ID;
public static final String CONCRETE_RAW_CONTACT_ID = Tables.DATA + "."
+ Data.RAW_CONTACT_ID;
public static final String CONCRETE_GROUP_ID = Tables.DATA + "."
+ GroupMembership.GROUP_ROW_ID;
public static final String CONCRETE_DATA1 = Tables.DATA + "." + Data.DATA1;
public static final String CONCRETE_DATA2 = Tables.DATA + "." + Data.DATA2;
public static final String CONCRETE_DATA3 = Tables.DATA + "." + Data.DATA3;
public static final String CONCRETE_DATA4 = Tables.DATA + "." + Data.DATA4;
public static final String CONCRETE_DATA5 = Tables.DATA + "." + Data.DATA5;
public static final String CONCRETE_DATA6 = Tables.DATA + "." + Data.DATA6;
public static final String CONCRETE_DATA7 = Tables.DATA + "." + Data.DATA7;
public static final String CONCRETE_DATA8 = Tables.DATA + "." + Data.DATA8;
public static final String CONCRETE_DATA9 = Tables.DATA + "." + Data.DATA9;
public static final String CONCRETE_DATA10 = Tables.DATA + "." + Data.DATA10;
public static final String CONCRETE_DATA11 = Tables.DATA + "." + Data.DATA11;
public static final String CONCRETE_DATA12 = Tables.DATA + "." + Data.DATA12;
public static final String CONCRETE_DATA13 = Tables.DATA + "." + Data.DATA13;
public static final String CONCRETE_DATA14 = Tables.DATA + "." + Data.DATA14;
public static final String CONCRETE_DATA15 = Tables.DATA + "." + Data.DATA15;
public static final String CONCRETE_IS_PRIMARY = Tables.DATA + "." + Data.IS_PRIMARY;
public static final String CONCRETE_PACKAGE_ID = Tables.DATA + "." + PACKAGE_ID;
}
// Used only for legacy API support
public interface ExtensionsColumns {
public static final String NAME = Data.DATA1;
public static final String VALUE = Data.DATA2;
}
public interface GroupMembershipColumns {
public static final String RAW_CONTACT_ID = Data.RAW_CONTACT_ID;
public static final String GROUP_ROW_ID = GroupMembership.GROUP_ROW_ID;
}
public interface PhoneColumns {
public static final String NORMALIZED_NUMBER = Data.DATA4;
public static final String CONCRETE_NORMALIZED_NUMBER = DataColumns.CONCRETE_DATA4;
}
public interface GroupsColumns {
public static final String PACKAGE_ID = "package_id";
public static final String CONCRETE_ID = Tables.GROUPS + "." + BaseColumns._ID;
public static final String CONCRETE_SOURCE_ID = Tables.GROUPS + "." + Groups.SOURCE_ID;
public static final String CONCRETE_ACCOUNT_NAME =
Tables.GROUPS + "." + Groups.ACCOUNT_NAME;
public static final String CONCRETE_ACCOUNT_TYPE =
Tables.GROUPS + "." + Groups.ACCOUNT_TYPE;
public static final String CONCRETE_ACTION = Tables.GROUPS + "." + Groups.ACTION;
public static final String CONCRETE_ACTION_URI = Tables.GROUPS + "." + Groups.ACTION_URI;
}
public interface ActivitiesColumns {
public static final String PACKAGE_ID = "package_id";
public static final String MIMETYPE_ID = "mimetype_id";
}
public interface PhoneLookupColumns {
public static final String _ID = BaseColumns._ID;
public static final String DATA_ID = "data_id";
public static final String RAW_CONTACT_ID = "raw_contact_id";
public static final String NORMALIZED_NUMBER = "normalized_number";
public static final String MIN_MATCH = "min_match";
}
public interface NameLookupColumns {
public static final String RAW_CONTACT_ID = "raw_contact_id";
public static final String DATA_ID = "data_id";
public static final String NORMALIZED_NAME = "normalized_name";
public static final String NAME_TYPE = "name_type";
}
public final static class NameLookupType {
public static final int NAME_EXACT = 0;
public static final int NAME_VARIANT = 1;
public static final int NAME_COLLATION_KEY = 2;
public static final int NICKNAME = 3;
public static final int EMAIL_BASED_NICKNAME = 4;
// This is the highest name lookup type code plus one
public static final int TYPE_COUNT = 5;
public static boolean isBasedOnStructuredName(int nameLookupType) {
return nameLookupType == NameLookupType.NAME_EXACT
|| nameLookupType == NameLookupType.NAME_VARIANT
|| nameLookupType == NameLookupType.NAME_COLLATION_KEY;
}
}
public interface PackagesColumns {
public static final String _ID = BaseColumns._ID;
public static final String PACKAGE = "package";
public static final String CONCRETE_ID = Tables.PACKAGES + "." + _ID;
}
public interface MimetypesColumns {
public static final String _ID = BaseColumns._ID;
public static final String MIMETYPE = "mimetype";
public static final String CONCRETE_ID = Tables.MIMETYPES + "." + BaseColumns._ID;
public static final String CONCRETE_MIMETYPE = Tables.MIMETYPES + "." + MIMETYPE;
}
public interface AggregationExceptionColumns {
public static final String _ID = BaseColumns._ID;
}
public interface NicknameLookupColumns {
public static final String NAME = "name";
public static final String CLUSTER = "cluster";
}
public interface SettingsColumns {
public static final String CONCRETE_ACCOUNT_NAME = Tables.SETTINGS + "."
+ Settings.ACCOUNT_NAME;
public static final String CONCRETE_ACCOUNT_TYPE = Tables.SETTINGS + "."
+ Settings.ACCOUNT_TYPE;
}
public interface PresenceColumns {
String RAW_CONTACT_ID = "presence_raw_contact_id";
String CONTACT_ID = "presence_contact_id";
}
public interface AggregatedPresenceColumns {
String CONTACT_ID = "presence_contact_id";
String CONCRETE_CONTACT_ID = Tables.AGGREGATED_PRESENCE + "." + CONTACT_ID;
}
public interface StatusUpdatesColumns {
String DATA_ID = "status_update_data_id";
String CONCRETE_DATA_ID = Tables.STATUS_UPDATES + "." + DATA_ID;
String CONCRETE_PRESENCE = Tables.STATUS_UPDATES + "." + StatusUpdates.PRESENCE;
String CONCRETE_STATUS = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS;
String CONCRETE_STATUS_TIMESTAMP = Tables.STATUS_UPDATES + "."
+ StatusUpdates.STATUS_TIMESTAMP;
String CONCRETE_STATUS_RES_PACKAGE = Tables.STATUS_UPDATES + "."
+ StatusUpdates.STATUS_RES_PACKAGE;
String CONCRETE_STATUS_LABEL = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS_LABEL;
String CONCRETE_STATUS_ICON = Tables.STATUS_UPDATES + "." + StatusUpdates.STATUS_ICON;
}
public interface ContactsStatusUpdatesColumns {
String ALIAS = "contacts_" + Tables.STATUS_UPDATES;
String CONCRETE_DATA_ID = ALIAS + "." + StatusUpdatesColumns.DATA_ID;
String CONCRETE_PRESENCE = ALIAS + "." + StatusUpdates.PRESENCE;
String CONCRETE_STATUS = ALIAS + "." + StatusUpdates.STATUS;
String CONCRETE_STATUS_TIMESTAMP = ALIAS + "." + StatusUpdates.STATUS_TIMESTAMP;
String CONCRETE_STATUS_RES_PACKAGE = ALIAS + "." + StatusUpdates.STATUS_RES_PACKAGE;
String CONCRETE_STATUS_LABEL = ALIAS + "." + StatusUpdates.STATUS_LABEL;
String CONCRETE_STATUS_ICON = ALIAS + "." + StatusUpdates.STATUS_ICON;
}
public interface StreamItemsColumns {
String CONCRETE_ID = Tables.STREAM_ITEMS + "." + BaseColumns._ID;
String CONCRETE_RAW_CONTACT_ID = Tables.STREAM_ITEMS + "." + StreamItems.RAW_CONTACT_ID;
String CONCRETE_PACKAGE = Tables.STREAM_ITEMS + "." + StreamItems.RES_PACKAGE;
String CONCRETE_ICON = Tables.STREAM_ITEMS + "." + StreamItems.RES_ICON;
String CONCRETE_LABEL = Tables.STREAM_ITEMS + "." + StreamItems.RES_LABEL;
String CONCRETE_TEXT = Tables.STREAM_ITEMS + "." + StreamItems.TEXT;
String CONCRETE_TIMESTAMP = Tables.STREAM_ITEMS + "." + StreamItems.TIMESTAMP;
String CONCRETE_COMMENTS = Tables.STREAM_ITEMS + "." + StreamItems.COMMENTS;
String CONCRETE_ACTION = Tables.STREAM_ITEMS + "." + StreamItems.ACTION;
String CONCRETE_ACTION_URI = Tables.STREAM_ITEMS + "." + StreamItems.ACTION_URI;
}
public interface StreamItemPhotosColumns {
String CONCRETE_ID = Tables.STREAM_ITEM_PHOTOS + "." + BaseColumns._ID;
String CONCRETE_STREAM_ITEM_ID = Tables.STREAM_ITEM_PHOTOS + "."
+ StreamItemPhotos.STREAM_ITEM_ID;
String CONCRETE_SORT_INDEX = Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.SORT_INDEX;
String CONCRETE_PICTURE = Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.PICTURE;
String CONCRETE_ACTION = Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.ACTION;
String CONCRETE_ACTION_URI = Tables.STREAM_ITEM_PHOTOS + "." + StreamItemPhotos.ACTION_URI;
}
public interface PropertiesColumns {
String PROPERTY_KEY = "property_key";
String PROPERTY_VALUE = "property_value";
}
public interface AccountsColumns {
String ACCOUNT_NAME = RawContacts.ACCOUNT_NAME;
String ACCOUNT_TYPE = RawContacts.ACCOUNT_TYPE;
String PROFILE_RAW_CONTACT_ID = "profile_raw_contact_id";
}
public static final class DirectoryColumns {
public static final String TYPE_RESOURCE_NAME = "typeResourceName";
}
public static final class SearchIndexColumns {
public static final String CONTACT_ID = "contact_id";
public static final String CONTENT = "content";
public static final String NAME = "name";
public static final String TOKENS = "tokens";
}
/**
* Private table for calculating per-contact-method ranking.
*/
public static final class DataUsageStatColumns {
/** type: INTEGER (long) */
public static final String _ID = "stat_id";
public static final String CONCRETE_ID = Tables.DATA_USAGE_STAT + "." + _ID;
/** type: INTEGER (long) */
public static final String DATA_ID = "data_id";
public static final String CONCRETE_DATA_ID = Tables.DATA_USAGE_STAT + "." + DATA_ID;
/** type: INTEGER (long) */
public static final String LAST_TIME_USED = "last_time_used";
public static final String CONCRETE_LAST_TIME_USED =
Tables.DATA_USAGE_STAT + "." + LAST_TIME_USED;
/** type: INTEGER */
public static final String TIMES_USED = "times_used";
public static final String CONCRETE_TIMES_USED =
Tables.DATA_USAGE_STAT + "." + TIMES_USED;
/** type: INTEGER */
public static final String USAGE_TYPE_INT = "usage_type";
public static final String CONCRETE_USAGE_TYPE =
Tables.DATA_USAGE_STAT + "." + USAGE_TYPE_INT;
/**
* Integer values for USAGE_TYPE.
*
* @see ContactsContract.DataUsageFeedback#USAGE_TYPE
*/
public static final int USAGE_TYPE_INT_CALL = 0;
public static final int USAGE_TYPE_INT_LONG_TEXT = 1;
public static final int USAGE_TYPE_INT_SHORT_TEXT = 2;
}
/** In-memory cache of previously found MIME-type mappings */
private final HashMap<String, Long> mMimetypeCache = new HashMap<String, Long>();
/** In-memory cache of previously found package name mappings */
private final HashMap<String, Long> mPackageCache = new HashMap<String, Long>();
private long mMimeTypeIdEmail;
private long mMimeTypeIdIm;
private long mMimeTypeIdSip;
private long mMimeTypeIdStructuredName;
private long mMimeTypeIdOrganization;
private long mMimeTypeIdNickname;
private long mMimeTypeIdPhone;
/** Compiled statements for querying and inserting mappings */
private SQLiteStatement mMimetypeQuery;
private SQLiteStatement mPackageQuery;
private SQLiteStatement mContactIdQuery;
private SQLiteStatement mAggregationModeQuery;
private SQLiteStatement mMimetypeInsert;
private SQLiteStatement mPackageInsert;
private SQLiteStatement mDataMimetypeQuery;
private SQLiteStatement mActivitiesMimetypeQuery;
/** Precompiled sql statement for setting a data record to the primary. */
private SQLiteStatement mSetPrimaryStatement;
/** Precompiled sql statement for setting a data record to the super primary. */
private SQLiteStatement mSetSuperPrimaryStatement;
/** Precompiled sql statement for clearing super primary of a single record. */
private SQLiteStatement mClearSuperPrimaryStatement;
/** Precompiled sql statement for updating a contact display name */
private SQLiteStatement mRawContactDisplayNameUpdate;
private SQLiteStatement mNameLookupInsert;
private SQLiteStatement mNameLookupDelete;
private SQLiteStatement mStatusUpdateAutoTimestamp;
private SQLiteStatement mStatusUpdateInsert;
private SQLiteStatement mStatusUpdateReplace;
private SQLiteStatement mStatusAttributionUpdate;
private SQLiteStatement mStatusUpdateDelete;
private SQLiteStatement mResetNameVerifiedForOtherRawContacts;
private SQLiteStatement mContactInDefaultDirectoryQuery;
private final Context mContext;
private final boolean mDatabaseOptimizationEnabled;
private final SyncStateContentProviderHelper mSyncState;
private final CountryMonitor mCountryMonitor;
private StringBuilder mSb = new StringBuilder();
private boolean mReopenDatabase = false;
private static ContactsDatabaseHelper sSingleton = null;
private boolean mUseStrictPhoneNumberComparison;
private String[] mSelectionArgs1 = new String[1];
private NameSplitter.Name mName = new NameSplitter.Name();
private CharArrayBuffer mCharArrayBuffer = new CharArrayBuffer(128);
private NameSplitter mNameSplitter;
public static synchronized ContactsDatabaseHelper getInstance(Context context) {
if (sSingleton == null) {
sSingleton = new ContactsDatabaseHelper(context, DATABASE_NAME, true);
}
return sSingleton;
}
/**
* Private constructor, callers except unit tests should obtain an instance through
* {@link #getInstance(android.content.Context)} instead.
*/
ContactsDatabaseHelper(Context context) {
this(context, null, false);
}
private ContactsDatabaseHelper(
Context context, String databaseName, boolean optimizationEnabled) {
super(context, databaseName, null, DATABASE_VERSION);
mDatabaseOptimizationEnabled = optimizationEnabled;
Resources resources = context.getResources();
mContext = context;
mSyncState = new SyncStateContentProviderHelper();
mCountryMonitor = new CountryMonitor(context);
mUseStrictPhoneNumberComparison =
resources.getBoolean(
com.android.internal.R.bool.config_use_strict_phone_number_comparation);
}
private void refreshDatabaseCaches(SQLiteDatabase db) {
mStatusUpdateDelete = null;
mStatusUpdateReplace = null;
mStatusUpdateInsert = null;
mStatusUpdateAutoTimestamp = null;
mStatusAttributionUpdate = null;
mResetNameVerifiedForOtherRawContacts = null;
mRawContactDisplayNameUpdate = null;
mSetPrimaryStatement = null;
mClearSuperPrimaryStatement = null;
mSetSuperPrimaryStatement = null;
mNameLookupInsert = null;
mNameLookupDelete = null;
mPackageQuery = null;
mPackageInsert = null;
mDataMimetypeQuery = null;
mActivitiesMimetypeQuery = null;
mContactIdQuery = null;
mAggregationModeQuery = null;
mContactInDefaultDirectoryQuery = null;
populateMimeTypeCache(db);
}
private void populateMimeTypeCache(SQLiteDatabase db) {
mMimetypeCache.clear();
mPackageCache.clear();
mMimetypeQuery = db.compileStatement(
"SELECT " + MimetypesColumns._ID +
" FROM " + Tables.MIMETYPES +
" WHERE " + MimetypesColumns.MIMETYPE + "=?");
mMimetypeInsert = db.compileStatement(
"INSERT INTO " + Tables.MIMETYPES + "("
+ MimetypesColumns.MIMETYPE +
") VALUES (?)");
mMimeTypeIdEmail = getMimeTypeId(Email.CONTENT_ITEM_TYPE);
mMimeTypeIdIm = getMimeTypeId(Im.CONTENT_ITEM_TYPE);
mMimeTypeIdSip = getMimeTypeId(SipAddress.CONTENT_ITEM_TYPE);
mMimeTypeIdStructuredName = getMimeTypeId(StructuredName.CONTENT_ITEM_TYPE);
mMimeTypeIdOrganization = getMimeTypeId(Organization.CONTENT_ITEM_TYPE);
mMimeTypeIdNickname = getMimeTypeId(Nickname.CONTENT_ITEM_TYPE);
mMimeTypeIdPhone = getMimeTypeId(Phone.CONTENT_ITEM_TYPE);
}
@Override
public void onOpen(SQLiteDatabase db) {
refreshDatabaseCaches(db);
mSyncState.onDatabaseOpened(db);
db.execSQL("ATTACH DATABASE ':memory:' AS " + DATABASE_PRESENCE + ";");
db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_PRESENCE + "." + Tables.PRESENCE + " ("+
StatusUpdates.DATA_ID + " INTEGER PRIMARY KEY REFERENCES data(_id)," +
StatusUpdates.PROTOCOL + " INTEGER NOT NULL," +
StatusUpdates.CUSTOM_PROTOCOL + " TEXT," +
StatusUpdates.IM_HANDLE + " TEXT," +
StatusUpdates.IM_ACCOUNT + " TEXT," +
PresenceColumns.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," +
PresenceColumns.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
StatusUpdates.PRESENCE + " INTEGER," +
StatusUpdates.CHAT_CAPABILITY + " INTEGER NOT NULL DEFAULT 0," +
"UNIQUE(" + StatusUpdates.PROTOCOL + ", " + StatusUpdates.CUSTOM_PROTOCOL
+ ", " + StatusUpdates.IM_HANDLE + ", " + StatusUpdates.IM_ACCOUNT + ")" +
");");
db.execSQL("CREATE INDEX IF NOT EXISTS " + DATABASE_PRESENCE + ".presenceIndex" + " ON "
+ Tables.PRESENCE + " (" + PresenceColumns.RAW_CONTACT_ID + ");");
db.execSQL("CREATE INDEX IF NOT EXISTS " + DATABASE_PRESENCE + ".presenceIndex2" + " ON "
+ Tables.PRESENCE + " (" + PresenceColumns.CONTACT_ID + ");");
db.execSQL("CREATE TABLE IF NOT EXISTS "
+ DATABASE_PRESENCE + "." + Tables.AGGREGATED_PRESENCE + " ("+
AggregatedPresenceColumns.CONTACT_ID
+ " INTEGER PRIMARY KEY REFERENCES contacts(_id)," +
StatusUpdates.PRESENCE + " INTEGER," +
StatusUpdates.CHAT_CAPABILITY + " INTEGER NOT NULL DEFAULT 0" +
");");
db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_deleted"
+ " BEFORE DELETE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
+ " BEGIN "
+ " DELETE FROM " + Tables.AGGREGATED_PRESENCE
+ " WHERE " + AggregatedPresenceColumns.CONTACT_ID + " = " +
"(SELECT " + PresenceColumns.CONTACT_ID +
" FROM " + Tables.PRESENCE +
" WHERE " + PresenceColumns.RAW_CONTACT_ID
+ "=OLD." + PresenceColumns.RAW_CONTACT_ID +
" AND NOT EXISTS" +
"(SELECT " + PresenceColumns.RAW_CONTACT_ID +
" FROM " + Tables.PRESENCE +
" WHERE " + PresenceColumns.CONTACT_ID
+ "=OLD." + PresenceColumns.CONTACT_ID +
" AND " + PresenceColumns.RAW_CONTACT_ID
+ "!=OLD." + PresenceColumns.RAW_CONTACT_ID + "));"
+ " END");
final String replaceAggregatePresenceSql =
"INSERT OR REPLACE INTO " + Tables.AGGREGATED_PRESENCE + "("
+ AggregatedPresenceColumns.CONTACT_ID + ", "
+ StatusUpdates.PRESENCE + ", "
+ StatusUpdates.CHAT_CAPABILITY + ")"
+ " SELECT "
+ PresenceColumns.CONTACT_ID + ","
+ StatusUpdates.PRESENCE + ","
+ StatusUpdates.CHAT_CAPABILITY
+ " FROM " + Tables.PRESENCE
+ " WHERE "
+ " (ifnull(" + StatusUpdates.PRESENCE + ",0) * 10 "
+ "+ ifnull(" + StatusUpdates.CHAT_CAPABILITY + ", 0))"
+ " = (SELECT "
+ "MAX (ifnull(" + StatusUpdates.PRESENCE + ",0) * 10 "
+ "+ ifnull(" + StatusUpdates.CHAT_CAPABILITY + ", 0))"
+ " FROM " + Tables.PRESENCE
+ " WHERE " + PresenceColumns.CONTACT_ID
+ "=NEW." + PresenceColumns.CONTACT_ID
+ ")"
+ " AND " + PresenceColumns.CONTACT_ID + "=NEW." + PresenceColumns.CONTACT_ID + ";";
db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_inserted"
+ " AFTER INSERT ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
+ " BEGIN "
+ replaceAggregatePresenceSql
+ " END");
db.execSQL("CREATE TRIGGER " + DATABASE_PRESENCE + "." + Tables.PRESENCE + "_updated"
+ " AFTER UPDATE ON " + DATABASE_PRESENCE + "." + Tables.PRESENCE
+ " BEGIN "
+ replaceAggregatePresenceSql
+ " END");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "Bootstrapping database version: " + DATABASE_VERSION);
mSyncState.createDatabase(db);
// One row per group of contacts corresponding to the same person
db.execSQL("CREATE TABLE " + Tables.CONTACTS + " (" +
BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Contacts.NAME_RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id)," +
Contacts.PHOTO_ID + " INTEGER REFERENCES data(_id)," +
Contacts.CUSTOM_RINGTONE + " TEXT," +
Contacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," +
Contacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
Contacts.LAST_TIME_CONTACTED + " INTEGER," +
Contacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
Contacts.HAS_PHONE_NUMBER + " INTEGER NOT NULL DEFAULT 0," +
Contacts.LOOKUP_KEY + " TEXT," +
ContactsColumns.LAST_STATUS_UPDATE_ID + " INTEGER REFERENCES data(_id)" +
");");
db.execSQL("CREATE INDEX contacts_has_phone_index ON " + Tables.CONTACTS + " (" +
Contacts.HAS_PHONE_NUMBER +
");");
db.execSQL("CREATE INDEX contacts_name_raw_contact_id_index ON " + Tables.CONTACTS + " (" +
Contacts.NAME_RAW_CONTACT_ID +
");");
// Contacts table
db.execSQL("CREATE TABLE " + Tables.RAW_CONTACTS + " (" +
RawContacts._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
RawContacts.ACCOUNT_NAME + " STRING DEFAULT NULL, " +
RawContacts.ACCOUNT_TYPE + " STRING DEFAULT NULL, " +
RawContacts.SOURCE_ID + " TEXT," +
RawContacts.RAW_CONTACT_IS_READ_ONLY + " INTEGER NOT NULL DEFAULT 0," +
RawContacts.VERSION + " INTEGER NOT NULL DEFAULT 1," +
RawContacts.DIRTY + " INTEGER NOT NULL DEFAULT 0," +
RawContacts.DELETED + " INTEGER NOT NULL DEFAULT 0," +
RawContacts.CONTACT_ID + " INTEGER REFERENCES contacts(_id)," +
RawContacts.AGGREGATION_MODE + " INTEGER NOT NULL DEFAULT " +
RawContacts.AGGREGATION_MODE_DEFAULT + "," +
RawContactsColumns.AGGREGATION_NEEDED + " INTEGER NOT NULL DEFAULT 1," +
RawContacts.CUSTOM_RINGTONE + " TEXT," +
RawContacts.SEND_TO_VOICEMAIL + " INTEGER NOT NULL DEFAULT 0," +
RawContacts.TIMES_CONTACTED + " INTEGER NOT NULL DEFAULT 0," +
RawContacts.LAST_TIME_CONTACTED + " INTEGER," +
RawContacts.STARRED + " INTEGER NOT NULL DEFAULT 0," +
RawContacts.DISPLAY_NAME_PRIMARY + " TEXT," +
RawContacts.DISPLAY_NAME_ALTERNATIVE + " TEXT," +
RawContacts.DISPLAY_NAME_SOURCE + " INTEGER NOT NULL DEFAULT " +
DisplayNameSources.UNDEFINED + "," +
RawContacts.PHONETIC_NAME + " TEXT," +
RawContacts.PHONETIC_NAME_STYLE + " TEXT," +
RawContacts.SORT_KEY_PRIMARY + " TEXT COLLATE " +
ContactsProvider2.PHONEBOOK_COLLATOR_NAME + "," +
RawContacts.SORT_KEY_ALTERNATIVE + " TEXT COLLATE " +
ContactsProvider2.PHONEBOOK_COLLATOR_NAME + "," +
RawContacts.NAME_VERIFIED + " INTEGER NOT NULL DEFAULT 0," +
RawContacts.SYNC1 + " TEXT, " +
RawContacts.SYNC2 + " TEXT, " +
RawContacts.SYNC3 + " TEXT, " +
RawContacts.SYNC4 + " TEXT " +
");");
db.execSQL("CREATE INDEX raw_contacts_contact_id_index ON " + Tables.RAW_CONTACTS + " (" +
RawContacts.CONTACT_ID +
");");
db.execSQL("CREATE INDEX raw_contacts_source_id_index ON " + Tables.RAW_CONTACTS + " (" +
RawContacts.SOURCE_ID + ", " +
RawContacts.ACCOUNT_TYPE + ", " +
RawContacts.ACCOUNT_NAME +
");");
db.execSQL("CREATE TABLE " + Tables.STREAM_ITEMS + " (" +
StreamItems._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
StreamItems.RAW_CONTACT_ID + " INTEGER NOT NULL, " +
StreamItems.RES_PACKAGE + " INTEGER NOT NULL, " +
StreamItems.RES_ICON + " INTEGER, " +
StreamItems.RES_LABEL + " INTEGER, " +
StreamItems.TEXT + " TEXT NOT NULL, " +
StreamItems.TIMESTAMP + " INTEGER NOT NULL, " +
StreamItems.COMMENTS + " TEXT NOT NULL, " +
StreamItems.ACTION + " TEXT, " +
StreamItems.ACTION_URI + " TEXT, " +
"FOREIGN KEY(" + StreamItems.RAW_CONTACT_ID + ") REFERENCES " +
Tables.RAW_CONTACTS + "(" + RawContacts._ID + "));");
db.execSQL("CREATE TABLE " + Tables.STREAM_ITEM_PHOTOS + " (" +
StreamItemPhotos._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
StreamItemPhotos.STREAM_ITEM_ID + " INTEGER NOT NULL, " +
StreamItemPhotos.SORT_INDEX + " INTEGER, " +
StreamItemPhotos.PICTURE + " BLOB, " +
StreamItemPhotos.ACTION + " TEXT, " +
StreamItemPhotos.ACTION_URI + " TEXT, " +
"FOREIGN KEY(" + StreamItemPhotos.STREAM_ITEM_ID + ") REFERENCES " +
Tables.STREAM_ITEMS + "(" + StreamItems._ID + "));");
// TODO readd the index and investigate a controlled use of it
// db.execSQL("CREATE INDEX raw_contacts_agg_index ON " + Tables.RAW_CONTACTS + " (" +
// RawContactsColumns.AGGREGATION_NEEDED +
// ");");
// Package name mapping table
db.execSQL("CREATE TABLE " + Tables.PACKAGES + " (" +
PackagesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
PackagesColumns.PACKAGE + " TEXT NOT NULL" +
");");
// Mimetype mapping table
db.execSQL("CREATE TABLE " + Tables.MIMETYPES + " (" +
MimetypesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
MimetypesColumns.MIMETYPE + " TEXT NOT NULL" +
");");
// Mimetype table requires an index on mime type
db.execSQL("CREATE UNIQUE INDEX mime_type ON " + Tables.MIMETYPES + " (" +
MimetypesColumns.MIMETYPE +
");");
// Public generic data table
db.execSQL("CREATE TABLE " + Tables.DATA + " (" +
Data._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
DataColumns.PACKAGE_ID + " INTEGER REFERENCES package(_id)," +
DataColumns.MIMETYPE_ID + " INTEGER REFERENCES mimetype(_id) NOT NULL," +
Data.RAW_CONTACT_ID + " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
Data.IS_READ_ONLY + " INTEGER NOT NULL DEFAULT 0," +
Data.IS_PRIMARY + " INTEGER NOT NULL DEFAULT 0," +
Data.IS_SUPER_PRIMARY + " INTEGER NOT NULL DEFAULT 0," +
Data.DATA_VERSION + " INTEGER NOT NULL DEFAULT 0," +
Data.DATA1 + " TEXT," +
Data.DATA2 + " TEXT," +
Data.DATA3 + " TEXT," +
Data.DATA4 + " TEXT," +
Data.DATA5 + " TEXT," +
Data.DATA6 + " TEXT," +
Data.DATA7 + " TEXT," +
Data.DATA8 + " TEXT," +
Data.DATA9 + " TEXT," +
Data.DATA10 + " TEXT," +
Data.DATA11 + " TEXT," +
Data.DATA12 + " TEXT," +
Data.DATA13 + " TEXT," +
Data.DATA14 + " TEXT," +
Data.DATA15 + " TEXT," +
Data.SYNC1 + " TEXT, " +
Data.SYNC2 + " TEXT, " +
Data.SYNC3 + " TEXT, " +
Data.SYNC4 + " TEXT " +
");");
db.execSQL("CREATE INDEX data_raw_contact_id ON " + Tables.DATA + " (" +
Data.RAW_CONTACT_ID +
");");
/**
* For email lookup and similar queries.
*/
db.execSQL("CREATE INDEX data_mimetype_data1_index ON " + Tables.DATA + " (" +
DataColumns.MIMETYPE_ID + "," +
Data.DATA1 +
");");
// Private phone numbers table used for lookup
db.execSQL("CREATE TABLE " + Tables.PHONE_LOOKUP + " (" +
PhoneLookupColumns.DATA_ID
+ " INTEGER REFERENCES data(_id) NOT NULL," +
PhoneLookupColumns.RAW_CONTACT_ID
+ " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
PhoneLookupColumns.NORMALIZED_NUMBER + " TEXT NOT NULL," +
PhoneLookupColumns.MIN_MATCH + " TEXT NOT NULL" +
");");
db.execSQL("CREATE INDEX phone_lookup_index ON " + Tables.PHONE_LOOKUP + " (" +
PhoneLookupColumns.NORMALIZED_NUMBER + "," +
PhoneLookupColumns.RAW_CONTACT_ID + "," +
PhoneLookupColumns.DATA_ID +
");");
db.execSQL("CREATE INDEX phone_lookup_min_match_index ON " + Tables.PHONE_LOOKUP + " (" +
PhoneLookupColumns.MIN_MATCH + "," +
PhoneLookupColumns.RAW_CONTACT_ID + "," +
PhoneLookupColumns.DATA_ID +
");");
db.execSQL("CREATE INDEX phone_lookup_data_id_min_match_index ON " + Tables.PHONE_LOOKUP +
" (" + PhoneLookupColumns.DATA_ID + ", " + PhoneLookupColumns.MIN_MATCH + ");");
// Private name/nickname table used for lookup
db.execSQL("CREATE TABLE " + Tables.NAME_LOOKUP + " (" +
NameLookupColumns.DATA_ID
+ " INTEGER REFERENCES data(_id) NOT NULL," +
NameLookupColumns.RAW_CONTACT_ID
+ " INTEGER REFERENCES raw_contacts(_id) NOT NULL," +
NameLookupColumns.NORMALIZED_NAME + " TEXT NOT NULL," +
NameLookupColumns.NAME_TYPE + " INTEGER NOT NULL," +
"PRIMARY KEY ("
+ NameLookupColumns.DATA_ID + ", "
+ NameLookupColumns.NORMALIZED_NAME + ", "
+ NameLookupColumns.NAME_TYPE + ")" +
");");
db.execSQL("CREATE INDEX name_lookup_raw_contact_id_index ON " + Tables.NAME_LOOKUP + " (" +
NameLookupColumns.RAW_CONTACT_ID +
");");
db.execSQL("CREATE TABLE " + Tables.NICKNAME_LOOKUP + " (" +