SimAccountType.java revision 3f6a2444e0134b7380cdb2e13abf4bf1163336d0
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.contacts.model.account;
17
18import android.accounts.AuthenticatorDescription;
19import android.content.Context;
20import android.provider.ContactsContract.CommonDataKinds.Nickname;
21import android.provider.ContactsContract.CommonDataKinds.StructuredName;
22
23import com.android.contacts.R;
24import com.android.contacts.model.dataitem.DataKind;
25
26import com.google.common.collect.Lists;
27
28import java.util.Collections;
29
30/**
31 * Account type for SIM card contacts
32 */
33public class SimAccountType extends BaseAccountType {
34
35    public SimAccountType(Context context) {
36        this.titleRes = R.string.account_sim;
37        this.iconRes = R.drawable.ic_sim_card_tinted_24dp;
38
39        try {
40            addDataKindStructuredName(context);
41            addDataKindName(context);
42            final DataKind phoneKind = addDataKindPhone(context);
43            phoneKind.typeOverallMax = 1;
44            // SIM card contacts don't necessarily support separate types (based on data exposed
45            // in Samsung and LG Contacts Apps.
46            phoneKind.typeList = Collections.emptyList();
47
48            mIsInitialized = true;
49        } catch (DefinitionException e) {
50            // Just fail fast. Because we're explicitly adding the fields in this class this
51            // exception should only happen in case of a bug.
52            throw new IllegalStateException(e);
53        }
54    }
55
56    @Override
57    public boolean areContactsWritable() {
58        return true;
59    }
60
61    @Override
62    public boolean isGroupMembershipEditable() {
63        return false;
64    }
65
66    @Override
67    public void initializeFieldsFromAuthenticator(AuthenticatorDescription authenticator) {
68        // Do nothing. We want to use our local icon and title
69    }
70
71    @Override
72    protected DataKind addDataKindStructuredName(Context context) throws DefinitionException {
73        final DataKind kind = addKind(new DataKind(StructuredName.CONTENT_ITEM_TYPE,
74                R.string.nameLabelsGroup, Weight.NONE, true));
75        kind.actionHeader = new SimpleInflater(R.string.nameLabelsGroup);
76        kind.actionBody = new SimpleInflater(Nickname.NAME);
77        kind.typeOverallMax = 1;
78
79
80        kind.fieldList = Lists.newArrayList();
81        kind.fieldList.add(new EditField(StructuredName.GIVEN_NAME, R.string.name_given,
82                FLAGS_PERSON_NAME));
83        kind.fieldList.add(new EditField(StructuredName.FAMILY_NAME, R.string.name_family,
84                FLAGS_PERSON_NAME));
85
86        return kind;
87    }
88
89    @Override
90    protected DataKind addDataKindName(Context context) throws DefinitionException {
91        final DataKind kind = addKind(new DataKind(DataKind.PSEUDO_MIME_TYPE_NAME,
92                R.string.nameLabelsGroup, Weight.NONE, true));
93        kind.actionHeader = new SimpleInflater(R.string.nameLabelsGroup);
94        kind.actionBody = new SimpleInflater(Nickname.NAME);
95        kind.typeOverallMax = 1;
96
97        final boolean displayOrderPrimary =
98                context.getResources().getBoolean(R.bool.config_editor_field_order_primary);
99
100        kind.fieldList = Lists.newArrayList();
101        if (!displayOrderPrimary) {
102            kind.fieldList.add(new EditField(StructuredName.FAMILY_NAME, R.string.name_family,
103                    FLAGS_PERSON_NAME));
104            kind.fieldList.add(new EditField(StructuredName.GIVEN_NAME, R.string.name_given,
105                    FLAGS_PERSON_NAME));
106        } else {
107            kind.fieldList.add(new EditField(StructuredName.GIVEN_NAME, R.string.name_given,
108                    FLAGS_PERSON_NAME));
109            kind.fieldList.add(new EditField(StructuredName.FAMILY_NAME, R.string.name_family,
110                    FLAGS_PERSON_NAME));
111        }
112
113        return kind;
114    }
115}
116