Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 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 | */ |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 16 | package com.android.contacts.model.account; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 17 | |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 18 | import android.accounts.AuthenticatorDescription; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Gary Mai | 698cee7 | 2016-09-19 16:09:54 -0700 | [diff] [blame] | 20 | import android.provider.ContactsContract.CommonDataKinds.Nickname; |
| 21 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 22 | |
Arthur Wang | 3f6a244 | 2016-12-05 14:51:59 -0800 | [diff] [blame] | 23 | import com.android.contacts.R; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 24 | import com.android.contacts.model.dataitem.DataKind; |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 25 | |
Gary Mai | 698cee7 | 2016-09-19 16:09:54 -0700 | [diff] [blame] | 26 | import com.google.common.collect.Lists; |
| 27 | |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 28 | import java.util.Collections; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Account type for SIM card contacts |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 32 | */ |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 33 | public class SimAccountType extends BaseAccountType { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 34 | |
| 35 | public SimAccountType(Context context) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 36 | this.titleRes = R.string.account_sim; |
John Shao | bd9ef3c | 2016-12-15 12:42:03 -0800 | [diff] [blame] | 37 | this.iconRes = R.drawable.quantum_ic_sim_card_vd_theme_24; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 38 | |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 39 | try { |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 40 | addDataKindStructuredName(context); |
Gary Mai | 7a6daea | 2016-10-10 15:41:48 -0700 | [diff] [blame] | 41 | addDataKindName(context); |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 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; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean isGroupMembershipEditable() { |
| 63 | return false; |
| 64 | } |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 65 | |
| 66 | @Override |
| 67 | public void initializeFieldsFromAuthenticator(AuthenticatorDescription authenticator) { |
| 68 | // Do nothing. We want to use our local icon and title |
| 69 | } |
Gary Mai | 698cee7 | 2016-09-19 16:09:54 -0700 | [diff] [blame] | 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 | |
Gary Mai | 7a6daea | 2016-10-10 15:41:48 -0700 | [diff] [blame] | 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 | |
Gary Mai | 698cee7 | 2016-09-19 16:09:54 -0700 | [diff] [blame] | 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 | } |
Marcus Hagerott | 75895e7 | 2016-12-12 17:21:57 -0800 | [diff] [blame] | 115 | |
| 116 | @Override |
| 117 | public AccountInfo wrapAccount(Context context, AccountWithDataSet account) { |
| 118 | // Use the "SIM" type label for the name as well because on OEM phones the "name" is |
| 119 | // not always user-friendly |
| 120 | return new AccountInfo( |
| 121 | new AccountDisplayInfo(account, getDisplayLabel(context), getDisplayLabel(context), |
| 122 | getDisplayIcon(context), true), this); |
| 123 | } |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 124 | } |