summaryrefslogtreecommitdiffstats
path: root/java/com/android/contacts/common/model/account/BaseAccountType.java
blob: 8e9ba738cfa3461965231636dd896d8fc7306c5e (plain)
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
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
/*
 * 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.contacts.common.model.account;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.Resources;
import android.provider.ContactsContract.CommonDataKinds.BaseTypes;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
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.Relation;
import android.provider.ContactsContract.CommonDataKinds.SipAddress;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;
import android.util.ArrayMap;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import com.android.contacts.common.R;
import com.android.contacts.common.model.dataitem.DataKind;
import com.android.contacts.common.util.CommonDateUtils;
import com.android.contacts.common.util.ContactDisplayUtils;
import com.android.dialer.common.LogUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public abstract class BaseAccountType extends AccountType {

  public static final StringInflater ORGANIZATION_BODY_INFLATER =
      new StringInflater() {
        @Override
        public CharSequence inflateUsing(Context context, ContentValues values) {
          final CharSequence companyValue =
              values.containsKey(Organization.COMPANY)
                  ? values.getAsString(Organization.COMPANY)
                  : null;
          final CharSequence titleValue =
              values.containsKey(Organization.TITLE)
                  ? values.getAsString(Organization.TITLE)
                  : null;

          if (companyValue != null && titleValue != null) {
            return companyValue + ": " + titleValue;
          } else if (companyValue == null) {
            return titleValue;
          } else {
            return companyValue;
          }
        }
      };
  protected static final int FLAGS_PHONE = EditorInfo.TYPE_CLASS_PHONE;
  protected static final int FLAGS_EMAIL =
      EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
  protected static final int FLAGS_PERSON_NAME =
      EditorInfo.TYPE_CLASS_TEXT
          | EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS
          | EditorInfo.TYPE_TEXT_VARIATION_PERSON_NAME;
  protected static final int FLAGS_PHONETIC =
      EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PHONETIC;
  protected static final int FLAGS_GENERIC_NAME =
      EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS;
  protected static final int FLAGS_NOTE =
      EditorInfo.TYPE_CLASS_TEXT
          | EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES
          | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
  protected static final int FLAGS_EVENT = EditorInfo.TYPE_CLASS_TEXT;
  protected static final int FLAGS_WEBSITE =
      EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_URI;
  protected static final int FLAGS_POSTAL =
      EditorInfo.TYPE_CLASS_TEXT
          | EditorInfo.TYPE_TEXT_VARIATION_POSTAL_ADDRESS
          | EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS
          | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
  protected static final int FLAGS_SIP_ADDRESS =
      EditorInfo.TYPE_CLASS_TEXT
          | EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS; // since SIP addresses have the same
  // basic format as email addresses
  protected static final int FLAGS_RELATION =
      EditorInfo.TYPE_CLASS_TEXT
          | EditorInfo.TYPE_TEXT_FLAG_CAP_WORDS
          | EditorInfo.TYPE_TEXT_VARIATION_PERSON_NAME;

  // Specify the maximum number of lines that can be used to display various field types.  If no
  // value is specified for a particular type, we use the default value from {@link DataKind}.
  protected static final int MAX_LINES_FOR_POSTAL_ADDRESS = 10;
  protected static final int MAX_LINES_FOR_GROUP = 10;
  protected static final int MAX_LINES_FOR_NOTE = 100;

  public BaseAccountType() {
    this.accountType = null;
    this.dataSet = null;
    this.titleRes = R.string.account_phone;
    this.iconRes = R.mipmap.ic_contacts_launcher;
  }

  protected static EditType buildPhoneType(int type) {
    return new EditType(type, Phone.getTypeLabelResource(type));
  }

  protected static EditType buildEmailType(int type) {
    return new EditType(type, Email.getTypeLabelResource(type));
  }

  protected static EditType buildPostalType(int type) {
    return new EditType(type, StructuredPostal.getTypeLabelResource(type));
  }

  protected static EditType buildImType(int type) {
    return new EditType(type, Im.getProtocolLabelResource(type));
  }

  protected static EditType buildEventType(int type, boolean yearOptional) {
    return new EventEditType(type, Event.getTypeResource(type)).setYearOptional(yearOptional);
  }

  protected static EditType buildRelationType(int type) {
    return new EditType(type, Relation.getTypeLabelResource(type));
  }

  // Utility methods to keep code shorter.
  private static boolean getAttr(AttributeSet attrs, String attribute, boolean defaultValue) {
    return attrs.getAttributeBooleanValue(null, attribute, defaultValue);
  }

  private static int getAttr(AttributeSet attrs, String attribute, int defaultValue) {
    return attrs.getAttributeIntValue(null, attribute, defaultValue);
  }

  private static String getAttr(AttributeSet attrs, String attribute) {
    return attrs.getAttributeValue(null, attribute);
  }

  protected DataKind addDataKindStructuredName(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                StructuredName.CONTENT_ITEM_TYPE, R.string.nameLabelsGroup, Weight.NONE, true));
    kind.actionHeader = new SimpleInflater(R.string.nameLabelsGroup);
    kind.actionBody = new SimpleInflater(Nickname.NAME);
    kind.typeOverallMax = 1;

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(
        new EditField(StructuredName.DISPLAY_NAME, R.string.full_name, FLAGS_PERSON_NAME));
    kind.fieldList.add(
        new EditField(StructuredName.PREFIX, R.string.name_prefix, FLAGS_PERSON_NAME)
            .setLongForm(true));
    kind.fieldList.add(
        new EditField(StructuredName.FAMILY_NAME, R.string.name_family, FLAGS_PERSON_NAME)
            .setLongForm(true));
    kind.fieldList.add(
        new EditField(StructuredName.MIDDLE_NAME, R.string.name_middle, FLAGS_PERSON_NAME)
            .setLongForm(true));
    kind.fieldList.add(
        new EditField(StructuredName.GIVEN_NAME, R.string.name_given, FLAGS_PERSON_NAME)
            .setLongForm(true));
    kind.fieldList.add(
        new EditField(StructuredName.SUFFIX, R.string.name_suffix, FLAGS_PERSON_NAME)
            .setLongForm(true));
    kind.fieldList.add(
        new EditField(
            StructuredName.PHONETIC_FAMILY_NAME, R.string.name_phonetic_family, FLAGS_PHONETIC));
    kind.fieldList.add(
        new EditField(
            StructuredName.PHONETIC_MIDDLE_NAME, R.string.name_phonetic_middle, FLAGS_PHONETIC));
    kind.fieldList.add(
        new EditField(
            StructuredName.PHONETIC_GIVEN_NAME, R.string.name_phonetic_given, FLAGS_PHONETIC));

    return kind;
  }

  protected DataKind addDataKindDisplayName(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME,
                R.string.nameLabelsGroup,
                Weight.NONE,
                true));
    kind.actionHeader = new SimpleInflater(R.string.nameLabelsGroup);
    kind.actionBody = new SimpleInflater(Nickname.NAME);
    kind.typeOverallMax = 1;

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(
        new EditField(StructuredName.DISPLAY_NAME, R.string.full_name, FLAGS_PERSON_NAME)
            .setShortForm(true));

    boolean displayOrderPrimary =
        context.getResources().getBoolean(R.bool.config_editor_field_order_primary);

    if (!displayOrderPrimary) {
      kind.fieldList.add(
          new EditField(StructuredName.PREFIX, R.string.name_prefix, FLAGS_PERSON_NAME)
              .setLongForm(true));
      kind.fieldList.add(
          new EditField(StructuredName.FAMILY_NAME, R.string.name_family, FLAGS_PERSON_NAME)
              .setLongForm(true));
      kind.fieldList.add(
          new EditField(StructuredName.MIDDLE_NAME, R.string.name_middle, FLAGS_PERSON_NAME)
              .setLongForm(true));
      kind.fieldList.add(
          new EditField(StructuredName.GIVEN_NAME, R.string.name_given, FLAGS_PERSON_NAME)
              .setLongForm(true));
      kind.fieldList.add(
          new EditField(StructuredName.SUFFIX, R.string.name_suffix, FLAGS_PERSON_NAME)
              .setLongForm(true));
    } else {
      kind.fieldList.add(
          new EditField(StructuredName.PREFIX, R.string.name_prefix, FLAGS_PERSON_NAME)
              .setLongForm(true));
      kind.fieldList.add(
          new EditField(StructuredName.GIVEN_NAME, R.string.name_given, FLAGS_PERSON_NAME)
              .setLongForm(true));
      kind.fieldList.add(
          new EditField(StructuredName.MIDDLE_NAME, R.string.name_middle, FLAGS_PERSON_NAME)
              .setLongForm(true));
      kind.fieldList.add(
          new EditField(StructuredName.FAMILY_NAME, R.string.name_family, FLAGS_PERSON_NAME)
              .setLongForm(true));
      kind.fieldList.add(
          new EditField(StructuredName.SUFFIX, R.string.name_suffix, FLAGS_PERSON_NAME)
              .setLongForm(true));
    }

    return kind;
  }

  protected DataKind addDataKindPhoneticName(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME,
                R.string.name_phonetic,
                Weight.NONE,
                true));
    kind.actionHeader = new SimpleInflater(R.string.nameLabelsGroup);
    kind.actionBody = new SimpleInflater(Nickname.NAME);
    kind.typeOverallMax = 1;

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(
        new EditField(DataKind.PSEUDO_COLUMN_PHONETIC_NAME, R.string.name_phonetic, FLAGS_PHONETIC)
            .setShortForm(true));
    kind.fieldList.add(
        new EditField(
                StructuredName.PHONETIC_FAMILY_NAME, R.string.name_phonetic_family, FLAGS_PHONETIC)
            .setLongForm(true));
    kind.fieldList.add(
        new EditField(
                StructuredName.PHONETIC_MIDDLE_NAME, R.string.name_phonetic_middle, FLAGS_PHONETIC)
            .setLongForm(true));
    kind.fieldList.add(
        new EditField(
                StructuredName.PHONETIC_GIVEN_NAME, R.string.name_phonetic_given, FLAGS_PHONETIC)
            .setLongForm(true));

    return kind;
  }

  protected DataKind addDataKindNickname(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                Nickname.CONTENT_ITEM_TYPE, R.string.nicknameLabelsGroup, Weight.NICKNAME, true));
    kind.typeOverallMax = 1;
    kind.actionHeader = new SimpleInflater(R.string.nicknameLabelsGroup);
    kind.actionBody = new SimpleInflater(Nickname.NAME);
    kind.defaultValues = new ContentValues();
    kind.defaultValues.put(Nickname.TYPE, Nickname.TYPE_DEFAULT);

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(
        new EditField(Nickname.NAME, R.string.nicknameLabelsGroup, FLAGS_PERSON_NAME));

    return kind;
  }

  protected DataKind addDataKindPhone(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(Phone.CONTENT_ITEM_TYPE, R.string.phoneLabelsGroup, Weight.PHONE, true));
    kind.iconAltRes = R.drawable.quantum_ic_message_white_24;
    kind.iconAltDescriptionRes = R.string.sms;
    kind.actionHeader = new PhoneActionInflater();
    kind.actionAltHeader = new PhoneActionAltInflater();
    kind.actionBody = new SimpleInflater(Phone.NUMBER);
    kind.typeColumn = Phone.TYPE;
    kind.typeList = new ArrayList<>();
    kind.typeList.add(buildPhoneType(Phone.TYPE_MOBILE));
    kind.typeList.add(buildPhoneType(Phone.TYPE_HOME));
    kind.typeList.add(buildPhoneType(Phone.TYPE_WORK));
    kind.typeList.add(buildPhoneType(Phone.TYPE_FAX_WORK).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_FAX_HOME).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_PAGER).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_OTHER));
    kind.typeList.add(
        buildPhoneType(Phone.TYPE_CUSTOM).setSecondary(true).setCustomColumn(Phone.LABEL));
    kind.typeList.add(buildPhoneType(Phone.TYPE_CALLBACK).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_CAR).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_COMPANY_MAIN).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_ISDN).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_MAIN).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_OTHER_FAX).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_RADIO).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_TELEX).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_TTY_TDD).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_WORK_MOBILE).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_WORK_PAGER).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_ASSISTANT).setSecondary(true));
    kind.typeList.add(buildPhoneType(Phone.TYPE_MMS).setSecondary(true));

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(new EditField(Phone.NUMBER, R.string.phoneLabelsGroup, FLAGS_PHONE));

    return kind;
  }

  protected DataKind addDataKindEmail(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(Email.CONTENT_ITEM_TYPE, R.string.emailLabelsGroup, Weight.EMAIL, true));
    kind.actionHeader = new EmailActionInflater();
    kind.actionBody = new SimpleInflater(Email.DATA);
    kind.typeColumn = Email.TYPE;
    kind.typeList = new ArrayList<>();
    kind.typeList.add(buildEmailType(Email.TYPE_HOME));
    kind.typeList.add(buildEmailType(Email.TYPE_WORK));
    kind.typeList.add(buildEmailType(Email.TYPE_OTHER));
    kind.typeList.add(buildEmailType(Email.TYPE_MOBILE));
    kind.typeList.add(
        buildEmailType(Email.TYPE_CUSTOM).setSecondary(true).setCustomColumn(Email.LABEL));

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(new EditField(Email.DATA, R.string.emailLabelsGroup, FLAGS_EMAIL));

    return kind;
  }

  protected DataKind addDataKindStructuredPostal(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                StructuredPostal.CONTENT_ITEM_TYPE,
                R.string.postalLabelsGroup,
                Weight.STRUCTURED_POSTAL,
                true));
    kind.actionHeader = new PostalActionInflater();
    kind.actionBody = new SimpleInflater(StructuredPostal.FORMATTED_ADDRESS);
    kind.typeColumn = StructuredPostal.TYPE;
    kind.typeList = new ArrayList<>();
    kind.typeList.add(buildPostalType(StructuredPostal.TYPE_HOME));
    kind.typeList.add(buildPostalType(StructuredPostal.TYPE_WORK));
    kind.typeList.add(buildPostalType(StructuredPostal.TYPE_OTHER));
    kind.typeList.add(
        buildPostalType(StructuredPostal.TYPE_CUSTOM)
            .setSecondary(true)
            .setCustomColumn(StructuredPostal.LABEL));

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(
        new EditField(StructuredPostal.FORMATTED_ADDRESS, R.string.postal_address, FLAGS_POSTAL));

    kind.maxLinesForDisplay = MAX_LINES_FOR_POSTAL_ADDRESS;

    return kind;
  }

  protected DataKind addDataKindIm(Context context) throws DefinitionException {
    DataKind kind =
        addKind(new DataKind(Im.CONTENT_ITEM_TYPE, R.string.imLabelsGroup, Weight.IM, true));
    kind.actionHeader = new ImActionInflater();
    kind.actionBody = new SimpleInflater(Im.DATA);

    // NOTE: even though a traditional "type" exists, for editing
    // purposes we're using the protocol to pick labels

    kind.defaultValues = new ContentValues();
    kind.defaultValues.put(Im.TYPE, Im.TYPE_OTHER);

    kind.typeColumn = Im.PROTOCOL;
    kind.typeList = new ArrayList<>();
    kind.typeList.add(buildImType(Im.PROTOCOL_AIM));
    kind.typeList.add(buildImType(Im.PROTOCOL_MSN));
    kind.typeList.add(buildImType(Im.PROTOCOL_YAHOO));
    kind.typeList.add(buildImType(Im.PROTOCOL_SKYPE));
    kind.typeList.add(buildImType(Im.PROTOCOL_QQ));
    kind.typeList.add(buildImType(Im.PROTOCOL_GOOGLE_TALK));
    kind.typeList.add(buildImType(Im.PROTOCOL_ICQ));
    kind.typeList.add(buildImType(Im.PROTOCOL_JABBER));
    kind.typeList.add(
        buildImType(Im.PROTOCOL_CUSTOM).setSecondary(true).setCustomColumn(Im.CUSTOM_PROTOCOL));

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(new EditField(Im.DATA, R.string.imLabelsGroup, FLAGS_EMAIL));

    return kind;
  }

  protected DataKind addDataKindOrganization(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                Organization.CONTENT_ITEM_TYPE,
                R.string.organizationLabelsGroup,
                Weight.ORGANIZATION,
                true));
    kind.actionHeader = new SimpleInflater(R.string.organizationLabelsGroup);
    kind.actionBody = ORGANIZATION_BODY_INFLATER;
    kind.typeOverallMax = 1;

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(
        new EditField(Organization.COMPANY, R.string.ghostData_company, FLAGS_GENERIC_NAME));
    kind.fieldList.add(
        new EditField(Organization.TITLE, R.string.ghostData_title, FLAGS_GENERIC_NAME));

    return kind;
  }

  protected DataKind addDataKindPhoto(Context context) throws DefinitionException {
    DataKind kind = addKind(new DataKind(Photo.CONTENT_ITEM_TYPE, -1, Weight.NONE, true));
    kind.typeOverallMax = 1;
    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(new EditField(Photo.PHOTO, -1, -1));
    return kind;
  }

  protected DataKind addDataKindNote(Context context) throws DefinitionException {
    DataKind kind =
        addKind(new DataKind(Note.CONTENT_ITEM_TYPE, R.string.label_notes, Weight.NOTE, true));
    kind.typeOverallMax = 1;
    kind.actionHeader = new SimpleInflater(R.string.label_notes);
    kind.actionBody = new SimpleInflater(Note.NOTE);
    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(new EditField(Note.NOTE, R.string.label_notes, FLAGS_NOTE));

    kind.maxLinesForDisplay = MAX_LINES_FOR_NOTE;

    return kind;
  }

  protected DataKind addDataKindWebsite(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                Website.CONTENT_ITEM_TYPE, R.string.websiteLabelsGroup, Weight.WEBSITE, true));
    kind.actionHeader = new SimpleInflater(R.string.websiteLabelsGroup);
    kind.actionBody = new SimpleInflater(Website.URL);
    kind.defaultValues = new ContentValues();
    kind.defaultValues.put(Website.TYPE, Website.TYPE_OTHER);

    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(new EditField(Website.URL, R.string.websiteLabelsGroup, FLAGS_WEBSITE));

    return kind;
  }

  protected DataKind addDataKindSipAddress(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                SipAddress.CONTENT_ITEM_TYPE,
                R.string.label_sip_address,
                Weight.SIP_ADDRESS,
                true));

    kind.actionHeader = new SimpleInflater(R.string.label_sip_address);
    kind.actionBody = new SimpleInflater(SipAddress.SIP_ADDRESS);
    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(
        new EditField(SipAddress.SIP_ADDRESS, R.string.label_sip_address, FLAGS_SIP_ADDRESS));
    kind.typeOverallMax = 1;

    return kind;
  }

  protected DataKind addDataKindGroupMembership(Context context) throws DefinitionException {
    DataKind kind =
        addKind(
            new DataKind(
                GroupMembership.CONTENT_ITEM_TYPE,
                R.string.groupsLabel,
                Weight.GROUP_MEMBERSHIP,
                true));

    kind.typeOverallMax = 1;
    kind.fieldList = new ArrayList<>();
    kind.fieldList.add(new EditField(GroupMembership.GROUP_ROW_ID, -1, -1));

    kind.maxLinesForDisplay = MAX_LINES_FOR_GROUP;

    return kind;
  }

  @Override
  public boolean isGroupMembershipEditable() {
    return false;
  }

  /** Parses the content of the EditSchema tag in contacts.xml. */
  protected final void parseEditSchema(Context context, XmlPullParser parser, AttributeSet attrs)
      throws XmlPullParserException, IOException, DefinitionException {

    final int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
        && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
      final int depth = parser.getDepth();
      if (type != XmlPullParser.START_TAG || depth != outerDepth + 1) {
        continue; // Not direct child tag
      }

      final String tag = parser.getName();

      if (Tag.DATA_KIND.equals(tag)) {
        for (DataKind kind : KindParser.INSTANCE.parseDataKindTag(context, parser, attrs)) {
          addKind(kind);
        }
      } else {
        LogUtil.i("BaseAccountType.parseEditSchema", "Skipping unknown tag " + tag);
      }
    }
  }

  private interface Tag {

    String DATA_KIND = "DataKind";
    String TYPE = "Type";
  }

  private interface Attr {

    String MAX_OCCURRENCE = "maxOccurs";
    String DATE_WITH_TIME = "dateWithTime";
    String YEAR_OPTIONAL = "yearOptional";
    String KIND = "kind";
    String TYPE = "type";
  }

  protected interface Weight {

    int NONE = -1;
    int PHONE = 10;
    int EMAIL = 15;
    int STRUCTURED_POSTAL = 25;
    int NICKNAME = 111;
    int EVENT = 120;
    int ORGANIZATION = 125;
    int NOTE = 130;
    int IM = 140;
    int SIP_ADDRESS = 145;
    int GROUP_MEMBERSHIP = 150;
    int WEBSITE = 160;
    int RELATIONSHIP = 999;
  }

  /**
   * Simple inflater that assumes a string resource has a "%s" that will be filled from the given
   * column.
   */
  public static class SimpleInflater implements StringInflater {

    private final int mStringRes;
    private final String mColumnName;

    public SimpleInflater(int stringRes) {
      this(stringRes, null);
    }

    public SimpleInflater(String columnName) {
      this(-1, columnName);
    }

    public SimpleInflater(int stringRes, String columnName) {
      mStringRes = stringRes;
      mColumnName = columnName;
    }

    @Override
    public CharSequence inflateUsing(Context context, ContentValues values) {
      final boolean validColumn = values.containsKey(mColumnName);
      final boolean validString = mStringRes > 0;

      final CharSequence stringValue = validString ? context.getText(mStringRes) : null;
      final CharSequence columnValue = validColumn ? values.getAsString(mColumnName) : null;

      if (validString && validColumn) {
        return String.format(stringValue.toString(), columnValue);
      } else if (validString) {
        return stringValue;
      } else if (validColumn) {
        return columnValue;
      } else {
        return null;
      }
    }

    @Override
    public String toString() {
      return this.getClass().getSimpleName()
          + " mStringRes="
          + mStringRes
          + " mColumnName"
          + mColumnName;
    }

    public String getColumnNameForTest() {
      return mColumnName;
    }
  }

  public abstract static class CommonInflater implements StringInflater {

    protected abstract int getTypeLabelResource(Integer type);

    protected boolean isCustom(Integer type) {
      return type == BaseTypes.TYPE_CUSTOM;
    }

    protected String getTypeColumn() {
      return Phone.TYPE;
    }

    protected String getLabelColumn() {
      return Phone.LABEL;
    }

    protected CharSequence getTypeLabel(Resources res, Integer type, CharSequence label) {
      final int labelRes = getTypeLabelResource(type);
      if (type == null) {
        return res.getText(labelRes);
      } else if (isCustom(type)) {
        return res.getString(labelRes, label == null ? "" : label);
      } else {
        return res.getText(labelRes);
      }
    }

    @Override
    public CharSequence inflateUsing(Context context, ContentValues values) {
      final Integer type = values.getAsInteger(getTypeColumn());
      final String label = values.getAsString(getLabelColumn());
      return getTypeLabel(context.getResources(), type, label);
    }

    @Override
    public String toString() {
      return this.getClass().getSimpleName();
    }
  }

  public static class PhoneActionInflater extends CommonInflater {

    @Override
    protected boolean isCustom(Integer type) {
      return ContactDisplayUtils.isCustomPhoneType(type);
    }

    @Override
    protected int getTypeLabelResource(Integer type) {
      return ContactDisplayUtils.getPhoneLabelResourceId(type);
    }
  }

  public static class PhoneActionAltInflater extends CommonInflater {

    @Override
    protected boolean isCustom(Integer type) {
      return ContactDisplayUtils.isCustomPhoneType(type);
    }

    @Override
    protected int getTypeLabelResource(Integer type) {
      return ContactDisplayUtils.getSmsLabelResourceId(type);
    }
  }

  public static class EmailActionInflater extends CommonInflater {

    @Override
    protected int getTypeLabelResource(Integer type) {
      if (type == null) {
        return R.string.email;
      }
      switch (type) {
        case Email.TYPE_HOME:
          return R.string.email_home;
        case Email.TYPE_WORK:
          return R.string.email_work;
        case Email.TYPE_OTHER:
          return R.string.email_other;
        case Email.TYPE_MOBILE:
          return R.string.email_mobile;
        default:
          return R.string.email_custom;
      }
    }
  }

  public static class EventActionInflater extends CommonInflater {

    @Override
    protected int getTypeLabelResource(Integer type) {
      return Event.getTypeResource(type);
    }
  }

  public static class RelationActionInflater extends CommonInflater {

    @Override
    protected int getTypeLabelResource(Integer type) {
      return Relation.getTypeLabelResource(type == null ? Relation.TYPE_CUSTOM : type);
    }
  }

  public static class PostalActionInflater extends CommonInflater {

    @Override
    protected int getTypeLabelResource(Integer type) {
      if (type == null) {
        return R.string.map_other;
      }
      switch (type) {
        case StructuredPostal.TYPE_HOME:
          return R.string.map_home;
        case StructuredPostal.TYPE_WORK:
          return R.string.map_work;
        case StructuredPostal.TYPE_OTHER:
          return R.string.map_other;
        default:
          return R.string.map_custom;
      }
    }
  }

  public static class ImActionInflater extends CommonInflater {

    @Override
    protected String getTypeColumn() {
      return Im.PROTOCOL;
    }

    @Override
    protected String getLabelColumn() {
      return Im.CUSTOM_PROTOCOL;
    }

    @Override
    protected int getTypeLabelResource(Integer type) {
      if (type == null) {
        return R.string.chat;
      }
      switch (type) {
        case Im.PROTOCOL_AIM:
          return R.string.chat_aim;
        case Im.PROTOCOL_MSN:
          return R.string.chat_msn;
        case Im.PROTOCOL_YAHOO:
          return R.string.chat_yahoo;
        case Im.PROTOCOL_SKYPE:
          return R.string.chat_skype;
        case Im.PROTOCOL_QQ:
          return R.string.chat_qq;
        case Im.PROTOCOL_GOOGLE_TALK:
          return R.string.chat_gtalk;
        case Im.PROTOCOL_ICQ:
          return R.string.chat_icq;
        case Im.PROTOCOL_JABBER:
          return R.string.chat_jabber;
        case Im.PROTOCOL_NETMEETING:
          return R.string.chat;
        default:
          return R.string.chat;
      }
    }
  }

  // TODO Extract it to its own class, and move all KindBuilders to it as well.
  private static class KindParser {

    public static final KindParser INSTANCE = new KindParser();

    private final Map<String, KindBuilder> mBuilders = new ArrayMap<>();

    private KindParser() {
      addBuilder(new NameKindBuilder());
      addBuilder(new NicknameKindBuilder());
      addBuilder(new PhoneKindBuilder());
      addBuilder(new EmailKindBuilder());
      addBuilder(new StructuredPostalKindBuilder());
      addBuilder(new ImKindBuilder());
      addBuilder(new OrganizationKindBuilder());
      addBuilder(new PhotoKindBuilder());
      addBuilder(new NoteKindBuilder());
      addBuilder(new WebsiteKindBuilder());
      addBuilder(new SipAddressKindBuilder());
      addBuilder(new GroupMembershipKindBuilder());
      addBuilder(new EventKindBuilder());
      addBuilder(new RelationshipKindBuilder());
    }

    private void addBuilder(KindBuilder builder) {
      mBuilders.put(builder.getTagName(), builder);
    }

    /**
     * Takes a {@link XmlPullParser} at the start of a DataKind tag, parses it and returns {@link
     * DataKind}s. (Usually just one, but there are three for the "name" kind.)
     *
     * <p>This method returns a list, because we need to add 3 kinds for the name data kind.
     * (structured, display and phonetic)
     */
    public List<DataKind> parseDataKindTag(
        Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final String kind = getAttr(attrs, Attr.KIND);
      final KindBuilder builder = mBuilders.get(kind);
      if (builder != null) {
        return builder.parseDataKind(context, parser, attrs);
      } else {
        throw new DefinitionException("Undefined data kind '" + kind + "'");
      }
    }
  }

  private abstract static class KindBuilder {

    public abstract String getTagName();

    /** DataKind tag parser specific to each kind. Subclasses must implement it. */
    public abstract List<DataKind> parseDataKind(
        Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException;

    /** Creates a new {@link DataKind}, and also parses the child Type tags in the DataKind tag. */
    protected final DataKind newDataKind(
        Context context,
        XmlPullParser parser,
        AttributeSet attrs,
        boolean isPseudo,
        String mimeType,
        String typeColumn,
        int titleRes,
        int weight,
        StringInflater actionHeader,
        StringInflater actionBody)
        throws DefinitionException, XmlPullParserException, IOException {

      LogUtil.d("BaseAccountType.newDataKind", "Adding DataKind: " + mimeType);

      final DataKind kind = new DataKind(mimeType, titleRes, weight, true);
      kind.typeColumn = typeColumn;
      kind.actionHeader = actionHeader;
      kind.actionBody = actionBody;
      kind.fieldList = new ArrayList<>();

      // Get more information from the tag...
      // A pseudo data kind doesn't have corresponding tag the XML, so we skip this.
      if (!isPseudo) {
        kind.typeOverallMax = getAttr(attrs, Attr.MAX_OCCURRENCE, -1);

        // Process "Type" tags.
        // If a kind has the type column, contacts.xml must have at least one type
        // definition.  Otherwise, it mustn't have a type definition.
        if (kind.typeColumn != null) {
          // Parse and add types.
          kind.typeList = new ArrayList<>();
          parseTypes(parser, attrs, kind, true);
          if (kind.typeList.size() == 0) {
            throw new DefinitionException("Kind " + kind.mimeType + " must have at least one type");
          }
        } else {
          // Make sure it has no types.
          parseTypes(parser, attrs, kind, false /* can't have types */);
        }
      }

      return kind;
    }

    /**
     * Parses Type elements in a DataKind element, and if {@code canHaveTypes} is true adds them to
     * the given {@link DataKind}. Otherwise the {@link DataKind} can't have a type, so throws
     * {@link DefinitionException}.
     */
    private void parseTypes(
        XmlPullParser parser,
        AttributeSet attrs,
        DataKind kind,
        boolean canHaveTypes)
        throws DefinitionException, XmlPullParserException, IOException {
      final int outerDepth = parser.getDepth();
      int type;
      while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
          && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        final int depth = parser.getDepth();
        if (type != XmlPullParser.START_TAG || depth != outerDepth + 1) {
          continue; // Not direct child tag
        }

        final String tag = parser.getName();
        if (Tag.TYPE.equals(tag)) {
          if (canHaveTypes) {
            kind.typeList.add(parseTypeTag(attrs, kind));
          } else {
            throw new DefinitionException("Kind " + kind.mimeType + " can't have types");
          }
        } else {
          throw new DefinitionException("Unknown tag: " + tag);
        }
      }
    }

    /**
     * Parses a single Type element and returns an {@link EditType} built from it. Uses {@link
     * #buildEditTypeForTypeTag} defined in subclasses to actually build an {@link EditType}.
     */
    private EditType parseTypeTag(AttributeSet attrs, DataKind kind) throws DefinitionException {

      final String typeName = getAttr(attrs, Attr.TYPE);

      final EditType et = buildEditTypeForTypeTag(attrs, typeName);
      if (et == null) {
        throw new DefinitionException(
            "Undefined type '" + typeName + "' for data kind '" + kind.mimeType + "'");
      }
      et.specificMax = getAttr(attrs, Attr.MAX_OCCURRENCE, -1);

      return et;
    }

    /**
     * Returns an {@link EditType} for the given "type". Subclasses may optionally use the
     * attributes in the tag to set optional values. (e.g. "yearOptional" for the event kind)
     */
    protected EditType buildEditTypeForTypeTag(AttributeSet attrs, String type) {
      return null;
    }

    protected final void throwIfList(DataKind kind) throws DefinitionException {
      if (kind.typeOverallMax != 1) {
        throw new DefinitionException("Kind " + kind.mimeType + " must have 'overallMax=\"1\"'");
      }
    }
  }

  /** DataKind parser for Name. (structured, display, phonetic) */
  private static class NameKindBuilder extends KindBuilder {

    private static void checkAttributeTrue(boolean value, String attrName)
        throws DefinitionException {
      if (!value) {
        throw new DefinitionException(attrName + " must be true");
      }
    }

    @Override
    public String getTagName() {
      return "name";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {

      // Build 3 data kinds:
      // - StructuredName.CONTENT_ITEM_TYPE
      // - DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME
      // - DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME

      final boolean displayOrderPrimary =
          context.getResources().getBoolean(R.bool.config_editor_field_order_primary);

      final boolean supportsDisplayName = getAttr(attrs, "supportsDisplayName", false);
      final boolean supportsPrefix = getAttr(attrs, "supportsPrefix", false);
      final boolean supportsMiddleName = getAttr(attrs, "supportsMiddleName", false);
      final boolean supportsSuffix = getAttr(attrs, "supportsSuffix", false);
      final boolean supportsPhoneticFamilyName =
          getAttr(attrs, "supportsPhoneticFamilyName", false);
      final boolean supportsPhoneticMiddleName =
          getAttr(attrs, "supportsPhoneticMiddleName", false);
      final boolean supportsPhoneticGivenName = getAttr(attrs, "supportsPhoneticGivenName", false);

      // For now, every things must be supported.
      checkAttributeTrue(supportsDisplayName, "supportsDisplayName");
      checkAttributeTrue(supportsPrefix, "supportsPrefix");
      checkAttributeTrue(supportsMiddleName, "supportsMiddleName");
      checkAttributeTrue(supportsSuffix, "supportsSuffix");
      checkAttributeTrue(supportsPhoneticFamilyName, "supportsPhoneticFamilyName");
      checkAttributeTrue(supportsPhoneticMiddleName, "supportsPhoneticMiddleName");
      checkAttributeTrue(supportsPhoneticGivenName, "supportsPhoneticGivenName");

      final List<DataKind> kinds = new ArrayList<>();

      // Structured name
      final DataKind ks =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              StructuredName.CONTENT_ITEM_TYPE,
              null,
              R.string.nameLabelsGroup,
              Weight.NONE,
              new SimpleInflater(R.string.nameLabelsGroup),
              new SimpleInflater(Nickname.NAME));

      throwIfList(ks);
      kinds.add(ks);

      // Note about setLongForm/setShortForm below.
      // We need to set this only when the type supports display name. (=supportsDisplayName)
      // Otherwise (i.e. Exchange) we don't set these flags, but instead make some fields
      // "optional".

      ks.fieldList.add(
          new EditField(StructuredName.DISPLAY_NAME, R.string.full_name, FLAGS_PERSON_NAME));
      ks.fieldList.add(
          new EditField(StructuredName.PREFIX, R.string.name_prefix, FLAGS_PERSON_NAME)
              .setLongForm(true));
      ks.fieldList.add(
          new EditField(StructuredName.FAMILY_NAME, R.string.name_family, FLAGS_PERSON_NAME)
              .setLongForm(true));
      ks.fieldList.add(
          new EditField(StructuredName.MIDDLE_NAME, R.string.name_middle, FLAGS_PERSON_NAME)
              .setLongForm(true));
      ks.fieldList.add(
          new EditField(StructuredName.GIVEN_NAME, R.string.name_given, FLAGS_PERSON_NAME)
              .setLongForm(true));
      ks.fieldList.add(
          new EditField(StructuredName.SUFFIX, R.string.name_suffix, FLAGS_PERSON_NAME)
              .setLongForm(true));
      ks.fieldList.add(
          new EditField(
              StructuredName.PHONETIC_FAMILY_NAME, R.string.name_phonetic_family, FLAGS_PHONETIC));
      ks.fieldList.add(
          new EditField(
              StructuredName.PHONETIC_MIDDLE_NAME, R.string.name_phonetic_middle, FLAGS_PHONETIC));
      ks.fieldList.add(
          new EditField(
              StructuredName.PHONETIC_GIVEN_NAME, R.string.name_phonetic_given, FLAGS_PHONETIC));

      // Display name
      final DataKind kd =
          newDataKind(
              context,
              parser,
              attrs,
              true,
              DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME,
              null,
              R.string.nameLabelsGroup,
              Weight.NONE,
              new SimpleInflater(R.string.nameLabelsGroup),
              new SimpleInflater(Nickname.NAME));
      kd.typeOverallMax = 1;
      kinds.add(kd);

      kd.fieldList.add(
          new EditField(StructuredName.DISPLAY_NAME, R.string.full_name, FLAGS_PERSON_NAME)
              .setShortForm(true));

      if (!displayOrderPrimary) {
        kd.fieldList.add(
            new EditField(StructuredName.PREFIX, R.string.name_prefix, FLAGS_PERSON_NAME)
                .setLongForm(true));
        kd.fieldList.add(
            new EditField(StructuredName.FAMILY_NAME, R.string.name_family, FLAGS_PERSON_NAME)
                .setLongForm(true));
        kd.fieldList.add(
            new EditField(StructuredName.MIDDLE_NAME, R.string.name_middle, FLAGS_PERSON_NAME)
                .setLongForm(true));
        kd.fieldList.add(
            new EditField(StructuredName.GIVEN_NAME, R.string.name_given, FLAGS_PERSON_NAME)
                .setLongForm(true));
        kd.fieldList.add(
            new EditField(StructuredName.SUFFIX, R.string.name_suffix, FLAGS_PERSON_NAME)
                .setLongForm(true));
      } else {
        kd.fieldList.add(
            new EditField(StructuredName.PREFIX, R.string.name_prefix, FLAGS_PERSON_NAME)
                .setLongForm(true));
        kd.fieldList.add(
            new EditField(StructuredName.GIVEN_NAME, R.string.name_given, FLAGS_PERSON_NAME)
                .setLongForm(true));
        kd.fieldList.add(
            new EditField(StructuredName.MIDDLE_NAME, R.string.name_middle, FLAGS_PERSON_NAME)
                .setLongForm(true));
        kd.fieldList.add(
            new EditField(StructuredName.FAMILY_NAME, R.string.name_family, FLAGS_PERSON_NAME)
                .setLongForm(true));
        kd.fieldList.add(
            new EditField(StructuredName.SUFFIX, R.string.name_suffix, FLAGS_PERSON_NAME)
                .setLongForm(true));
      }

      // Phonetic name
      final DataKind kp =
          newDataKind(
              context,
              parser,
              attrs,
              true,
              DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME,
              null,
              R.string.name_phonetic,
              Weight.NONE,
              new SimpleInflater(R.string.nameLabelsGroup),
              new SimpleInflater(Nickname.NAME));
      kp.typeOverallMax = 1;
      kinds.add(kp);

      // We may want to change the order depending on displayOrderPrimary too.
      kp.fieldList.add(
          new EditField(
                  DataKind.PSEUDO_COLUMN_PHONETIC_NAME, R.string.name_phonetic, FLAGS_PHONETIC)
              .setShortForm(true));
      kp.fieldList.add(
          new EditField(
                  StructuredName.PHONETIC_FAMILY_NAME,
                  R.string.name_phonetic_family,
                  FLAGS_PHONETIC)
              .setLongForm(true));
      kp.fieldList.add(
          new EditField(
                  StructuredName.PHONETIC_MIDDLE_NAME,
                  R.string.name_phonetic_middle,
                  FLAGS_PHONETIC)
              .setLongForm(true));
      kp.fieldList.add(
          new EditField(
                  StructuredName.PHONETIC_GIVEN_NAME, R.string.name_phonetic_given, FLAGS_PHONETIC)
              .setLongForm(true));
      return kinds;
    }
  }

  private static class NicknameKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "nickname";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Nickname.CONTENT_ITEM_TYPE,
              null,
              R.string.nicknameLabelsGroup,
              Weight.NICKNAME,
              new SimpleInflater(R.string.nicknameLabelsGroup),
              new SimpleInflater(Nickname.NAME));

      kind.fieldList.add(
          new EditField(Nickname.NAME, R.string.nicknameLabelsGroup, FLAGS_PERSON_NAME));

      kind.defaultValues = new ContentValues();
      kind.defaultValues.put(Nickname.TYPE, Nickname.TYPE_DEFAULT);

      throwIfList(kind);
      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }
  }

  private static class PhoneKindBuilder extends KindBuilder {

    /** Just to avoid line-wrapping... */
    protected static EditType build(int type, boolean secondary) {
      return new EditType(type, Phone.getTypeLabelResource(type)).setSecondary(secondary);
    }

    @Override
    public String getTagName() {
      return "phone";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Phone.CONTENT_ITEM_TYPE,
              Phone.TYPE,
              R.string.phoneLabelsGroup,
              Weight.PHONE,
              new PhoneActionInflater(),
              new SimpleInflater(Phone.NUMBER));

      kind.iconAltRes = R.drawable.quantum_ic_message_white_24;
      kind.iconAltDescriptionRes = R.string.sms;
      kind.actionAltHeader = new PhoneActionAltInflater();

      kind.fieldList.add(new EditField(Phone.NUMBER, R.string.phoneLabelsGroup, FLAGS_PHONE));

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }

    @Override
    protected EditType buildEditTypeForTypeTag(AttributeSet attrs, String type) {
      if ("home".equals(type)) {
        return build(Phone.TYPE_HOME, false);
      }
      if ("mobile".equals(type)) {
        return build(Phone.TYPE_MOBILE, false);
      }
      if ("work".equals(type)) {
        return build(Phone.TYPE_WORK, false);
      }
      if ("fax_work".equals(type)) {
        return build(Phone.TYPE_FAX_WORK, true);
      }
      if ("fax_home".equals(type)) {
        return build(Phone.TYPE_FAX_HOME, true);
      }
      if ("pager".equals(type)) {
        return build(Phone.TYPE_PAGER, true);
      }
      if ("other".equals(type)) {
        return build(Phone.TYPE_OTHER, false);
      }
      if ("callback".equals(type)) {
        return build(Phone.TYPE_CALLBACK, true);
      }
      if ("car".equals(type)) {
        return build(Phone.TYPE_CAR, true);
      }
      if ("company_main".equals(type)) {
        return build(Phone.TYPE_COMPANY_MAIN, true);
      }
      if ("isdn".equals(type)) {
        return build(Phone.TYPE_ISDN, true);
      }
      if ("main".equals(type)) {
        return build(Phone.TYPE_MAIN, true);
      }
      if ("other_fax".equals(type)) {
        return build(Phone.TYPE_OTHER_FAX, true);
      }
      if ("radio".equals(type)) {
        return build(Phone.TYPE_RADIO, true);
      }
      if ("telex".equals(type)) {
        return build(Phone.TYPE_TELEX, true);
      }
      if ("tty_tdd".equals(type)) {
        return build(Phone.TYPE_TTY_TDD, true);
      }
      if ("work_mobile".equals(type)) {
        return build(Phone.TYPE_WORK_MOBILE, true);
      }
      if ("work_pager".equals(type)) {
        return build(Phone.TYPE_WORK_PAGER, true);
      }

      // Note "assistant" used to be a custom column for the fallback type, but not anymore.
      if ("assistant".equals(type)) {
        return build(Phone.TYPE_ASSISTANT, true);
      }
      if ("mms".equals(type)) {
        return build(Phone.TYPE_MMS, true);
      }
      if ("custom".equals(type)) {
        return build(Phone.TYPE_CUSTOM, true).setCustomColumn(Phone.LABEL);
      }
      return null;
    }
  }

  private static class EmailKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "email";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Email.CONTENT_ITEM_TYPE,
              Email.TYPE,
              R.string.emailLabelsGroup,
              Weight.EMAIL,
              new EmailActionInflater(),
              new SimpleInflater(Email.DATA));
      kind.fieldList.add(new EditField(Email.DATA, R.string.emailLabelsGroup, FLAGS_EMAIL));

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }

    @Override
    protected EditType buildEditTypeForTypeTag(AttributeSet attrs, String type) {
      // EditType is mutable, so we need to create a new instance every time.
      if ("home".equals(type)) {
        return buildEmailType(Email.TYPE_HOME);
      }
      if ("work".equals(type)) {
        return buildEmailType(Email.TYPE_WORK);
      }
      if ("other".equals(type)) {
        return buildEmailType(Email.TYPE_OTHER);
      }
      if ("mobile".equals(type)) {
        return buildEmailType(Email.TYPE_MOBILE);
      }
      if ("custom".equals(type)) {
        return buildEmailType(Email.TYPE_CUSTOM).setSecondary(true).setCustomColumn(Email.LABEL);
      }
      return null;
    }
  }

  private static class StructuredPostalKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "postal";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              StructuredPostal.CONTENT_ITEM_TYPE,
              StructuredPostal.TYPE,
              R.string.postalLabelsGroup,
              Weight.STRUCTURED_POSTAL,
              new PostalActionInflater(),
              new SimpleInflater(StructuredPostal.FORMATTED_ADDRESS));

      if (getAttr(attrs, "needsStructured", false)) {
        if (Locale.JAPANESE.getLanguage().equals(Locale.getDefault().getLanguage())) {
          // Japanese order
          kind.fieldList.add(
              new EditField(StructuredPostal.COUNTRY, R.string.postal_country, FLAGS_POSTAL)
                  .setOptional(true));
          kind.fieldList.add(
              new EditField(StructuredPostal.POSTCODE, R.string.postal_postcode, FLAGS_POSTAL));
          kind.fieldList.add(
              new EditField(StructuredPostal.REGION, R.string.postal_region, FLAGS_POSTAL));
          kind.fieldList.add(
              new EditField(StructuredPostal.CITY, R.string.postal_city, FLAGS_POSTAL));
          kind.fieldList.add(
              new EditField(StructuredPostal.STREET, R.string.postal_street, FLAGS_POSTAL));
        } else {
          // Generic order
          kind.fieldList.add(
              new EditField(StructuredPostal.STREET, R.string.postal_street, FLAGS_POSTAL));
          kind.fieldList.add(
              new EditField(StructuredPostal.CITY, R.string.postal_city, FLAGS_POSTAL));
          kind.fieldList.add(
              new EditField(StructuredPostal.REGION, R.string.postal_region, FLAGS_POSTAL));
          kind.fieldList.add(
              new EditField(StructuredPostal.POSTCODE, R.string.postal_postcode, FLAGS_POSTAL));
          kind.fieldList.add(
              new EditField(StructuredPostal.COUNTRY, R.string.postal_country, FLAGS_POSTAL)
                  .setOptional(true));
        }
      } else {
        kind.maxLinesForDisplay = MAX_LINES_FOR_POSTAL_ADDRESS;
        kind.fieldList.add(
            new EditField(
                StructuredPostal.FORMATTED_ADDRESS, R.string.postal_address, FLAGS_POSTAL));
      }

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }

    @Override
    protected EditType buildEditTypeForTypeTag(AttributeSet attrs, String type) {
      // EditType is mutable, so we need to create a new instance every time.
      if ("home".equals(type)) {
        return buildPostalType(StructuredPostal.TYPE_HOME);
      }
      if ("work".equals(type)) {
        return buildPostalType(StructuredPostal.TYPE_WORK);
      }
      if ("other".equals(type)) {
        return buildPostalType(StructuredPostal.TYPE_OTHER);
      }
      if ("custom".equals(type)) {
        return buildPostalType(StructuredPostal.TYPE_CUSTOM)
            .setSecondary(true)
            .setCustomColumn(Email.LABEL);
      }
      return null;
    }
  }

  private static class ImKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "im";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {

      // IM is special:
      // - It uses "protocol" as the custom label field
      // - Its TYPE is fixed to TYPE_OTHER

      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Im.CONTENT_ITEM_TYPE,
              Im.PROTOCOL,
              R.string.imLabelsGroup,
              Weight.IM,
              new ImActionInflater(),
              new SimpleInflater(Im.DATA) // header / action
              );
      kind.fieldList.add(new EditField(Im.DATA, R.string.imLabelsGroup, FLAGS_EMAIL));

      kind.defaultValues = new ContentValues();
      kind.defaultValues.put(Im.TYPE, Im.TYPE_OTHER);

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }

    @Override
    protected EditType buildEditTypeForTypeTag(AttributeSet attrs, String type) {
      if ("aim".equals(type)) {
        return buildImType(Im.PROTOCOL_AIM);
      }
      if ("msn".equals(type)) {
        return buildImType(Im.PROTOCOL_MSN);
      }
      if ("yahoo".equals(type)) {
        return buildImType(Im.PROTOCOL_YAHOO);
      }
      if ("skype".equals(type)) {
        return buildImType(Im.PROTOCOL_SKYPE);
      }
      if ("qq".equals(type)) {
        return buildImType(Im.PROTOCOL_QQ);
      }
      if ("google_talk".equals(type)) {
        return buildImType(Im.PROTOCOL_GOOGLE_TALK);
      }
      if ("icq".equals(type)) {
        return buildImType(Im.PROTOCOL_ICQ);
      }
      if ("jabber".equals(type)) {
        return buildImType(Im.PROTOCOL_JABBER);
      }
      if ("custom".equals(type)) {
        return buildImType(Im.PROTOCOL_CUSTOM)
            .setSecondary(true)
            .setCustomColumn(Im.CUSTOM_PROTOCOL);
      }
      return null;
    }
  }

  private static class OrganizationKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "organization";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Organization.CONTENT_ITEM_TYPE,
              null,
              R.string.organizationLabelsGroup,
              Weight.ORGANIZATION,
              new SimpleInflater(R.string.organizationLabelsGroup),
              ORGANIZATION_BODY_INFLATER);

      kind.fieldList.add(
          new EditField(Organization.COMPANY, R.string.ghostData_company, FLAGS_GENERIC_NAME));
      kind.fieldList.add(
          new EditField(Organization.TITLE, R.string.ghostData_title, FLAGS_GENERIC_NAME));

      throwIfList(kind);

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }
  }

  private static class PhotoKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "photo";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Photo.CONTENT_ITEM_TYPE,
              null /* no type */,
              Weight.NONE,
              -1,
              null,
              null // no header, no body
              );

      kind.fieldList.add(new EditField(Photo.PHOTO, -1, -1));

      throwIfList(kind);

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }
  }

  private static class NoteKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "note";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Note.CONTENT_ITEM_TYPE,
              null,
              R.string.label_notes,
              Weight.NOTE,
              new SimpleInflater(R.string.label_notes),
              new SimpleInflater(Note.NOTE));

      kind.fieldList.add(new EditField(Note.NOTE, R.string.label_notes, FLAGS_NOTE));
      kind.maxLinesForDisplay = MAX_LINES_FOR_NOTE;

      throwIfList(kind);

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }
  }

  private static class WebsiteKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "website";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Website.CONTENT_ITEM_TYPE,
              null,
              R.string.websiteLabelsGroup,
              Weight.WEBSITE,
              new SimpleInflater(R.string.websiteLabelsGroup),
              new SimpleInflater(Website.URL));

      kind.fieldList.add(new EditField(Website.URL, R.string.websiteLabelsGroup, FLAGS_WEBSITE));

      kind.defaultValues = new ContentValues();
      kind.defaultValues.put(Website.TYPE, Website.TYPE_OTHER);

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }
  }

  private static class SipAddressKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "sip_address";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              SipAddress.CONTENT_ITEM_TYPE,
              null,
              R.string.label_sip_address,
              Weight.SIP_ADDRESS,
              new SimpleInflater(R.string.label_sip_address),
              new SimpleInflater(SipAddress.SIP_ADDRESS));

      kind.fieldList.add(
          new EditField(SipAddress.SIP_ADDRESS, R.string.label_sip_address, FLAGS_SIP_ADDRESS));

      throwIfList(kind);

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }
  }

  private static class GroupMembershipKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "group_membership";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              GroupMembership.CONTENT_ITEM_TYPE,
              null,
              R.string.groupsLabel,
              Weight.GROUP_MEMBERSHIP,
              null,
              null);

      kind.fieldList.add(new EditField(GroupMembership.GROUP_ROW_ID, -1, -1));
      kind.maxLinesForDisplay = MAX_LINES_FOR_GROUP;

      throwIfList(kind);

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }
  }

  /**
   * Event DataKind parser.
   *
   * <p>Event DataKind is used only for Google/Exchange types, so this parser is not used for now.
   */
  private static class EventKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "event";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Event.CONTENT_ITEM_TYPE,
              Event.TYPE,
              R.string.eventLabelsGroup,
              Weight.EVENT,
              new EventActionInflater(),
              new SimpleInflater(Event.START_DATE));

      kind.fieldList.add(new EditField(Event.DATA, R.string.eventLabelsGroup, FLAGS_EVENT));

      if (getAttr(attrs, Attr.DATE_WITH_TIME, false)) {
        kind.dateFormatWithoutYear = CommonDateUtils.NO_YEAR_DATE_AND_TIME_FORMAT;
        kind.dateFormatWithYear = CommonDateUtils.DATE_AND_TIME_FORMAT;
      } else {
        kind.dateFormatWithoutYear = CommonDateUtils.NO_YEAR_DATE_FORMAT;
        kind.dateFormatWithYear = CommonDateUtils.FULL_DATE_FORMAT;
      }

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }

    @Override
    protected EditType buildEditTypeForTypeTag(AttributeSet attrs, String type) {
      final boolean yo = getAttr(attrs, Attr.YEAR_OPTIONAL, false);

      if ("birthday".equals(type)) {
        return buildEventType(Event.TYPE_BIRTHDAY, yo).setSpecificMax(1);
      }
      if ("anniversary".equals(type)) {
        return buildEventType(Event.TYPE_ANNIVERSARY, yo);
      }
      if ("other".equals(type)) {
        return buildEventType(Event.TYPE_OTHER, yo);
      }
      if ("custom".equals(type)) {
        return buildEventType(Event.TYPE_CUSTOM, yo)
            .setSecondary(true)
            .setCustomColumn(Event.LABEL);
      }
      return null;
    }
  }

  /**
   * Relationship DataKind parser.
   *
   * <p>Relationship DataKind is used only for Google/Exchange types, so this parser is not used for
   * now.
   */
  private static class RelationshipKindBuilder extends KindBuilder {

    @Override
    public String getTagName() {
      return "relationship";
    }

    @Override
    public List<DataKind> parseDataKind(Context context, XmlPullParser parser, AttributeSet attrs)
        throws DefinitionException, XmlPullParserException, IOException {
      final DataKind kind =
          newDataKind(
              context,
              parser,
              attrs,
              false,
              Relation.CONTENT_ITEM_TYPE,
              Relation.TYPE,
              R.string.relationLabelsGroup,
              Weight.RELATIONSHIP,
              new RelationActionInflater(),
              new SimpleInflater(Relation.NAME));

      kind.fieldList.add(
          new EditField(Relation.DATA, R.string.relationLabelsGroup, FLAGS_RELATION));

      kind.defaultValues = new ContentValues();
      kind.defaultValues.put(Relation.TYPE, Relation.TYPE_SPOUSE);

      List<DataKind> result = new ArrayList<>();
      result.add(kind);
      return result;
    }

    @Override
    protected EditType buildEditTypeForTypeTag(AttributeSet attrs, String type) {
      // EditType is mutable, so we need to create a new instance every time.
      if ("assistant".equals(type)) {
        return buildRelationType(Relation.TYPE_ASSISTANT);
      }
      if ("brother".equals(type)) {
        return buildRelationType(Relation.TYPE_BROTHER);
      }
      if ("child".equals(type)) {
        return buildRelationType(Relation.TYPE_CHILD);
      }
      if ("domestic_partner".equals(type)) {
        return buildRelationType(Relation.TYPE_DOMESTIC_PARTNER);
      }
      if ("father".equals(type)) {
        return buildRelationType(Relation.TYPE_FATHER);
      }
      if ("friend".equals(type)) {
        return buildRelationType(Relation.TYPE_FRIEND);
      }
      if ("manager".equals(type)) {
        return buildRelationType(Relation.TYPE_MANAGER);
      }
      if ("mother".equals(type)) {
        return buildRelationType(Relation.TYPE_MOTHER);
      }
      if ("parent".equals(type)) {
        return buildRelationType(Relation.TYPE_PARENT);
      }
      if ("partner".equals(type)) {
        return buildRelationType(Relation.TYPE_PARTNER);
      }
      if ("referred_by".equals(type)) {
        return buildRelationType(Relation.TYPE_REFERRED_BY);
      }
      if ("relative".equals(type)) {
        return buildRelationType(Relation.TYPE_RELATIVE);
      }
      if ("sister".equals(type)) {
        return buildRelationType(Relation.TYPE_SISTER);
      }
      if ("spouse".equals(type)) {
        return buildRelationType(Relation.TYPE_SPOUSE);
      }
      if ("custom".equals(type)) {
        return buildRelationType(Relation.TYPE_CUSTOM)
            .setSecondary(true)
            .setCustomColumn(Relation.LABEL);
      }
      return null;
    }
  }
}