• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package com.android.contacts.model.account;
17 
18 import android.accounts.AuthenticatorDescription;
19 import android.content.Context;
20 import android.graphics.PorterDuff;
21 import android.graphics.drawable.Drawable;
22 import android.provider.ContactsContract.CommonDataKinds.Nickname;
23 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
24 
25 import androidx.core.content.ContextCompat;
26 import androidx.core.content.res.ResourcesCompat;
27 
28 import com.android.contacts.R;
29 import com.android.contacts.model.dataitem.DataKind;
30 
31 import com.google.common.collect.Lists;
32 
33 import java.util.Collections;
34 
35 /**
36  * Account type for SIM card contacts
37  */
38 public class SimAccountType extends BaseAccountType {
39 
SimAccountType(Context context)40     public SimAccountType(Context context) {
41         this.titleRes = R.string.account_sim;
42         this.iconRes = R.drawable.quantum_ic_sim_card_vd_theme_24;
43 
44         try {
45             addDataKindStructuredName(context);
46             addDataKindName(context);
47             final DataKind phoneKind = addDataKindPhone(context);
48             phoneKind.typeOverallMax = 1;
49             // SIM card contacts don't necessarily support separate types (based on data exposed
50             // in Samsung and LG Contacts Apps.
51             phoneKind.typeList = Collections.emptyList();
52 
53             mIsInitialized = true;
54         } catch (DefinitionException e) {
55             // Just fail fast. Because we're explicitly adding the fields in this class this
56             // exception should only happen in case of a bug.
57             throw new IllegalStateException(e);
58         }
59     }
60 
61     @Override
areContactsWritable()62     public boolean areContactsWritable() {
63         return false;
64     }
65 
66     @Override
isGroupMembershipEditable()67     public boolean isGroupMembershipEditable() {
68         return false;
69     }
70 
71     @Override
initializeFieldsFromAuthenticator(AuthenticatorDescription authenticator)72     public void initializeFieldsFromAuthenticator(AuthenticatorDescription authenticator) {
73         // Do nothing. We want to use our local icon and title
74     }
75 
76     @Override
addDataKindStructuredName(Context context)77     protected DataKind addDataKindStructuredName(Context context) throws DefinitionException {
78         final DataKind kind = addKind(new DataKind(StructuredName.CONTENT_ITEM_TYPE,
79                 R.string.nameLabelsGroup, Weight.NONE, true));
80         kind.actionHeader = new SimpleInflater(R.string.nameLabelsGroup);
81         kind.actionBody = new SimpleInflater(Nickname.NAME);
82         kind.typeOverallMax = 1;
83 
84 
85         kind.fieldList = Lists.newArrayList();
86         kind.fieldList.add(new EditField(StructuredName.GIVEN_NAME, R.string.name_given,
87                 FLAGS_PERSON_NAME));
88         kind.fieldList.add(new EditField(StructuredName.FAMILY_NAME, R.string.name_family,
89                 FLAGS_PERSON_NAME));
90 
91         return kind;
92     }
93 
94     @Override
addDataKindName(Context context)95     protected DataKind addDataKindName(Context context) throws DefinitionException {
96         final DataKind kind = addKind(new DataKind(DataKind.PSEUDO_MIME_TYPE_NAME,
97                 R.string.nameLabelsGroup, Weight.NONE, true));
98         kind.actionHeader = new SimpleInflater(R.string.nameLabelsGroup);
99         kind.actionBody = new SimpleInflater(Nickname.NAME);
100         kind.typeOverallMax = 1;
101 
102         final boolean displayOrderPrimary =
103                 context.getResources().getBoolean(R.bool.config_editor_field_order_primary);
104 
105         kind.fieldList = Lists.newArrayList();
106         if (!displayOrderPrimary) {
107             kind.fieldList.add(new EditField(StructuredName.FAMILY_NAME, R.string.name_family,
108                     FLAGS_PERSON_NAME));
109             kind.fieldList.add(new EditField(StructuredName.GIVEN_NAME, R.string.name_given,
110                     FLAGS_PERSON_NAME));
111         } else {
112             kind.fieldList.add(new EditField(StructuredName.GIVEN_NAME, R.string.name_given,
113                     FLAGS_PERSON_NAME));
114             kind.fieldList.add(new EditField(StructuredName.FAMILY_NAME, R.string.name_family,
115                     FLAGS_PERSON_NAME));
116         }
117 
118         return kind;
119     }
120 
121     @Override
getDisplayIcon(Context context)122     public Drawable getDisplayIcon(Context context) {
123         final Drawable icon = ResourcesCompat.getDrawable(context.getResources(), iconRes, null);
124         icon.mutate().setColorFilter(ContextCompat.getColor(context,
125                 R.color.actionbar_icon_color_grey), PorterDuff.Mode.SRC_ATOP);
126         return icon;
127     }
128 
129     @Override
wrapAccount(Context context, AccountWithDataSet account)130     public AccountInfo wrapAccount(Context context, AccountWithDataSet account) {
131         // Use the "SIM" type label for the name as well because on OEM phones the "name" is
132         // not always user-friendly
133         return new AccountInfo(
134                 new AccountDisplayInfo(account, getDisplayLabel(context), getDisplayLabel(context),
135                         getDisplayIcon(context), true), this);
136     }
137 }
138