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
|
/*
* 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 android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.Extensions;
import android.provider.Contacts.GroupMembership;
import android.provider.Contacts.Groups;
import android.provider.Contacts.Organizations;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.provider.Contacts.Photos;
import android.provider.Contacts.Presence;
import android.provider.Contacts.Settings;
import android.provider.ContactsContract;
import android.test.suitebuilder.annotation.MediumTest;
import java.io.IOException;
/**
* Tests for legacy contacts APIs.
*
* Run the test like this:
* <code>
* adb shell am instrument -e class com.android.providers.contacts.LegacyContactsProviderTest -w \
* com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
* </code>
*
* Note that this SHOULD be a large test, but had to be bumped down to medium due to a bug in the
* SQLite cleanup code.
*/
@MediumTest
@SuppressWarnings("deprecation")
public class LegacyContactsProviderTest extends BaseContactsProvider2Test {
@Override
protected Class<? extends ContentProvider> getProviderClass() {
return SynchronousContactsProvider2.class;
}
@Override
protected String getAuthority() {
return Contacts.AUTHORITY;
}
public void testPeopleInsert() {
ContentValues values = new ContentValues();
putContactValues(values);
Uri uri = mResolver.insert(People.CONTENT_URI, values);
assertStoredValues(uri, values);
assertSelection(People.CONTENT_URI, values, "people", People._ID, ContentUris.parseId(uri));
}
public void testPeopleUpdate() {
ContentValues values = new ContentValues();
putContactValues(values);
Uri uri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(uri);
assertStoredValues(uri, values);
assertSelection(People.CONTENT_URI, values, "people", People._ID, personId);
values.clear();
putContactValues2(values);
mResolver.update(uri, values, null, null);
assertStoredValues(uri, values);
values.clear();
putContactValues(values);
mResolver.update(People.CONTENT_URI, values, People._ID + "=" + personId, null);
assertStoredValues(uri, values);
}
public void testPeopleDelete() {
ContentValues values = new ContentValues();
values.put(People.NAME, "John Doe");
Uri personId = mResolver.insert(People.CONTENT_URI, values);
mResolver.delete(personId, null, null);
Cursor c = mResolver.query(personId, null, People.NAME + "='John Doe'" , null, null);
assertEquals("Record count after deletion", 0, c.getCount());
c.close();
try {
mResolver.query(People.DELETED_CONTENT_URI, null, null, null, null);
} catch (UnsupportedOperationException e) {
// Expected exception
}
}
public void testPeopleFilter() {
ContentValues values = new ContentValues();
values.put(People.NAME, "Deer Doe");
mResolver.insert(People.CONTENT_URI, values);
values.clear();
values.put(People.NAME, "Dear Dough");
mResolver.insert(People.CONTENT_URI, values);
values.clear();
values.put(People.NAME, "D.R. Dauwe");
mResolver.insert(People.CONTENT_URI, values);
assertFilteredContacts("d", "Deer Doe", "Dear Dough", "D.R. Dauwe");
assertFilteredContacts("de", "Deer Doe", "Dear Dough");
assertFilteredContacts("dee", "Deer Doe");
assertFilteredContacts("der");
}
public void testDefaultDisplayName() {
ContentValues values = new ContentValues();
values.put(People.NAME, "John Doe");
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
assertStoredValue(personUri, People.DISPLAY_NAME, "John Doe");
}
public void testPrimaryOrganization() {
ContentValues values = new ContentValues();
final Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
// Primary
values.clear();
values.put(Organizations.ISPRIMARY, 1);
values.put(Organizations.COMPANY, "Google");
values.put(Organizations.TYPE, Organizations.TYPE_WORK);
values.put(Organizations.PERSON_ID, personId);
Uri orgUri1 = mResolver.insert(Organizations.CONTENT_URI, values);
// Non-primary
values.clear();
values.put(Organizations.COMPANY, "Acme");
values.put(Organizations.TYPE, Organizations.TYPE_WORK);
values.put(Organizations.PERSON_ID, personId);
Uri orgUri2 = mResolver.insert(Organizations.CONTENT_URI, values);
values.clear();
values.put(People.PRIMARY_ORGANIZATION_ID, ContentUris.parseId(orgUri1));
values.put(People.DISPLAY_NAME, "Google");
assertStoredValues(personUri, values);
// Remove the original primary organization
mResolver.delete(orgUri1, null, null);
values.clear();
values.put(People.PRIMARY_ORGANIZATION_ID, ContentUris.parseId(orgUri2));
values.put(People.DISPLAY_NAME, "Acme");
assertStoredValues(personUri, values);
// Remove the remaining organization
mResolver.delete(orgUri2, null, null);
values.clear();
values.putNull(People.PRIMARY_ORGANIZATION_ID);
values.putNull(People.DISPLAY_NAME);
assertStoredValues(personUri, values);
}
public void testPrimaryPhone() {
ContentValues values = new ContentValues();
putContactValuesExceptName(values);
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
// Primary
values.clear();
values.put(Phones.ISPRIMARY, 1);
values.put(Phones.TYPE, Phones.TYPE_WORK);
values.put(Phones.PERSON_ID, personId);
values.put(Phones.NUMBER, "12345");
Uri phoneUri1 = mResolver.insert(Phones.CONTENT_URI, values);
// Non-primary
values.clear();
values.put(Phones.TYPE, Phones.TYPE_WORK);
values.put(Phones.PERSON_ID, personId);
values.put(Phones.NUMBER, "67890");
Uri phoneUri2 = mResolver.insert(Phones.CONTENT_URI, values);
values.clear();
values.put(People.PRIMARY_PHONE_ID, ContentUris.parseId(phoneUri1));
values.put(People.DISPLAY_NAME, "12345");
assertStoredValues(personUri, values);
values.clear();
putContactValuesExceptName(values);
values.put(People.PRIMARY_PHONE_ID, ContentUris.parseId(phoneUri1));
assertStoredValues(phoneUri2, values);
// Remove the primary phone number
mResolver.delete(phoneUri1, null, null);
values.clear();
values.put(People.PRIMARY_PHONE_ID, ContentUris.parseId(phoneUri2));
values.put(People.DISPLAY_NAME, "67890");
assertStoredValues(personUri, values);
// Remove the remaining phone number
mResolver.delete(phoneUri2, null, null);
values.clear();
values.putNull(People.PRIMARY_PHONE_ID);
values.putNull(People.DISPLAY_NAME);
assertStoredValues(personUri, values);
}
public void testEmailCrud() {
ContentValues values = new ContentValues();
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
// Primary
values.clear();
values.put(ContactMethods.PERSON_ID, personId);
values.put(ContactMethods.KIND, Contacts.KIND_EMAIL);
values.put(ContactMethods.TYPE, ContactMethods.TYPE_HOME);
values.put(ContactMethods.DATA, "foo@acme.com");
values.put(ContactMethods.ISPRIMARY, 1);
Uri emailUri1 = mResolver.insert(ContactMethods.CONTENT_URI, values);
assertEquals(ContactMethods.CONTENT_EMAIL_ITEM_TYPE, mResolver.getType(emailUri1));
assertStoredValues(ContactMethods.CONTENT_URI,
ContactMethods._ID + "=" + ContentUris.parseId(emailUri1), null, values);
assertStoredValues(ContactMethods.CONTENT_EMAIL_URI,
ContactMethods._ID + "=" + ContentUris.parseId(emailUri1), null, values);
}
public void testPrimaryEmail() {
ContentValues values = new ContentValues();
putContactValuesExceptName(values);
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
// Primary
values.clear();
values.put(ContactMethods.PERSON_ID, personId);
values.put(ContactMethods.KIND, Contacts.KIND_EMAIL);
values.put(ContactMethods.TYPE, ContactMethods.TYPE_HOME);
values.put(ContactMethods.DATA, "foo@acme.com");
values.put(ContactMethods.ISPRIMARY, 1);
Uri emailUri1 = mResolver.insert(ContactMethods.CONTENT_URI, values);
// Non-primary
values.clear();
values.put(ContactMethods.PERSON_ID, personId);
values.put(ContactMethods.KIND, Contacts.KIND_EMAIL);
values.put(ContactMethods.TYPE, ContactMethods.TYPE_WORK);
values.put(ContactMethods.DATA, "bar@acme.com");
Uri emailUri2 = mResolver.insert(ContactMethods.CONTENT_URI, values);
values.clear();
values.put(People.PRIMARY_EMAIL_ID, ContentUris.parseId(emailUri1));
values.put(People.DISPLAY_NAME, "foo@acme.com");
assertStoredValues(personUri, values);
values.clear();
putContactValuesExceptName(values);
values.put(People.PRIMARY_EMAIL_ID, ContentUris.parseId(emailUri1));
assertStoredValues(emailUri2, values);
// Remove the primary email
mResolver.delete(emailUri1, null, null);
values.clear();
values.put(People.PRIMARY_EMAIL_ID, ContentUris.parseId(emailUri2));
values.put(People.DISPLAY_NAME, "bar@acme.com");
assertStoredValues(personUri, values);
// Remove the remaining email
mResolver.delete(emailUri2, null, null);
values.clear();
values.putNull(People.PRIMARY_EMAIL_ID);
values.putNull(People.DISPLAY_NAME);
assertStoredValues(personUri, values);
}
public void testMarkAsContacted() {
ContentValues values = new ContentValues();
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
int timesContactedBefore =
Integer.parseInt(getStoredValue(personUri, People.TIMES_CONTACTED));
long timeBefore = System.currentTimeMillis();
People.markAsContacted(mResolver, personId);
long timeAfter = System.currentTimeMillis();
long lastContacted = Long.parseLong(getStoredValue(personUri, People.LAST_TIME_CONTACTED));
int timesContactedAfter =
Integer.parseInt(getStoredValue(personUri, People.TIMES_CONTACTED));
assertTrue(lastContacted >= timeBefore);
assertTrue(lastContacted <= timeAfter);
assertEquals(timesContactedAfter, timesContactedBefore + 1);
}
public void testOrganizationsInsert() {
ContentValues values = new ContentValues();
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(Organizations.COMPANY, "Sierra");
values.put(Organizations.PERSON_ID, personId);
values.put(Organizations.TYPE, Organizations.TYPE_CUSTOM);
values.put(Organizations.LABEL, "Club");
values.put(Organizations.TITLE, "Member");
values.put(Organizations.ISPRIMARY, 1);
Uri uri = mResolver.insert(Organizations.CONTENT_URI, values);
assertStoredValues(uri, values);
assertSelection(Organizations.CONTENT_URI, values,
"organizations", Organizations._ID, ContentUris.parseId(uri));
assertPersonIdConstraint(Organizations.CONTENT_URI, Organizations.TYPE,
Organizations.TYPE_WORK);
assertTypeAndLabelConstraints(Organizations.CONTENT_URI, Organizations.PERSON_ID, personId,
Organizations.TYPE, Organizations.TYPE_CUSTOM, Organizations.TYPE_OTHER,
Organizations.LABEL);
Uri twigUri = Uri.withAppendedPath(personUri, Organizations.CONTENT_DIRECTORY);
assertStoredValues(twigUri, values);
Uri twigUriWithId = ContentUris.withAppendedId(twigUri, ContentUris.parseId(uri));
assertStoredValues(twigUriWithId, values);
}
public void testOrganizationsUpdate() {
ContentValues values = new ContentValues();
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(Organizations.COMPANY, "Sierra");
values.put(Organizations.PERSON_ID, personId);
values.put(Organizations.TYPE, Organizations.TYPE_CUSTOM);
values.put(Organizations.LABEL, "Club");
values.put(Organizations.TITLE, "Member");
values.put(Organizations.ISPRIMARY, 0);
Uri uri = mResolver.insert(Organizations.CONTENT_URI, values);
values.clear();
values.put(Organizations.COMPANY, "Planetary");
values.put(Organizations.PERSON_ID, personId);
values.put(Organizations.TYPE, Organizations.TYPE_CUSTOM);
values.put(Organizations.LABEL, "Society");
values.put(Organizations.TITLE, "Chair");
values.put(Organizations.ISPRIMARY, 1);
mResolver.update(uri, values, null, null);
assertStoredValues(uri, values);
}
public void testPhonesInsert() {
ContentValues values = new ContentValues();
putContactValues(values);
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(Phones.PERSON_ID, personId);
values.put(Phones.TYPE, Phones.TYPE_CUSTOM);
values.put(Phones.LABEL, "Directory");
values.put(Phones.NUMBER, "1-800-4664-411");
values.put(Phones.ISPRIMARY, 1);
Uri uri = mResolver.insert(Phones.CONTENT_URI, values);
ContentValues expectedResults[] = new ContentValues[1];
// Adding another value to assert
expectedResults[0] = new ContentValues(values);
expectedResults[0].put(Phones.NUMBER_KEY, "11446640081");
// The result is joined with People
putContactValues(expectedResults[0]);
assertStoredValues(uri, expectedResults);
assertSelection(Phones.CONTENT_URI, values, "phones",
Phones._ID, ContentUris.parseId(uri));
// Access the phone through People
Uri twigUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
assertStoredValues(twigUri, expectedResults);
// Now the person should be joined with Phone
values.clear();
putContactValues(values);
values.put(People.TYPE, Phones.TYPE_CUSTOM);
values.put(People.LABEL, "Directory");
values.put(People.NUMBER, "1-800-4664-411");
assertStoredValues(personUri, values);
assertPersonIdConstraint(Phones.CONTENT_URI, Phones.TYPE, Phones.TYPE_WORK);
assertTypeAndLabelConstraints(Phones.CONTENT_URI, Phones.PERSON_ID, personId, Phones.TYPE,
Phones.TYPE_CUSTOM, Phones.TYPE_OTHER, Phones.LABEL);
}
public void testPhonesUpdate() {
ContentValues values = new ContentValues();
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(Phones.PERSON_ID, personId);
values.put(Phones.TYPE, Phones.TYPE_CUSTOM);
values.put(Phones.LABEL, "Directory");
values.put(Phones.NUMBER, "1-800-4664-411");
values.put(Phones.ISPRIMARY, 0);
Uri uri = mResolver.insert(Phones.CONTENT_URI, values);
values.clear();
values.put(Phones.PERSON_ID, personId);
values.put(Phones.TYPE, Phones.TYPE_FAX_HOME);
values.putNull(Phones.LABEL);
values.put(Phones.NUMBER, "1-800-555-4663");
values.put(Phones.ISPRIMARY, 1);
mResolver.update(uri, values, null, null);
ContentValues[] expectedValues = new ContentValues[1];
expectedValues[0] = values;
expectedValues[0].put(Phones.NUMBER_KEY, "36645550081");
assertStoredValues(uri, expectedValues);
}
public void testPhonesFilterQuery() {
ContentValues values = new ContentValues();
putContactValues(values);
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(Phones.PERSON_ID, personId);
values.put(Phones.TYPE, Phones.TYPE_CUSTOM);
values.put(Phones.LABEL, "Directory");
values.put(Phones.NUMBER, "800-4664-411");
values.put(Phones.ISPRIMARY, 1);
Uri uri = mResolver.insert(Phones.CONTENT_URI, values);
Uri filterUri1 = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, "18004664411");
ContentValues[] expectedValues = new ContentValues[1];
expectedValues[0] = values;
expectedValues[0].put(Phones.NUMBER_KEY, "1144664008");
assertStoredValues(filterUri1, expectedValues);
Uri filterUri2 = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, "7773334444");
assertEquals(0, getCount(filterUri2, null, null));
}
public void testEmailInsert() {
assertContactMethodInsert(Contacts.KIND_EMAIL,
ContactMethods.TYPE_CUSTOM, "Some other way", "foo@acme.com", null, true);
}
public void testEmailUpdate() {
assertContactMethodUpdate(Contacts.KIND_EMAIL,
ContactMethods.TYPE_CUSTOM, "Some other way", "foo@acme.com", null, false,
ContactMethods.TYPE_HOME, null, "bar@acme.com", "aux", true);
}
public void testImInsertStandard() {
assertContactMethodInsert(Contacts.KIND_IM,
ContactMethods.TYPE_CUSTOM, "Some other way", "pre:3", "Bar", true);
}
public void testImUpdateStandard() {
assertContactMethodUpdate(Contacts.KIND_IM,
ContactMethods.TYPE_CUSTOM, "Some other way", "pre:3", "Bar", false,
ContactMethods.TYPE_WORK, null, "pre:4", "Buz", true);
}
public void testImInsertCustom() {
assertContactMethodInsert(Contacts.KIND_IM,
ContactMethods.TYPE_CUSTOM, "Some other way", "custom:my_proto", "Bar", true);
}
public void testImUpdateCustom() {
assertContactMethodUpdate(Contacts.KIND_IM,
ContactMethods.TYPE_CUSTOM, "Some other way", "custom:my_proto", "Bar", false,
ContactMethods.TYPE_HOME, null, "custom:my_other_proto", "Buz", true);
}
public void testPostalInsert() {
assertContactMethodInsert(Contacts.KIND_POSTAL,
ContactMethods.TYPE_CUSTOM, "Some other way", "Foo", "Bar", true);
}
public void testPostalUpdate() {
assertContactMethodUpdate(Contacts.KIND_POSTAL,
ContactMethods.TYPE_CUSTOM, "Some other way", "Foo", "Bar", false,
ContactMethods.TYPE_WORK, null, "Biz", "Baz", true);
}
private void assertContactMethodInsert(int kind, int type, String label, String data,
String auxData, boolean primary) {
ContentValues values = new ContentValues();
putContactValues(values);
final Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(ContactMethods.PERSON_ID, personId);
values.put(ContactMethods.KIND, kind);
values.put(ContactMethods.TYPE, type);
values.put(ContactMethods.LABEL, label);
values.put(ContactMethods.DATA, data);
values.put(ContactMethods.AUX_DATA, auxData);
values.put(ContactMethods.ISPRIMARY, primary ? 1 : 0);
Uri uri = mResolver.insert(ContactMethods.CONTENT_URI, values);
// The result is joined with People
putContactValues(values);
assertStoredValues(uri, values);
assertSelection(ContactMethods.CONTENT_URI, values, "contact_methods",
ContactMethods._ID, ContentUris.parseId(uri));
// Access the contact method through People
Uri twigUri = Uri.withAppendedPath(personUri, People.ContactMethods.CONTENT_DIRECTORY);
assertStoredValues(twigUri, values);
assertPersonIdConstraint(ContactMethods.CONTENT_URI, ContactMethods.TYPE,
ContactMethods.TYPE_WORK);
assertTypeAndLabelConstraints(ContactMethods.CONTENT_URI, ContactMethods.PERSON_ID,
personId, ContactMethods.TYPE, ContactMethods.TYPE_CUSTOM,
ContactMethods.TYPE_OTHER, ContactMethods.LABEL);
}
private void assertContactMethodUpdate(int kind,
int type1, String label1, String data1, String auxData1, boolean primary1,
int type2, String label2, String data2, String auxData2, boolean primary2) {
ContentValues values = new ContentValues();
putContactValues(values);
final Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(ContactMethods.PERSON_ID, personId);
values.put(ContactMethods.KIND, kind);
values.put(ContactMethods.TYPE, type1);
values.put(ContactMethods.LABEL, label1);
values.put(ContactMethods.DATA, data1);
values.put(ContactMethods.AUX_DATA, auxData1);
values.put(ContactMethods.ISPRIMARY, primary1 ? 1 : 0);
Uri uri = mResolver.insert(ContactMethods.CONTENT_URI, values);
values.clear();
values.put(ContactMethods.TYPE, type2);
values.put(ContactMethods.LABEL, label2);
values.put(ContactMethods.DATA, data2);
values.put(ContactMethods.AUX_DATA, auxData2);
values.put(ContactMethods.ISPRIMARY, primary2 ? 1 : 0);
mResolver.update(uri, values, null, null);
assertStoredValues(uri, values);
}
public void testExtensionsInsert() {
ContentValues values = new ContentValues();
final Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(Extensions.PERSON_ID, personId);
values.put(Extensions.NAME, "Foo");
values.put(Extensions.VALUE, "Bar");
Uri uri = mResolver.insert(Extensions.CONTENT_URI, values);
assertStoredValues(uri, values);
assertSelection(Extensions.CONTENT_URI, values, "extensions",
Extensions._ID, ContentUris.parseId(uri));
// Access the extensions through People
Uri twigUri = Uri.withAppendedPath(personUri, People.Extensions.CONTENT_DIRECTORY);
assertStoredValues(twigUri, values);
}
public void testExtensionsUpdate() {
ContentValues values = new ContentValues();
final Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(Extensions.PERSON_ID, personId);
values.put(Extensions.NAME, "Foo");
values.put(Extensions.VALUE, "Bar");
Uri uri = mResolver.insert(Extensions.CONTENT_URI, values);
values.clear();
values.put(Extensions.NAME, "Biz");
values.put(Extensions.VALUE, "Baz");
mResolver.update(uri, values, null, null);
assertStoredValues(uri, values);
}
public void testGroupsInsert() {
ContentValues values = new ContentValues();
values.put(Groups.NAME, "Galois");
values.put(Groups.NOTES, "Abel");
values.put(Groups.SYSTEM_ID, "12345");
Uri groupUri = mResolver.insert(Groups.CONTENT_URI, values);
assertStoredValues(groupUri, values);
}
public void testGroupsUpdate() {
ContentValues values = new ContentValues();
values.put(Groups.NAME, "Galois");
values.put(Groups.NOTES, "Abel");
values.put(Groups.SYSTEM_ID, "12345");
Uri groupUri = mResolver.insert(Groups.CONTENT_URI, values);
values.clear();
values.put(Groups.NAME, "Klein");
values.put(Groups.NOTES, "Vierergruppe");
values.put(Groups.SYSTEM_ID, "1234");
mResolver.update(groupUri, values, null, null);
assertStoredValues(groupUri, values);
}
public void testGroupMembershipsInsert() {
ContentValues values = new ContentValues();
values.put(Groups.NOTES, "Abel");
Uri groupUri = insertLegacyGroup("Galois", values);
Uri personUri = insertPerson("Klein", values);
Uri membershipUri = insertLegacyGroupMembership(groupUri, personUri, values);
values.put(Groups.NAME, "Galois");
values.put(Groups.NOTES, "Abel");
assertStoredValues(membershipUri, values);
assertSelection(GroupMembership.CONTENT_URI, values, "groupmembership",
GroupMembership._ID, ContentUris.parseId(membershipUri));
Uri personsGroupsUri = Uri.withAppendedPath(personUri, GroupMembership.CONTENT_DIRECTORY);
assertStoredValues(personsGroupsUri, values);
}
public void testAddToGroup() {
ContentValues values = new ContentValues();
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(Groups.NAME, "Galois");
Uri groupUri = mResolver.insert(Groups.CONTENT_URI, values);
People.addToGroup(mResolver, personId, "Galois");
values.clear();
values.put(GroupMembership.GROUP_ID, ContentUris.parseId(groupUri));
values.put(GroupMembership.PERSON_ID, personId);
Uri personsGroupsUri = Uri.withAppendedPath(personUri, GroupMembership.CONTENT_DIRECTORY);
assertStoredValues(personsGroupsUri, values);
}
public void testGroupMembersByGroupName() {
ContentValues values = new ContentValues();
Uri groupUri1 = insertLegacyGroup("Galois", values);
Uri personUri1 = insertPerson("Klein", values);
insertLegacyGroupMembership(groupUri1, personUri1, values);
Uri groupUri2 = insertLegacyGroup("Euler", values);
Uri personUri2 = insertPerson("Lagrange", values);
insertLegacyGroupMembership(groupUri2, personUri2, values);
// NOTE: testing non-public API support
assertStoredValue(Uri.parse("content://contacts/groups/name/Galois/members"),
People.NAME, "Klein");
assertStoredValue(Uri.parse("content://contacts/groups/name/Euler/members"),
People.NAME, "Lagrange");
}
public void testPhotoUpdate() throws Exception {
byte[] photo = loadPhotoFromResource(
com.android.providers.contacts.tests.R.drawable.earth_small, PhotoSize.ORIGINAL);
byte[] thumbnailedPhoto = loadPhotoFromResource(
com.android.providers.contacts.tests.R.drawable.earth_small, PhotoSize.THUMBNAIL);
ContentValues values = new ContentValues();
Uri personUri = mResolver.insert(People.CONTENT_URI, values);
values.clear();
values.put(Photos.DATA, photo);
values.put(Photos.LOCAL_VERSION, "10");
// FIXME this column was unavailable for update in legacy ContactsProvider
// values.put(Photos.DOWNLOAD_REQUIRED, 1);
values.put(Photos.EXISTS_ON_SERVER, 1);
values.put(Photos.SYNC_ERROR, "404 does not exist");
Uri photoUri = Uri.withAppendedPath(personUri, Photos.CONTENT_DIRECTORY);
mResolver.update(photoUri, values, null, null);
values.put(Photos.DATA, thumbnailedPhoto);
assertStoredValues(photoUri, values);
long photoId = Long.parseLong(getStoredValue(photoUri, Photos._ID));
values.put(Photos.LOCAL_VERSION, "11");
values.put(Photos.DATA, photo);
Uri contentUri = ContentUris.withAppendedId(Photos.CONTENT_URI, photoId);
mResolver.update(contentUri, values, null, null);
values.put(Photos.DATA, thumbnailedPhoto);
assertStoredValues(contentUri, values);
assertStoredValues(photoUri, values);
}
/**
* Capturing the search suggestion requirements in test cases as a reference.
*/
public void testSearchSuggestionsNotInMyContacts() throws Exception {
// We don't provide compatibility for search suggestions
return;
}
/**
* Capturing the search suggestion requirements in test cases as a reference.
*/
public void testSearchSuggestionsByName() throws Exception {
// We don't provide compatibility for search suggestions
return;
}
private void assertSearchSuggestion(boolean name, boolean photo, boolean organization,
boolean phone, boolean email, String query, boolean expectIcon1Uri, String expectedIcon2,
String expectedText1, String expectedText2) throws IOException {
ContentValues values = new ContentValues();
if (name) {
values.put(People.NAME, "Deer Dough");
}
final Uri personUri = mResolver.insert(People.CONTENT_URI, values);
long personId = ContentUris.parseId(personUri);
People.addToMyContactsGroup(mResolver, personId);
if (photo) {
values.clear();
byte[] photoData = loadTestPhoto();
values.put(Photos.DATA, photoData);
values.put(Photos.LOCAL_VERSION, "1");
values.put(Photos.EXISTS_ON_SERVER, 0);
Uri photoUri = Uri.withAppendedPath(personUri, Photos.CONTENT_DIRECTORY);
mResolver.update(photoUri, values, null, null);
}
if (organization) {
values.clear();
values.put(Organizations.ISPRIMARY, 1);
values.put(Organizations.COMPANY, "Google");
values.put(Organizations.TYPE, Organizations.TYPE_WORK);
values.put(Organizations.PERSON_ID, personId);
mResolver.insert(Organizations.CONTENT_URI, values);
}
if (email) {
values.clear();
values.put(ContactMethods.PERSON_ID, personId);
values.put(ContactMethods.KIND, Contacts.KIND_EMAIL);
values.put(ContactMethods.TYPE, ContactMethods.TYPE_HOME);
values.put(ContactMethods.DATA, "foo@acme.com");
values.put(ContactMethods.ISPRIMARY, 1);
mResolver.insert(ContactMethods.CONTENT_URI, values);
String protocol = ContactMethods
.encodePredefinedImProtocol(ContactMethods.PROTOCOL_GOOGLE_TALK);
values.clear();
values.put(Presence.IM_PROTOCOL, protocol);
values.put(Presence.IM_HANDLE, "foo@acme.com");
values.put(Presence.IM_ACCOUNT, "foo");
values.put(Presence.PRESENCE_STATUS, Presence.OFFLINE);
values.put(Presence.PRESENCE_CUSTOM_STATUS, "Coding for Android");
mResolver.insert(Presence.CONTENT_URI, values);
}
if (phone) {
values.clear();
values.put(Phones.PERSON_ID, personId);
values.put(Phones.TYPE, Phones.TYPE_HOME);
values.put(Phones.NUMBER, "1-800-4664-411");
values.put(Phones.ISPRIMARY, 1);
mResolver.insert(Phones.CONTENT_URI, values);
}
Uri searchUri = new Uri.Builder().scheme("content").authority(Contacts.AUTHORITY)
.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY).appendPath(query).build();
Cursor c = mResolver.query(searchUri, null, null, null, null);
assertEquals(1, c.getCount());
c.moveToFirst();
values.clear();
String icon1 = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1));
if (expectIcon1Uri) {
assertTrue(icon1.startsWith("content:"));
} else {
assertEquals(String.valueOf(com.android.internal.R.drawable.ic_contact_picture), icon1);
}
// SearchManager does not declare a constant for _id
values.put("_id", personId);
values.put(SearchManager.SUGGEST_COLUMN_ICON_2, expectedIcon2);
values.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, personId);
values.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, personId);
values.put(SearchManager.SUGGEST_COLUMN_TEXT_1, expectedText1);
values.put(SearchManager.SUGGEST_COLUMN_TEXT_2, expectedText2);
assertCursorValues(c, values);
c.close();
// Cleanup
mResolver.delete(personUri, null, null);
}
/**
* Capturing the search suggestion requirements in test cases as a reference.
*/
public void testSearchSuggestionsByPhoneNumber() throws Exception {
// We don't provide compatibility for search suggestions
return;
}
public void testSettings() throws Exception {
mActor.addAuthority(ContactsContract.AUTHORITY);
ContentValues values = new ContentValues();
values.put(Settings._SYNC_ACCOUNT, "foo");
values.put(Settings._SYNC_ACCOUNT_TYPE, "bar");
values.put(Settings.KEY, Settings.SYNC_EVERYTHING);
values.put(Settings.VALUE, 7);
mResolver.update(Settings.CONTENT_URI, values, null, null);
assertStoredValue(Settings.CONTENT_URI, Settings._SYNC_ACCOUNT + "='foo' AND "
+ Settings.KEY + "='" + Settings.SYNC_EVERYTHING + "'", null, Settings.VALUE, "7");
assertStoredValue(ContactsContract.Settings.CONTENT_URI,
ContactsContract.Settings.ACCOUNT_NAME + "='foo'",
null, ContactsContract.Settings.SHOULD_SYNC, "7");
values.clear();
values.put(ContactsContract.Settings.SHOULD_SYNC, 8);
mResolver.update(ContactsContract.Settings.CONTENT_URI, values,
ContactsContract.Settings.ACCOUNT_NAME + "='foo'", null);
assertStoredValue(Settings.CONTENT_URI, Settings._SYNC_ACCOUNT + "='foo' AND "
+ Settings.KEY + "='" + Settings.SYNC_EVERYTHING + "'", null, Settings.VALUE, "8");
}
private Uri insertPerson(String name, ContentValues values) {
values.put(People.NAME, name);
return mResolver.insert(People.CONTENT_URI, values);
}
private Uri insertLegacyGroup(String name, ContentValues values) {
values.put(Groups.NAME, name);
return mResolver.insert(Groups.CONTENT_URI, values);
}
private Uri insertLegacyGroupMembership(Uri groupUri, Uri personUri, ContentValues values) {
long groupId = ContentUris.parseId(groupUri);
long personId = ContentUris.parseId(personUri);
values.clear();
values.put(GroupMembership.GROUP_ID, groupId);
values.put(GroupMembership.PERSON_ID, personId);
Uri membershipUri = mResolver.insert(GroupMembership.CONTENT_URI, values);
return membershipUri;
}
private void putContactValues(ContentValues values) {
putContactValuesExceptName(values);
values.put(People.NAME, "Deer Dough");
}
private void putContactValuesExceptName(ContentValues values) {
// Populating only unhidden columns
values.put(People.PHONETIC_NAME, "Dear Doe");
values.put(People.NOTES, "Cash Cow");
values.put(People.TIMES_CONTACTED, 3);
values.put(People.LAST_TIME_CONTACTED, 10);
values.put(People.CUSTOM_RINGTONE, "ringtone");
values.put(People.SEND_TO_VOICEMAIL, 1);
values.put(People.STARRED, 1);
}
private void putContactValues2(ContentValues values) {
// Populating only unhidden columns
values.put(People.NAME, "John Doe");
values.put(People.PHONETIC_NAME, "jon dawe");
values.put(People.NOTES, "Poor Johnny");
values.put(People.TIMES_CONTACTED, 4);
values.put(People.LAST_TIME_CONTACTED, 11);
values.put(People.CUSTOM_RINGTONE, "rangtone");
values.put(People.SEND_TO_VOICEMAIL, 0);
values.put(People.STARRED, 0);
}
private void assertFilteredContacts(String filter, String... expectedNames) {
Uri filterUri = Uri.withAppendedPath(People.CONTENT_FILTER_URI, filter);
Cursor c = mResolver.query(filterUri, null, null, null, null);
try {
assertEquals("Record count", expectedNames.length, c.getCount());
int column = c.getColumnIndex(People.NAME);
for (int i = 0; i < expectedNames.length; i++) {
c.moveToNext();
assertEquals(expectedNames[i], c.getString(column));
}
} finally {
c.close();
}
}
private void assertPersonIdConstraint(Uri uri, String typeColumn, int defaultType) {
ContentValues values = new ContentValues();
values.put(typeColumn, defaultType);
try {
mResolver.insert(uri, values);
fail("Inserted row without person ID");
} catch (Exception e) {
// Exception expected
}
}
private void assertTypeAndLabelConstraints(Uri uri, String personIdColumn, long personId,
String typeColumn, int defaultType, int otherType, String labelColumn) {
ContentValues values = new ContentValues();
values.put(personIdColumn, personId);
values.put(labelColumn, "Foo");
try {
mResolver.insert(uri, values);
fail("Inserted row with label without defining type");
} catch (Exception e) {
// Exception expected
}
}
protected void assertSelection(Uri uri, ContentValues values, String legacyTable,
String idColumn, long id) {
values.put(idColumn, id);
String qualified = legacyTable + "." + idColumn;
super.assertSelection(uri, values, qualified, id);
}
}
|