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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
|
/*
* 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 static com.android.providers.contacts.ContactsActor.PACKAGE_GREY;
import static com.android.providers.contacts.TestUtils.cv;
import android.accounts.Account;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Entity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.AggregationExceptions;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
import android.provider.ContactsContract.CommonDataKinds.Identity;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.CommonDataKinds.SipAddress;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.Groups;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.Settings;
import android.provider.ContactsContract.StatusUpdates;
import android.provider.ContactsContract.StreamItems;
import android.test.MoreAsserts;
import android.test.mock.MockContentResolver;
import android.util.Log;
import com.android.providers.contacts.ContactsDatabaseHelper.Tables;
import com.android.providers.contacts.testutil.CommonDatabaseUtils;
import com.android.providers.contacts.testutil.DataUtil;
import com.android.providers.contacts.testutil.RawContactUtil;
import com.android.providers.contacts.testutil.TestUtil;
import com.android.providers.contacts.util.Hex;
import com.android.providers.contacts.util.MockClock;
import com.google.android.collect.Sets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* A common superclass for {@link ContactsProvider2}-related tests.
*/
public abstract class BaseContactsProvider2Test extends PhotoLoadingTestCase {
static final String ADD_VOICEMAIL_PERMISSION =
"com.android.voicemail.permission.ADD_VOICEMAIL";
/*
* Permission to allow querying voicemails
*/
static final String READ_VOICEMAIL_PERMISSION =
"com.android.voicemail.permission.READ_VOICEMAIL";
/*
* Permission to allow deleting and updating voicemails
*/
static final String WRITE_VOICEMAIL_PERMISSION =
"com.android.voicemail.permission.WRITE_VOICEMAIL";
protected static final String PACKAGE = "ContactsProvider2Test";
public static final String READ_ONLY_ACCOUNT_TYPE =
SynchronousContactsProvider2.READ_ONLY_ACCOUNT_TYPE;
protected ContactsActor mActor;
protected MockContentResolver mResolver;
protected Account mAccount = new Account("account1", "account type1");
protected Account mAccountTwo = new Account("account2", "account type2");
protected final static Long NO_LONG = new Long(0);
protected final static String NO_STRING = new String("");
protected final static Account NO_ACCOUNT = new Account("a", "b");
/**
* Use {@link MockClock#install()} to start using it.
* It'll be automatically uninstalled by {@link #tearDown()}.
*/
protected static final MockClock sMockClock = new MockClock();
protected Class<? extends ContentProvider> getProviderClass() {
return SynchronousContactsProvider2.class;
}
protected String getAuthority() {
return ContactsContract.AUTHORITY;
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActor = new ContactsActor(getContext(), PACKAGE_GREY, getProviderClass(), getAuthority());
mResolver = mActor.resolver;
if (mActor.provider instanceof SynchronousContactsProvider2) {
getContactsProvider().wipeData();
}
// Give the actor access to read/write contacts and profile data by default.
mActor.addPermissions(
"android.permission.READ_CONTACTS",
"android.permission.WRITE_CONTACTS",
"android.permission.READ_SOCIAL_STREAM",
"android.permission.WRITE_SOCIAL_STREAM",
"android.permission.READ_PROFILE",
"android.permission.WRITE_PROFILE");
}
@Override
protected void tearDown() throws Exception {
sMockClock.uninstall();
super.tearDown();
}
public SynchronousContactsProvider2 getContactsProvider() {
return (SynchronousContactsProvider2) mActor.provider;
}
public Context getMockContext() {
return mActor.context;
}
public void addAuthority(String authority) {
mActor.addAuthority(authority);
}
public ContentProvider addProvider(Class<? extends ContentProvider> providerClass,
String authority) throws Exception {
return mActor.addProvider(providerClass, authority);
}
public ContentProvider getProvider() {
return mActor.provider;
}
protected Uri setCallerIsSyncAdapter(Uri uri, Account account) {
if (account == null) {
return uri;
}
final Uri.Builder builder = uri.buildUpon();
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, account.name);
builder.appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type);
builder.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true");
return builder.build();
}
protected int updateItem(Uri uri, long id, String... extras) {
Uri itemUri = ContentUris.withAppendedId(uri, id);
return updateItem(itemUri, extras);
}
protected int updateItem(Uri uri, String... extras) {
ContentValues values = new ContentValues();
CommonDatabaseUtils.extrasVarArgsToValues(values, extras);
return mResolver.update(uri, values, null, null);
}
protected long createGroup(Account account, String sourceId, String title) {
return createGroup(account, sourceId, title, 1, false, false);
}
protected long createGroup(Account account, String sourceId, String title, int visible) {
return createGroup(account, sourceId, title, visible, false, false);
}
protected long createAutoAddGroup(Account account) {
return createGroup(account, "auto", "auto",
0 /* visible */, true /* auto-add */, false /* fav */);
}
protected long createGroup(Account account, String sourceId, String title,
int visible, boolean autoAdd, boolean favorite) {
ContentValues values = new ContentValues();
values.put(Groups.SOURCE_ID, sourceId);
values.put(Groups.TITLE, title);
values.put(Groups.GROUP_VISIBLE, visible);
values.put(Groups.AUTO_ADD, autoAdd ? 1 : 0);
values.put(Groups.FAVORITES, favorite ? 1 : 0);
final Uri uri = TestUtil.maybeAddAccountQueryParameters(Groups.CONTENT_URI, account);
return ContentUris.parseId(mResolver.insert(uri, values));
}
protected void createSettings(Account account, String shouldSync, String ungroupedVisible) {
createSettings(new AccountWithDataSet(account.name, account.type, null),
shouldSync, ungroupedVisible);
}
protected void createSettings(AccountWithDataSet account, String shouldSync,
String ungroupedVisible) {
ContentValues values = new ContentValues();
values.put(Settings.ACCOUNT_NAME, account.getAccountName());
values.put(Settings.ACCOUNT_TYPE, account.getAccountType());
if (account.getDataSet() != null) {
values.put(Settings.DATA_SET, account.getDataSet());
}
values.put(Settings.SHOULD_SYNC, shouldSync);
values.put(Settings.UNGROUPED_VISIBLE, ungroupedVisible);
mResolver.insert(Settings.CONTENT_URI, values);
}
protected Uri insertOrganization(long rawContactId, ContentValues values) {
return insertOrganization(rawContactId, values, false);
}
protected Uri insertOrganization(long rawContactId, ContentValues values, boolean primary) {
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
values.put(Organization.TYPE, Organization.TYPE_WORK);
if (primary) {
values.put(Data.IS_PRIMARY, 1);
}
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertPhoneNumber(long rawContactId, String phoneNumber) {
return insertPhoneNumber(rawContactId, phoneNumber, false);
}
protected Uri insertPhoneNumber(long rawContactId, String phoneNumber, boolean primary) {
return insertPhoneNumber(rawContactId, phoneNumber, primary, Phone.TYPE_HOME);
}
protected Uri insertPhoneNumber(long rawContactId, String phoneNumber, boolean primary,
int type) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, phoneNumber);
values.put(Phone.TYPE, type);
if (primary) {
values.put(Data.IS_PRIMARY, 1);
}
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertEmail(long rawContactId, String email) {
return insertEmail(rawContactId, email, false);
}
protected Uri insertEmail(long rawContactId, String email, boolean primary) {
return insertEmail(rawContactId, email, primary, Email.TYPE_HOME, null);
}
protected Uri insertEmail(long rawContactId, String email, boolean primary,
boolean superPrimary) {
return insertEmail(rawContactId, email, primary, superPrimary, Email.TYPE_HOME, null);
}
protected Uri insertEmail(long rawContactId, String email, boolean primary, int type,
String label) {
return insertEmail(rawContactId, email, primary, false, type, label);
}
protected Uri insertEmail(long rawContactId, String email, boolean primary,
boolean superPrimary, int type, String label) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
values.put(Email.DATA, email);
values.put(Email.TYPE, type);
values.put(Email.LABEL, label);
if (primary) {
values.put(Data.IS_PRIMARY, 1);
}
if (superPrimary) {
values.put(Data.IS_SUPER_PRIMARY, 1);
}
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertSipAddress(long rawContactId, String sipAddress) {
return insertSipAddress(rawContactId, sipAddress, false);
}
protected Uri insertSipAddress(long rawContactId, String sipAddress, boolean primary) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, SipAddress.CONTENT_ITEM_TYPE);
values.put(SipAddress.SIP_ADDRESS, sipAddress);
if (primary) {
values.put(Data.IS_PRIMARY, 1);
}
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertNickname(long rawContactId, String nickname) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE);
values.put(Nickname.NAME, nickname);
values.put(Nickname.TYPE, Nickname.TYPE_OTHER_NAME);
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertPostalAddress(long rawContactId, String formattedAddress) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
values.put(StructuredPostal.FORMATTED_ADDRESS, formattedAddress);
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertPostalAddress(long rawContactId, ContentValues values) {
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertPhoto(long rawContactId) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
values.put(Photo.PHOTO, loadTestPhoto());
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertPhoto(long rawContactId, int resourceId) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
values.put(Photo.PHOTO, loadPhotoFromResource(resourceId, PhotoSize.ORIGINAL));
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertGroupMembership(long rawContactId, String sourceId) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);
values.put(GroupMembership.GROUP_SOURCE_ID, sourceId);
return mResolver.insert(Data.CONTENT_URI, values);
}
protected Uri insertGroupMembership(long rawContactId, Long groupId) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE);
values.put(GroupMembership.GROUP_ROW_ID, groupId);
return mResolver.insert(Data.CONTENT_URI, values);
}
public void removeGroupMemberships(long rawContactId) {
mResolver.delete(Data.CONTENT_URI,
Data.MIMETYPE + "=? AND " + GroupMembership.RAW_CONTACT_ID + "=?",
new String[] { GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(rawContactId) });
}
protected Uri insertStatusUpdate(int protocol, String customProtocol, String handle,
int presence, String status, int chatMode) {
return insertStatusUpdate(protocol, customProtocol, handle, presence, status, chatMode,
false);
}
protected Uri insertStatusUpdate(int protocol, String customProtocol, String handle,
int presence, String status, int chatMode, boolean isUserProfile) {
return insertStatusUpdate(protocol, customProtocol, handle, presence, status, 0, chatMode,
isUserProfile);
}
protected Uri insertStatusUpdate(int protocol, String customProtocol, String handle,
int presence, String status, long timestamp, int chatMode, boolean isUserProfile) {
ContentValues values = new ContentValues();
values.put(StatusUpdates.PROTOCOL, protocol);
values.put(StatusUpdates.CUSTOM_PROTOCOL, customProtocol);
values.put(StatusUpdates.IM_HANDLE, handle);
return insertStatusUpdate(values, presence, status, timestamp, chatMode, isUserProfile);
}
protected Uri insertStatusUpdate(
long dataId, int presence, String status, long timestamp, int chatMode) {
return insertStatusUpdate(dataId, presence, status, timestamp, chatMode, false);
}
protected Uri insertStatusUpdate(
long dataId, int presence, String status, long timestamp, int chatMode,
boolean isUserProfile) {
ContentValues values = new ContentValues();
values.put(StatusUpdates.DATA_ID, dataId);
return insertStatusUpdate(values, presence, status, timestamp, chatMode, isUserProfile);
}
private Uri insertStatusUpdate(
ContentValues values, int presence, String status, long timestamp, int chatMode,
boolean isUserProfile) {
if (presence != 0) {
values.put(StatusUpdates.PRESENCE, presence);
values.put(StatusUpdates.CHAT_CAPABILITY, chatMode);
}
if (status != null) {
values.put(StatusUpdates.STATUS, status);
}
if (timestamp != 0) {
values.put(StatusUpdates.STATUS_TIMESTAMP, timestamp);
}
Uri insertUri = isUserProfile
? StatusUpdates.PROFILE_CONTENT_URI
: StatusUpdates.CONTENT_URI;
Uri resultUri = mResolver.insert(insertUri, values);
return resultUri;
}
protected Uri insertStreamItem(long rawContactId, ContentValues values, Account account) {
return mResolver.insert(
TestUtil.maybeAddAccountQueryParameters(Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
RawContacts.StreamItems.CONTENT_DIRECTORY), account),
values);
}
protected Uri insertStreamItemPhoto(long streamItemId, ContentValues values, Account account) {
return mResolver.insert(
TestUtil.maybeAddAccountQueryParameters(Uri.withAppendedPath(
ContentUris.withAppendedId(StreamItems.CONTENT_URI, streamItemId),
StreamItems.StreamItemPhotos.CONTENT_DIRECTORY), account),
values);
}
protected Uri insertImHandle(long rawContactId, int protocol, String customProtocol,
String handle) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
values.put(Im.PROTOCOL, protocol);
values.put(Im.CUSTOM_PROTOCOL, customProtocol);
values.put(Im.DATA, handle);
values.put(Im.TYPE, Im.TYPE_HOME);
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertEvent(long rawContactId, int type, String date) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Event.CONTENT_ITEM_TYPE);
values.put(Event.TYPE, type);
values.put(Event.START_DATE, date);
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertNote(long rawContactId, String note) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE);
values.put(Note.NOTE, note);
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected Uri insertIdentity(long rawContactId, String identity, String namespace) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Identity.CONTENT_ITEM_TYPE);
values.put(Identity.NAMESPACE, namespace);
values.put(Identity.IDENTITY, identity);
Uri resultUri = mResolver.insert(Data.CONTENT_URI, values);
return resultUri;
}
protected void setContactAccount(long rawContactId, String accountType, String accountName) {
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
mResolver.update(ContentUris.withAppendedId(
RawContacts.CONTENT_URI, rawContactId), values, null, null);
}
protected void setAggregationException(int type, long rawContactId1, long rawContactId2) {
ContentValues values = new ContentValues();
values.put(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1);
values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2);
values.put(AggregationExceptions.TYPE, type);
assertEquals(1, mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null));
}
protected void markInvisible(long contactId) {
// There's no api for this, so we just tweak the DB directly.
SQLiteDatabase db = ((ContactsProvider2) getProvider()).getDatabaseHelper()
.getWritableDatabase();
db.execSQL("DELETE FROM " + Tables.DEFAULT_DIRECTORY +
" WHERE " + BaseColumns._ID + "=" + contactId);
}
protected Cursor queryRawContact(long rawContactId) {
return mResolver.query(ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
null, null, null, null);
}
protected Cursor queryContact(long contactId) {
return mResolver.query(ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
null, null, null, null);
}
protected Cursor queryContact(long contactId, String[] projection) {
return mResolver.query(ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
projection, null, null, null);
}
protected Uri getContactUriForRawContact(long rawContactId) {
return ContentUris.withAppendedId(Contacts.CONTENT_URI, queryContactId(rawContactId));
}
protected long queryContactId(long rawContactId) {
Cursor c = queryRawContact(rawContactId);
assertTrue(c.moveToFirst());
long contactId = c.getLong(c.getColumnIndex(RawContacts.CONTACT_ID));
c.close();
return contactId;
}
protected long queryPhotoId(long contactId) {
Cursor c = queryContact(contactId);
assertTrue(c.moveToFirst());
long photoId = c.getInt(c.getColumnIndex(Contacts.PHOTO_ID));
c.close();
return photoId;
}
protected long queryPhotoFileId(long contactId) {
return getStoredLongValue(ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
Contacts.PHOTO_FILE_ID);
}
protected boolean queryRawContactIsStarred(long rawContactId) {
Cursor c = queryRawContact(rawContactId);
try {
assertTrue(c.moveToFirst());
return c.getLong(c.getColumnIndex(RawContacts.STARRED)) != 0;
} finally {
c.close();
}
}
protected String queryDisplayName(long contactId) {
Cursor c = queryContact(contactId);
assertTrue(c.moveToFirst());
String displayName = c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME));
c.close();
return displayName;
}
protected String queryLookupKey(long contactId) {
Cursor c = queryContact(contactId);
assertTrue(c.moveToFirst());
String lookupKey = c.getString(c.getColumnIndex(Contacts.LOOKUP_KEY));
c.close();
return lookupKey;
}
protected void assertAggregated(long rawContactId1, long rawContactId2) {
long contactId1 = queryContactId(rawContactId1);
long contactId2 = queryContactId(rawContactId2);
assertTrue(contactId1 == contactId2);
}
protected void assertAggregated(long rawContactId1, long rawContactId2,
String expectedDisplayName) {
long contactId1 = queryContactId(rawContactId1);
long contactId2 = queryContactId(rawContactId2);
assertTrue(contactId1 == contactId2);
String displayName = queryDisplayName(contactId1);
assertEquals(expectedDisplayName, displayName);
}
protected void assertNotAggregated(long rawContactId1, long rawContactId2) {
long contactId1 = queryContactId(rawContactId1);
long contactId2 = queryContactId(rawContactId2);
assertTrue(contactId1 != contactId2);
}
protected void assertStructuredName(long rawContactId, String prefix, String givenName,
String middleName, String familyName, String suffix) {
Uri uri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
RawContacts.Data.CONTENT_DIRECTORY);
final String[] projection = new String[] {
StructuredName.PREFIX, StructuredName.GIVEN_NAME, StructuredName.MIDDLE_NAME,
StructuredName.FAMILY_NAME, StructuredName.SUFFIX
};
Cursor c = mResolver.query(uri, projection, Data.MIMETYPE + "='"
+ StructuredName.CONTENT_ITEM_TYPE + "'", null, null);
assertTrue(c.moveToFirst());
assertEquals(prefix, c.getString(0));
assertEquals(givenName, c.getString(1));
assertEquals(middleName, c.getString(2));
assertEquals(familyName, c.getString(3));
assertEquals(suffix, c.getString(4));
c.close();
}
protected long assertSingleGroup(Long rowId, Account account, String sourceId, String title) {
Cursor c = mResolver.query(Groups.CONTENT_URI, null, null, null, null);
try {
assertTrue(c.moveToNext());
long actualRowId = assertGroup(c, rowId, account, sourceId, title);
assertFalse(c.moveToNext());
return actualRowId;
} finally {
c.close();
}
}
protected long assertSingleGroupMembership(Long rowId, Long rawContactId, Long groupRowId,
String sourceId) {
Cursor c = mResolver.query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
try {
assertTrue(c.moveToNext());
long actualRowId = assertGroupMembership(c, rowId, rawContactId, groupRowId, sourceId);
assertFalse(c.moveToNext());
return actualRowId;
} finally {
c.close();
}
}
protected long assertGroupMembership(Cursor c, Long rowId, Long rawContactId, Long groupRowId,
String sourceId) {
assertNullOrEquals(c, rowId, Data._ID);
assertNullOrEquals(c, rawContactId, GroupMembership.RAW_CONTACT_ID);
assertNullOrEquals(c, groupRowId, GroupMembership.GROUP_ROW_ID);
assertNullOrEquals(c, sourceId, GroupMembership.GROUP_SOURCE_ID);
return c.getLong(c.getColumnIndexOrThrow("_id"));
}
protected long assertGroup(Cursor c, Long rowId, Account account, String sourceId, String title) {
assertNullOrEquals(c, rowId, Groups._ID);
assertNullOrEquals(c, account);
assertNullOrEquals(c, sourceId, Groups.SOURCE_ID);
assertNullOrEquals(c, title, Groups.TITLE);
return c.getLong(c.getColumnIndexOrThrow("_id"));
}
private void assertNullOrEquals(Cursor c, Account account) {
if (account == NO_ACCOUNT) {
return;
}
if (account == null) {
assertTrue(c.isNull(c.getColumnIndexOrThrow(Groups.ACCOUNT_NAME)));
assertTrue(c.isNull(c.getColumnIndexOrThrow(Groups.ACCOUNT_TYPE)));
} else {
assertEquals(account.name, c.getString(c.getColumnIndexOrThrow(Groups.ACCOUNT_NAME)));
assertEquals(account.type, c.getString(c.getColumnIndexOrThrow(Groups.ACCOUNT_TYPE)));
}
}
private void assertNullOrEquals(Cursor c, Long value, String columnName) {
if (value != NO_LONG) {
if (value == null) assertTrue(c.isNull(c.getColumnIndexOrThrow(columnName)));
else assertEquals((long) value, c.getLong(c.getColumnIndexOrThrow(columnName)));
}
}
private void assertNullOrEquals(Cursor c, String value, String columnName) {
if (value != NO_STRING) {
if (value == null) assertTrue(c.isNull(c.getColumnIndexOrThrow(columnName)));
else assertEquals(value, c.getString(c.getColumnIndexOrThrow(columnName)));
}
}
protected void assertDataRow(ContentValues actual, String expectedMimetype,
Object... expectedArguments) {
assertEquals(actual.toString(), expectedMimetype, actual.getAsString(Data.MIMETYPE));
for (int i = 0; i < expectedArguments.length; i += 2) {
String columnName = (String) expectedArguments[i];
Object expectedValue = expectedArguments[i + 1];
if (expectedValue instanceof Uri) {
expectedValue = ContentUris.parseId((Uri) expectedValue);
}
if (expectedValue == null) {
assertNull(actual.toString(), actual.get(columnName));
}
if (expectedValue instanceof Long) {
assertEquals("mismatch at " + columnName + " from " + actual.toString(),
expectedValue, actual.getAsLong(columnName));
} else if (expectedValue instanceof Integer) {
assertEquals("mismatch at " + columnName + " from " + actual.toString(),
expectedValue, actual.getAsInteger(columnName));
} else if (expectedValue instanceof String) {
assertEquals("mismatch at " + columnName + " from " + actual.toString(),
expectedValue, actual.getAsString(columnName));
} else {
assertEquals("mismatch at " + columnName + " from " + actual.toString(),
expectedValue, actual.get(columnName));
}
}
}
protected void assertNoRowsAndClose(Cursor c) {
try {
assertFalse(c.moveToNext());
} finally {
c.close();
}
}
protected static class IdComparator implements Comparator<ContentValues> {
@Override
public int compare(ContentValues o1, ContentValues o2) {
long id1 = o1.getAsLong(ContactsContract.Data._ID);
long id2 = o2.getAsLong(ContactsContract.Data._ID);
if (id1 == id2) return 0;
return (id1 < id2) ? -1 : 1;
}
}
protected ContentValues[] asSortedContentValuesArray(
ArrayList<Entity.NamedContentValues> subValues) {
ContentValues[] result = new ContentValues[subValues.size()];
int i = 0;
for (Entity.NamedContentValues subValue : subValues) {
result[i] = subValue.values;
i++;
}
Arrays.sort(result, new IdComparator());
return result;
}
protected void assertDirty(Uri uri, boolean state) {
Cursor c = mResolver.query(uri, new String[]{"dirty"}, null, null, null);
assertTrue(c.moveToNext());
assertEquals(state, c.getLong(0) != 0);
assertFalse(c.moveToNext());
c.close();
}
protected long getVersion(Uri uri) {
Cursor c = mResolver.query(uri, new String[]{"version"}, null, null, null);
assertTrue(c.moveToNext());
long version = c.getLong(0);
assertFalse(c.moveToNext());
c.close();
return version;
}
protected void clearDirty(Uri uri) {
ContentValues values = new ContentValues();
values.put("dirty", 0);
mResolver.update(uri, values, null, null);
}
protected void storeValue(Uri contentUri, long id, String column, String value) {
storeValue(ContentUris.withAppendedId(contentUri, id), column, value);
}
protected void storeValue(Uri contentUri, String column, String value) {
ContentValues values = new ContentValues();
values.put(column, value);
mResolver.update(contentUri, values, null, null);
}
protected void storeValue(Uri contentUri, long id, String column, long value) {
storeValue(ContentUris.withAppendedId(contentUri, id), column, value);
}
protected void storeValue(Uri contentUri, String column, long value) {
ContentValues values = new ContentValues();
values.put(column, value);
mResolver.update(contentUri, values, null, null);
}
protected void assertStoredValue(Uri contentUri, long id, String column, Object expectedValue) {
assertStoredValue(ContentUris.withAppendedId(contentUri, id), column, expectedValue);
}
protected void assertStoredValue(Uri rowUri, String column, Object expectedValue) {
String value = getStoredValue(rowUri, column);
if (expectedValue == null) {
assertNull("Column value " + column, value);
} else {
assertEquals("Column value " + column, String.valueOf(expectedValue), value);
}
}
protected void assertStoredValue(Uri rowUri, String selection, String[] selectionArgs,
String column, Object expectedValue) {
String value = getStoredValue(rowUri, selection, selectionArgs, column);
if (expectedValue == null) {
assertNull("Column value " + column, value);
} else {
assertEquals("Column value " + column, String.valueOf(expectedValue), value);
}
}
protected String getStoredValue(Uri rowUri, String column) {
return getStoredValue(rowUri, null, null, column);
}
protected String getStoredValue(Uri uri, String selection, String[] selectionArgs,
String column) {
String value = null;
Cursor c = mResolver.query(uri, new String[] { column }, selection, selectionArgs, null);
try {
assertEquals("Record count for " + uri, 1, c.getCount());
if (c.moveToFirst()) {
value = c.getString(c.getColumnIndex(column));
}
} finally {
c.close();
}
return value;
}
protected Long getStoredLongValue(Uri uri, String selection, String[] selectionArgs,
String column) {
Long value = null;
Cursor c = mResolver.query(uri, new String[] { column }, selection, selectionArgs, null);
try {
assertEquals("Record count", 1, c.getCount());
if (c.moveToFirst()) {
value = c.getLong(c.getColumnIndex(column));
}
} finally {
c.close();
}
return value;
}
protected Long getStoredLongValue(Uri uri, String column) {
return getStoredLongValue(uri, null, null, column);
}
protected void assertStoredValues(Uri rowUri, ContentValues expectedValues) {
assertStoredValues(rowUri, null, null, expectedValues);
}
protected void assertStoredValues(Uri rowUri, ContentValues... expectedValues) {
assertStoredValues(rowUri, null, null, expectedValues);
}
protected void assertStoredValues(Uri rowUri, String selection, String[] selectionArgs,
ContentValues expectedValues) {
Cursor c = mResolver.query(rowUri, null, selection, selectionArgs, null);
try {
assertEquals("Record count", 1, c.getCount());
c.moveToFirst();
assertCursorValues(c, expectedValues);
} catch (Error e) {
TestUtils.dumpCursor(c);
throw e;
} finally {
c.close();
}
}
protected void assertContainsValues(Uri rowUri, ContentValues expectedValues) {
Cursor c = mResolver.query(rowUri, null, null, null, null);
try {
assertEquals("Record count", 1, c.getCount());
c.moveToFirst();
assertCursorValuesPartialMatch(c, expectedValues);
} catch (Error e) {
TestUtils.dumpCursor(c);
throw e;
} finally {
c.close();
}
}
protected void assertStoredValuesWithProjection(Uri rowUri, ContentValues expectedValues) {
assertStoredValuesWithProjection(rowUri, new ContentValues[] {expectedValues});
}
protected void assertStoredValuesWithProjection(Uri rowUri, ContentValues... expectedValues) {
assertTrue("Need at least one ContentValues for this test", expectedValues.length > 0);
Cursor c = mResolver.query(rowUri, buildProjection(expectedValues[0]), null, null, null);
try {
assertEquals("Record count", expectedValues.length, c.getCount());
c.moveToFirst();
assertCursorValues(c, expectedValues);
} catch (Error e) {
TestUtils.dumpCursor(c);
throw e;
} finally {
c.close();
}
}
protected void assertStoredValues(
Uri rowUri, String selection, String[] selectionArgs, ContentValues... expectedValues) {
assertStoredValues(mResolver.query(rowUri, null, selection, selectionArgs, null),
expectedValues);
}
private void assertStoredValues(Cursor c, ContentValues... expectedValues) {
try {
assertEquals("Record count", expectedValues.length, c.getCount());
assertCursorValues(c, expectedValues);
} catch (Error e) {
TestUtils.dumpCursor(c);
throw e;
} finally {
c.close();
}
}
/**
* A variation of {@link #assertStoredValues}, but it queries directly to the DB.
*/
protected void assertStoredValuesDb(
String sql, String[] selectionArgs, ContentValues... expectedValues) {
SQLiteDatabase db = ((ContactsProvider2) getProvider()).getDatabaseHelper()
.getReadableDatabase();
assertStoredValues(db.rawQuery(sql, selectionArgs), expectedValues);
}
protected void assertStoredValuesOrderly(Uri rowUri, ContentValues... expectedValues) {
assertStoredValuesOrderly(rowUri, null, null, expectedValues);
}
protected void assertStoredValuesOrderly(Uri rowUri, String selection,
String[] selectionArgs, ContentValues... expectedValues) {
Cursor c = mResolver.query(rowUri, null, selection, selectionArgs, null);
try {
assertEquals("Record count", expectedValues.length, c.getCount());
assertCursorValuesOrderly(c, expectedValues);
} catch (Error e) {
TestUtils.dumpCursor(c);
throw e;
} finally {
c.close();
}
}
/**
* Constructs a selection (where clause) out of all supplied values, uses it
* to query the provider and verifies that a single row is returned and it
* has the same values as requested.
*/
protected void assertSelection(Uri uri, ContentValues values, String idColumn, long id) {
assertSelection(uri, values, idColumn, id, null);
}
public void assertSelectionWithProjection(Uri uri, ContentValues values, String idColumn,
long id) {
assertSelection(uri, values, idColumn, id, buildProjection(values));
}
private void assertSelection(Uri uri, ContentValues values, String idColumn, long id,
String[] projection) {
StringBuilder sb = new StringBuilder();
ArrayList<String> selectionArgs = new ArrayList<String>(values.size());
if (idColumn != null) {
sb.append(idColumn).append("=").append(id);
}
Set<Map.Entry<String, Object>> entries = values.valueSet();
for (Map.Entry<String, Object> entry : entries) {
String column = entry.getKey();
Object value = entry.getValue();
if (sb.length() != 0) {
sb.append(" AND ");
}
sb.append(column);
if (value == null) {
sb.append(" IS NULL");
} else {
sb.append("=?");
selectionArgs.add(String.valueOf(value));
}
}
Cursor c = mResolver.query(uri, projection, sb.toString(), selectionArgs.toArray(new String[0]),
null);
try {
assertEquals("Record count", 1, c.getCount());
c.moveToFirst();
assertCursorValues(c, values);
} catch (Error e) {
TestUtils.dumpCursor(c);
throw e;
} finally {
c.close();
}
}
protected void assertCursorValue(Cursor cursor, String column, Object expectedValue) {
String actualValue = cursor.getString(cursor.getColumnIndex(column));
assertEquals("Column " + column, String.valueOf(expectedValue),
String.valueOf(actualValue));
}
protected void assertCursorValues(Cursor cursor, ContentValues expectedValues) {
StringBuilder message = new StringBuilder();
boolean result = equalsWithExpectedValues(cursor, expectedValues, message);
assertTrue(message.toString(), result);
}
protected void assertCursorValuesPartialMatch(Cursor cursor, ContentValues expectedValues) {
StringBuilder message = new StringBuilder();
boolean result = expectedValuePartiallyMatches(cursor, expectedValues, message);
assertTrue(message.toString(), result);
}
protected void assertCursorHasAnyRecordMatch(Cursor cursor, ContentValues expectedValues) {
final StringBuilder message = new StringBuilder();
boolean found = false;
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
message.setLength(0);
final int pos = cursor.getPosition();
found = equalsWithExpectedValues(cursor, expectedValues, message);
if (found) {
break;
}
}
assertTrue("Expected values can not be found " + expectedValues + "," + message.toString(),
found);
}
protected void assertCursorValues(Cursor cursor, ContentValues... expectedValues) {
StringBuilder message = new StringBuilder();
// In case if expectedValues contains multiple identical values, remember which cursor
// rows are "consumed" to prevent multiple ContentValues from hitting the same row.
final BitSet used = new BitSet(cursor.getCount());
for (ContentValues v : expectedValues) {
boolean found = false;
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
final int pos = cursor.getPosition();
if (used.get(pos)) continue;
found = equalsWithExpectedValues(cursor, v, message);
if (found) {
used.set(pos);
break;
}
}
assertTrue("Expected values can not be found " + v + "," + message.toString(), found);
}
}
private void assertCursorValuesOrderly(Cursor cursor, ContentValues... expectedValues) {
StringBuilder message = new StringBuilder();
cursor.moveToPosition(-1);
for (ContentValues v : expectedValues) {
assertTrue(cursor.moveToNext());
boolean ok = equalsWithExpectedValues(cursor, v, message);
assertTrue("ContentValues didn't match. Pos=" + cursor.getPosition() + ", values=" +
v + message.toString(), ok);
}
}
private boolean expectedValuePartiallyMatches(Cursor cursor, ContentValues expectedValues,
StringBuilder msgBuffer) {
for (String column : expectedValues.keySet()) {
int index = cursor.getColumnIndex(column);
if (index == -1) {
msgBuffer.append(" No such column: ").append(column);
return false;
}
String expectedValue = expectedValues.getAsString(column);
String value = cursor.getString(cursor.getColumnIndex(column));
if (value != null && !value.contains(expectedValue)) {
msgBuffer.append(" Column value ").append(column).append(" expected to contain <")
.append(expectedValue).append(">, but was <").append(value).append('>');
return false;
}
}
return true;
}
private boolean equalsWithExpectedValues(Cursor cursor, ContentValues expectedValues,
StringBuilder msgBuffer) {
for (String column : expectedValues.keySet()) {
int index = cursor.getColumnIndex(column);
if (index == -1) {
msgBuffer.append(" No such column: ").append(column);
return false;
}
Object expectedValue = expectedValues.get(column);
String value;
if (expectedValue instanceof byte[]) {
expectedValue = Hex.encodeHex((byte[])expectedValue, false);
value = Hex.encodeHex(cursor.getBlob(index), false);
} else {
expectedValue = expectedValues.getAsString(column);
value = cursor.getString(cursor.getColumnIndex(column));
}
if (expectedValue != null && !expectedValue.equals(value) || value != null
&& !value.equals(expectedValue)) {
msgBuffer
.append(" Column value ")
.append(column)
.append(" expected <")
.append(expectedValue)
.append(">, but was <")
.append(value)
.append('>');
return false;
}
}
return true;
}
private static final String[] DATA_USAGE_PROJECTION =
new String[] {Data.DATA1, Data.TIMES_USED, Data.LAST_TIME_USED};
protected void assertDataUsageCursorContains(Uri uri, String data1, int timesUsed,
int lastTimeUsed) {
final Cursor cursor = mResolver.query(uri, DATA_USAGE_PROJECTION, null, null,
null);
try {
assertCursorHasAnyRecordMatch(cursor, cv(Data.DATA1, data1, Data.TIMES_USED, timesUsed,
Data.LAST_TIME_USED, lastTimeUsed));
} finally {
cursor.close();
}
}
private String[] buildProjection(ContentValues values) {
String[] projection = new String[values.size()];
Iterator<Entry<String, Object>> iter = values.valueSet().iterator();
for (int i = 0; i < projection.length; i++) {
projection[i] = iter.next().getKey();
}
return projection;
}
protected int getCount(Uri uri) {
return getCount(uri, null, null);
}
protected int getCount(Uri uri, String selection, String[] selectionArgs) {
Cursor c = mResolver.query(uri, null, selection, selectionArgs, null);
try {
return c.getCount();
} finally {
c.close();
}
}
public static void dump(ContentResolver resolver, boolean aggregatedOnly) {
String[] projection = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME
};
String selection = null;
if (aggregatedOnly) {
selection = Contacts._ID
+ " IN (SELECT contact_id" +
" FROM raw_contacts GROUP BY contact_id HAVING count(*) > 1)";
}
Cursor c = resolver.query(Contacts.CONTENT_URI, projection, selection, null,
Contacts.DISPLAY_NAME);
while(c.moveToNext()) {
long contactId = c.getLong(0);
Log.i("Contact ", String.format("%5d %s", contactId, c.getString(1)));
dumpRawContacts(resolver, contactId);
Log.i(" ", ".");
}
c.close();
}
private static void dumpRawContacts(ContentResolver resolver, long contactId) {
String[] projection = new String[] {
RawContacts._ID,
};
Cursor c = resolver.query(RawContacts.CONTENT_URI, projection, RawContacts.CONTACT_ID + "="
+ contactId, null, null);
while(c.moveToNext()) {
long rawContactId = c.getLong(0);
Log.i("RawContact", String.format(" %-5d", rawContactId));
dumpData(resolver, rawContactId);
}
c.close();
}
private static void dumpData(ContentResolver resolver, long rawContactId) {
String[] projection = new String[] {
Data.MIMETYPE,
Data.DATA1,
Data.DATA2,
Data.DATA3,
};
Cursor c = resolver.query(Data.CONTENT_URI, projection, Data.RAW_CONTACT_ID + "="
+ rawContactId, null, Data.MIMETYPE);
while(c.moveToNext()) {
String mimetype = c.getString(0);
if (Photo.CONTENT_ITEM_TYPE.equals(mimetype)) {
Log.i("Photo ", "");
} else {
mimetype = mimetype.substring(mimetype.indexOf('/') + 1);
Log.i("Data ", String.format(" %-10s %s,%s,%s", mimetype,
c.getString(1), c.getString(2), c.getString(3)));
}
}
c.close();
}
protected void assertNetworkNotified(boolean expected) {
assertEquals(expected, (getContactsProvider()).isNetworkNotified());
}
protected void assertProjection(Uri uri, String[] expectedProjection) {
Cursor cursor = mResolver.query(uri, null, "0", null, null);
String[] actualProjection = cursor.getColumnNames();
MoreAsserts.assertEquals("Incorrect projection for URI: " + uri,
Sets.newHashSet(expectedProjection), Sets.newHashSet(actualProjection));
cursor.close();
}
protected void assertRowCount(int expectedCount, Uri uri, String selection, String[] args) {
Cursor cursor = mResolver.query(uri, null, selection, args, null);
try {
assertEquals(expectedCount, cursor.getCount());
} catch (Error e) {
TestUtils.dumpCursor(cursor);
throw e;
} finally {
cursor.close();
}
}
/**
* A contact in the database, and the attributes used to create it. Construct using
* {@link GoldenContactBuilder#build()}.
*/
public final class GoldenContact {
private final long rawContactId;
private final long contactId;
private final String givenName;
private final String familyName;
private final String nickname;
private final byte[] photo;
private final String company;
private final String title;
private final String phone;
private final String email;
private GoldenContact(GoldenContactBuilder builder, long rawContactId, long contactId) {
this.rawContactId = rawContactId;
this.contactId = contactId;
givenName = builder.givenName;
familyName = builder.familyName;
nickname = builder.nickname;
photo = builder.photo;
company = builder.company;
title = builder.title;
phone = builder.phone;
email = builder.email;
}
public void delete() {
Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
mResolver.delete(rawContactUri, null, null);
}
/**
* Returns the index of the contact in table "raw_contacts"
*/
public long getRawContactId() {
return rawContactId;
}
/**
* Returns the index of the contact in table "contacts"
*/
public long getContactId() {
return contactId;
}
/**
* Returns the lookup key for the contact.
*/
public String getLookupKey() {
return queryLookupKey(contactId);
}
/**
* Returns the contact's given name.
*/
public String getGivenName() {
return givenName;
}
/**
* Returns the contact's family name.
*/
public String getFamilyName() {
return familyName;
}
/**
* Returns the contact's nickname.
*/
public String getNickname() {
return nickname;
}
/**
* Return's the contact's photo
*/
public byte[] getPhoto() {
return photo;
}
/**
* Return's the company at which the contact works.
*/
public String getCompany() {
return company;
}
/**
* Returns the contact's job title.
*/
public String getTitle() {
return title;
}
/**
* Returns the contact's phone number
*/
public String getPhone() {
return phone;
}
/**
* Returns the contact's email address
*/
public String getEmail() {
return email;
}
}
/**
* Builds {@link GoldenContact} objects. Unspecified boolean objects default to false.
* Unspecified String objects default to null.
*/
public final class GoldenContactBuilder {
private String givenName;
private String familyName;
private String nickname;
private byte[] photo;
private String company;
private String title;
private String phone;
private String email;
/**
* The contact's given and family names.
*
* TODO(dplotnikov): inline, or should we require them to set both names if they set either?
*/
public GoldenContactBuilder name(String givenName, String familyName) {
return givenName(givenName).familyName(familyName);
}
/**
* The contact's given name.
*/
public GoldenContactBuilder givenName(String value) {
givenName = value;
return this;
}
/**
* The contact's family name.
*/
public GoldenContactBuilder familyName(String value) {
familyName = value;
return this;
}
/**
* The contact's nickname.
*/
public GoldenContactBuilder nickname(String value) {
nickname = value;
return this;
}
/**
* The contact's photo.
*/
public GoldenContactBuilder photo(byte[] value) {
photo = value;
return this;
}
/**
* The company at which the contact works.
*/
public GoldenContactBuilder company(String value) {
company = value;
return this;
}
/**
* The contact's job title.
*/
public GoldenContactBuilder title(String value) {
title = value;
return this;
}
/**
* The contact's phone number.
*/
public GoldenContactBuilder phone(String value) {
phone = value;
return this;
}
/**
* The contact's email address; also sets their IM status to {@link StatusUpdates#OFFLINE}
* with a presence of "Coding for Android".
*/
public GoldenContactBuilder email(String value) {
email = value;
return this;
}
/**
* Builds the {@link GoldenContact} specified by this builder.
*/
public GoldenContact build() {
final long groupId = createGroup(mAccount, "gsid1", "title1");
long rawContactId = RawContactUtil.createRawContact(mResolver);
insertGroupMembership(rawContactId, groupId);
if (givenName != null || familyName != null) {
DataUtil.insertStructuredName(mResolver, rawContactId, givenName, familyName);
}
if (nickname != null) {
insertNickname(rawContactId, nickname);
}
if (photo != null) {
insertPhoto(rawContactId);
}
if (company != null || title != null) {
insertOrganization(rawContactId);
}
if (email != null) {
insertEmail(rawContactId);
}
if (phone != null) {
insertPhone(rawContactId);
}
long contactId = queryContactId(rawContactId);
return new GoldenContact(this, rawContactId, contactId);
}
private void insertPhoto(long rawContactId) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
values.put(Photo.PHOTO, photo);
mResolver.insert(Data.CONTENT_URI, values);
}
private void insertOrganization(long rawContactId) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
values.put(Organization.TYPE, Organization.TYPE_WORK);
if (company != null) {
values.put(Organization.COMPANY, company);
}
if (title != null) {
values.put(Organization.TITLE, title);
}
mResolver.insert(Data.CONTENT_URI, values);
}
private void insertEmail(long rawContactId) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
values.put(Email.TYPE, Email.TYPE_WORK);
values.put(Email.DATA, "foo@acme.com");
mResolver.insert(Data.CONTENT_URI, values);
int protocol = Im.PROTOCOL_GOOGLE_TALK;
values.clear();
values.put(StatusUpdates.PROTOCOL, protocol);
values.put(StatusUpdates.IM_HANDLE, email);
values.put(StatusUpdates.IM_ACCOUNT, "foo");
values.put(StatusUpdates.PRESENCE_STATUS, StatusUpdates.OFFLINE);
values.put(StatusUpdates.CHAT_CAPABILITY, StatusUpdates.CAPABILITY_HAS_CAMERA);
values.put(StatusUpdates.PRESENCE_CUSTOM_STATUS, "Coding for Android");
mResolver.insert(StatusUpdates.CONTENT_URI, values);
}
private void insertPhone(long rawContactId) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Data.IS_PRIMARY, 1);
values.put(Phone.TYPE, Phone.TYPE_HOME);
values.put(Phone.NUMBER, phone);
mResolver.insert(Data.CONTENT_URI, values);
}
}
}
|