Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 17 | package com.android.contacts.editor; |
| 18 | |
| 19 | import com.android.contacts.R; |
| 20 | import com.android.contacts.common.model.AccountTypeManager; |
| 21 | import com.android.contacts.common.model.RawContactDelta; |
| 22 | import com.android.contacts.common.model.RawContactDeltaList; |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 23 | import com.android.contacts.common.model.RawContactModifier; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 24 | import com.android.contacts.common.model.ValuesDelta; |
| 25 | import com.android.contacts.common.model.account.AccountType; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 26 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 27 | import com.android.contacts.common.model.dataitem.DataKind; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 28 | import com.android.contacts.common.util.AccountsListAdapter; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 29 | import com.android.contacts.common.util.MaterialColorMapUtils; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 30 | import com.android.contacts.editor.CompactContactEditorFragment.PhotoHandler; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 31 | import com.android.contacts.util.UiClosables; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 32 | |
| 33 | import android.content.Context; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 34 | import android.graphics.Bitmap; |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 36 | import android.provider.ContactsContract.CommonDataKinds.Email; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 37 | import android.provider.ContactsContract.CommonDataKinds.Event; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 38 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 39 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 40 | import android.provider.ContactsContract.CommonDataKinds.Nickname; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 41 | import android.provider.ContactsContract.CommonDataKinds.Note; |
| 42 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 43 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 44 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 45 | import android.provider.ContactsContract.CommonDataKinds.Relation; |
| 46 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 47 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 48 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
| 49 | import android.provider.ContactsContract.CommonDataKinds.Website; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 50 | import android.text.TextUtils; |
| 51 | import android.util.AttributeSet; |
| 52 | import android.util.Log; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 53 | import android.util.Pair; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 54 | import android.view.LayoutInflater; |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 55 | import android.view.View; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 56 | import android.view.ViewGroup; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 57 | import android.widget.AdapterView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 58 | import android.widget.LinearLayout; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 59 | import android.widget.ListPopupWindow; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 60 | import android.widget.TextView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 61 | |
| 62 | import java.util.ArrayList; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 63 | import java.util.Arrays; |
| 64 | import java.util.Comparator; |
Walter Jang | 19d985f | 2015-07-01 13:36:27 -0700 | [diff] [blame] | 65 | import java.util.HashMap; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 66 | import java.util.List; |
Walter Jang | 19d985f | 2015-07-01 13:36:27 -0700 | [diff] [blame] | 67 | import java.util.Map; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 68 | import java.util.Objects; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 69 | import java.util.TreeSet; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 72 | * View to display information from multiple {@link RawContactDelta}s grouped together. |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 73 | */ |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 74 | public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener { |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 75 | |
| 76 | private static final String TAG = "CompactEditorView"; |
| 77 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 78 | private static final MimeTypeComparator MIME_TYPE_COMPARATOR = new MimeTypeComparator(); |
| 79 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Callbacks for hosts of {@link CompactRawContactsEditorView}s. |
| 82 | */ |
| 83 | public interface Listener { |
| 84 | |
| 85 | /** |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 86 | * Invoked when the structured name editor field has changed. |
| 87 | * |
| 88 | * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}. |
| 89 | * @param valuesDelta The values from the underlying {@link RawContactDelta}. |
| 90 | */ |
| 91 | public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Invoked when the compact editor should rebind editors for a new account. |
| 95 | * |
| 96 | * @param oldState Old data being edited. |
| 97 | * @param oldAccount Old account associated with oldState. |
| 98 | * @param newAccount New account to be used. |
| 99 | */ |
| 100 | public void onRebindEditorsForNewContact(RawContactDelta oldState, |
| 101 | AccountWithDataSet oldAccount, AccountWithDataSet newAccount); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 104 | /** Used to sort kind sections. */ |
| 105 | private static final class MimeTypeComparator implements |
| 106 | Comparator<Map.Entry<String,List<KindSectionData>>> { |
| 107 | |
| 108 | // Order of kinds roughly matches quick contacts; we diverge in the following ways: |
| 109 | // 1) all names are together at the top (in quick contacts the nickname and phonetic name |
| 110 | // are in the about card) |
| 111 | // 2) IM is moved up after addresses |
| 112 | // 3) SIP addresses are moved to below phone numbers |
| 113 | private static final List<String> MIME_TYPE_ORDER = Arrays.asList(new String[] { |
| 114 | StructuredName.CONTENT_ITEM_TYPE, |
| 115 | DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME, |
| 116 | DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME, |
| 117 | Nickname.CONTENT_ITEM_TYPE, |
| 118 | Phone.CONTENT_ITEM_TYPE, |
| 119 | SipAddress.CONTENT_ITEM_TYPE, |
| 120 | Email.CONTENT_ITEM_TYPE, |
| 121 | StructuredPostal.CONTENT_ITEM_TYPE, |
| 122 | Im.CONTENT_ITEM_TYPE, |
| 123 | Website.CONTENT_ITEM_TYPE, |
| 124 | Organization.CONTENT_ITEM_TYPE, |
| 125 | Event.CONTENT_ITEM_TYPE, |
| 126 | Relation.CONTENT_ITEM_TYPE, |
| 127 | Note.CONTENT_ITEM_TYPE |
| 128 | }); |
| 129 | |
| 130 | @Override |
| 131 | public int compare(Map.Entry<String, List<KindSectionData>> entry1, |
| 132 | Map.Entry<String, List<KindSectionData>> entry2) { |
| 133 | if (entry1 == entry2) return 0; |
| 134 | if (entry1 == null) return -1; |
| 135 | if (entry2 == null) return 1; |
| 136 | |
| 137 | final String mimeType1 = entry1.getKey(); |
| 138 | final String mimeType2 = entry2.getKey(); |
| 139 | |
| 140 | int index1 = MIME_TYPE_ORDER.indexOf(mimeType1); |
| 141 | int index2 = MIME_TYPE_ORDER.indexOf(mimeType2); |
| 142 | |
| 143 | // Fallback to alphabetical ordering of the mime type if both are not found |
| 144 | if (index1 < 0 && index2 < 0) return mimeType1.compareTo(mimeType2); |
| 145 | if (index1 < 0) return 1; |
| 146 | if (index2 < 0) return -1; |
| 147 | |
| 148 | return index1 < index2 ? -1 : 1; |
| 149 | } |
| 150 | } |
| 151 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 152 | private CompactRawContactsEditorView.Listener mListener; |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 153 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 154 | private AccountTypeManager mAccountTypeManager; |
| 155 | private LayoutInflater mLayoutInflater; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 156 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 157 | private ViewIdGenerator mViewIdGenerator; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 158 | private MaterialColorMapUtils.MaterialPalette mMaterialPalette; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 159 | private long mPhotoId; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 160 | private boolean mHasNewContact; |
| 161 | private boolean mIsUserProfile; |
| 162 | private AccountWithDataSet mPrimaryAccount; |
| 163 | private RawContactDelta mPrimaryRawContactDelta; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 164 | private Map<String,List<KindSectionData>> mKindSectionDataMap = new HashMap<>(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 165 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 166 | // Account header |
| 167 | private View mAccountHeaderContainer; |
| 168 | private TextView mAccountHeaderType; |
| 169 | private TextView mAccountHeaderName; |
| 170 | |
| 171 | // Account selector |
| 172 | private View mAccountSelectorContainer; |
| 173 | private View mAccountSelector; |
| 174 | private TextView mAccountSelectorType; |
| 175 | private TextView mAccountSelectorName; |
| 176 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 177 | private CompactPhotoEditorView mPhotoView; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 178 | private ViewGroup mKindSectionViews; |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 179 | private View mMoreFields; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 180 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 181 | private long mPhotoRawContactId; |
| 182 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 183 | public CompactRawContactsEditorView(Context context) { |
| 184 | super(context); |
| 185 | } |
| 186 | |
| 187 | public CompactRawContactsEditorView(Context context, AttributeSet attrs) { |
| 188 | super(context, attrs); |
| 189 | } |
| 190 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Sets the receiver for {@link CompactRawContactsEditorView} callbacks. |
| 193 | */ |
| 194 | public void setListener(Listener listener) { |
| 195 | mListener = listener; |
| 196 | } |
| 197 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 198 | @Override |
| 199 | protected void onFinishInflate() { |
| 200 | super.onFinishInflate(); |
| 201 | |
| 202 | mAccountTypeManager = AccountTypeManager.getInstance(getContext()); |
| 203 | mLayoutInflater = (LayoutInflater) |
| 204 | getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 205 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 206 | // Account header |
| 207 | mAccountHeaderContainer = findViewById(R.id.account_container); |
| 208 | mAccountHeaderType = (TextView) findViewById(R.id.account_type); |
| 209 | mAccountHeaderName = (TextView) findViewById(R.id.account_name); |
| 210 | |
| 211 | // Account selector |
| 212 | mAccountSelectorContainer = findViewById(R.id.account_selector_container); |
| 213 | mAccountSelector = findViewById(R.id.account); |
| 214 | mAccountSelectorType = (TextView) findViewById(R.id.account_type_selector); |
| 215 | mAccountSelectorName = (TextView) findViewById(R.id.account_name_selector); |
| 216 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 217 | mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 218 | mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 219 | mMoreFields = findViewById(R.id.more_fields); |
| 220 | mMoreFields.setOnClickListener(this); |
| 221 | } |
| 222 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 223 | @Override |
| 224 | public void onClick(View view) { |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 225 | if (view.getId() == R.id.more_fields) { |
| 226 | // Stop hiding empty editors and allow the user to enter values for all kinds now |
| 227 | for (int i = 0; i < mKindSectionViews.getChildCount(); i++) { |
| 228 | final CompactKindSectionView kindSectionView = |
| 229 | (CompactKindSectionView) mKindSectionViews.getChildAt(i); |
| 230 | kindSectionView.setHideWhenEmpty(false); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 231 | // Prevent the user from adding new names |
| 232 | if (!StructuredName.CONTENT_ITEM_TYPE.equals(kindSectionView.getMimeType())) { |
| 233 | kindSectionView.setShowOneEmptyEditor(true); |
| 234 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 235 | kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ false); |
| 236 | } |
| 237 | |
| 238 | updateMoreFieldsButton(); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 239 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void setEnabled(boolean enabled) { |
| 244 | super.setEnabled(enabled); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 245 | final int childCount = mKindSectionViews.getChildCount(); |
| 246 | for (int i = 0; i < childCount; i++) { |
| 247 | mKindSectionViews.getChildAt(i).setEnabled(enabled); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 251 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 252 | * Pass through to {@link CompactPhotoEditorView#setPhotoHandler}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 253 | */ |
| 254 | public void setPhotoHandler(PhotoHandler photoHandler) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 255 | mPhotoView.setPhotoHandler(photoHandler); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 259 | * Pass through to {@link CompactPhotoEditorView#setPhoto}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 260 | */ |
| 261 | public void setPhoto(Bitmap bitmap) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 262 | mPhotoView.setPhoto(bitmap); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 266 | * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}. |
| 267 | */ |
| 268 | public void setFullSizePhoto(Uri photoUri) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 269 | mPhotoView.setFullSizedPhoto(photoUri); |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 273 | * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 274 | */ |
| 275 | public boolean isWritablePhotoSet() { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 276 | return mPhotoView.isWritablePhotoSet(); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 280 | * Get the raw contact ID for the CompactHeaderView photo. |
| 281 | */ |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 282 | public long getPhotoRawContactId() { |
| 283 | return mPhotoRawContactId; |
| 284 | } |
| 285 | |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 286 | public View getAggregationAnchorView() { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 287 | // TODO: since there may be more than one structured name now we should return the one |
| 288 | // being edited instead of just the first one. |
| 289 | for (int i = 0; i < mKindSectionViews.getChildCount(); i++) { |
| 290 | final CompactKindSectionView kindSectionView = |
| 291 | (CompactKindSectionView) mKindSectionViews.getChildAt(i); |
| 292 | if (!StructuredName.CONTENT_ITEM_TYPE.equals(kindSectionView.getMimeType())) { |
| 293 | return kindSectionView.findViewById(R.id.anchor_view); |
| 294 | } |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 295 | } |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 296 | return null; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 299 | public void setState(RawContactDeltaList rawContactDeltas, |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 300 | MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator, |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 301 | long photoId, boolean hasNewContact, boolean isUserProfile, |
| 302 | AccountWithDataSet primaryAccount) { |
| 303 | // Clear previous state and reset views |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 304 | mKindSectionDataMap.clear(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 305 | mKindSectionViews.removeAllViews(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 306 | mMoreFields.setVisibility(View.VISIBLE); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 307 | |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 308 | mMaterialPalette = materialPalette; |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 309 | mViewIdGenerator = viewIdGenerator; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 310 | mPhotoId = photoId; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 311 | mHasNewContact = hasNewContact; |
| 312 | mIsUserProfile = isUserProfile; |
| 313 | mPrimaryAccount = primaryAccount; |
| 314 | if (mPrimaryAccount == null) { |
| 315 | mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount(); |
| 316 | } |
| 317 | vlog("state: primary " + mPrimaryAccount); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 318 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 319 | // Parse the given raw contact deltas |
| 320 | if (rawContactDeltas == null || rawContactDeltas.isEmpty()) { |
| 321 | elog("No raw contact deltas"); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 322 | return; |
| 323 | } |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 324 | vlog("state: setting state from " + rawContactDeltas.size() + " RawContactDelta(s)"); |
| 325 | parseRawContactDeltas(rawContactDeltas); |
| 326 | if (mKindSectionDataMap == null || mKindSectionDataMap.isEmpty()) { |
| 327 | elog("No kind section data parsed from RawContactDelta(s)"); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | // Setup the view |
| 332 | setId(mViewIdGenerator.getId(rawContactDeltas.get(0), /* dataKind =*/ null, |
| 333 | /* valuesDelta =*/ null, ViewIdGenerator.NO_VIEW_INDEX)); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 334 | addAccountInfo(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 335 | addPhotoView(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 336 | addKindSectionViews(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 337 | updateMoreFieldsButton(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 340 | private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 341 | if (mPrimaryAccount != null) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 342 | // Use the first writable contact that matches the primary account |
| 343 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 344 | if (!rawContactDelta.isVisible()) continue; |
| 345 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 346 | if (accountType == null || !accountType.areContactsWritable()) continue; |
| 347 | if (matchesPrimaryAccount(rawContactDelta)) { |
| 348 | vlog("parse: matched primary account raw contact"); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 349 | mPrimaryRawContactDelta = rawContactDelta; |
| 350 | break; |
| 351 | } |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 354 | if (mPrimaryRawContactDelta == null) { |
| 355 | // Fall back to the first writable raw contact |
| 356 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 357 | if (!rawContactDelta.isVisible()) continue; |
| 358 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 359 | if (accountType != null && accountType.areContactsWritable()) { |
| 360 | vlog("parse: falling back to the first writable raw contact as primary"); |
| 361 | mPrimaryRawContactDelta = rawContactDelta; |
| 362 | break; |
| 363 | } |
| 364 | } |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 365 | } |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 366 | if (mPrimaryRawContactDelta != null) { |
| 367 | RawContactModifier.ensureKindExists(mPrimaryRawContactDelta, |
| 368 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager), |
| 369 | StructuredName.CONTENT_ITEM_TYPE); |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 370 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 371 | RawContactModifier.ensureKindExists(mPrimaryRawContactDelta, |
| 372 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager), |
| 373 | Photo.CONTENT_ITEM_TYPE); |
| 374 | } |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 375 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 376 | // Build the kind section data list map |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 377 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 378 | if (rawContactDelta == null || !rawContactDelta.isVisible()) continue; |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 379 | vlog("parse: " + rawContactDelta); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 380 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 381 | if (accountType == null) continue; |
| 382 | final List<DataKind> dataKinds = accountType.getSortedDataKinds(); |
| 383 | final int dataKindSize = dataKinds == null ? 0 : dataKinds.size(); |
| 384 | vlog("parse: " + dataKindSize + " dataKinds(s)"); |
| 385 | for (int i = 0; i < dataKindSize; i++) { |
| 386 | final DataKind dataKind = dataKinds.get(i); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 387 | if (dataKind == null || !dataKind.editable) continue; |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 388 | final List<KindSectionData> kindSectionDataList = |
| 389 | getKindSectionDataList(dataKind.mimeType); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 390 | final KindSectionData kindSectionData = |
| 391 | new KindSectionData(accountType, dataKind, rawContactDelta); |
| 392 | kindSectionDataList.add(kindSectionData); |
| 393 | vlog("parse: " + i + " " + dataKind.mimeType + " " + |
| 394 | (kindSectionData.hasValuesDeltas() |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 395 | ? kindSectionData.getValuesDeltas().size() : 0) + " value(s)"); |
Walter Jang | ac679af | 2015-06-01 12:17:06 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 400 | private List<KindSectionData> getKindSectionDataList(String mimeType) { |
| 401 | List<KindSectionData> kindSectionDataList = mKindSectionDataMap.get(mimeType); |
| 402 | if (kindSectionDataList == null) { |
| 403 | kindSectionDataList = new ArrayList<>(); |
| 404 | mKindSectionDataMap.put(mimeType, kindSectionDataList); |
| 405 | } |
| 406 | return kindSectionDataList; |
| 407 | } |
| 408 | |
| 409 | /** Whether the given RawContactDelta is from the primary account. */ |
| 410 | private boolean matchesPrimaryAccount(RawContactDelta rawContactDelta) { |
| 411 | return Objects.equals(mPrimaryAccount.name, rawContactDelta.getAccountName()) |
| 412 | && Objects.equals(mPrimaryAccount.type, rawContactDelta.getAccountType()) |
| 413 | && Objects.equals(mPrimaryAccount.dataSet, rawContactDelta.getDataSet()); |
| 414 | } |
| 415 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 416 | private void addAccountInfo() { |
| 417 | if (mPrimaryRawContactDelta == null) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 418 | mAccountHeaderContainer.setVisibility(View.GONE); |
| 419 | mAccountSelectorContainer.setVisibility(View.GONE); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | // Get the account information for the primary raw contact delta |
| 424 | final Pair<String,String> accountInfo = EditorUiUtils.getAccountInfo(getContext(), |
| 425 | mIsUserProfile, mPrimaryRawContactDelta.getAccountName(), |
| 426 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager)); |
| 427 | |
| 428 | // The account header and selector show the same information so both shouldn't be visible |
| 429 | // at the same time |
| 430 | final List<AccountWithDataSet> accounts = |
| 431 | AccountTypeManager.getInstance(getContext()).getAccounts(true); |
| 432 | if (mHasNewContact && !mIsUserProfile && accounts.size() > 1) { |
| 433 | mAccountHeaderContainer.setVisibility(View.GONE); |
| 434 | addAccountSelector(accountInfo); |
| 435 | } else { |
| 436 | addAccountHeader(accountInfo); |
| 437 | mAccountSelectorContainer.setVisibility(View.GONE); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | private void addAccountHeader(Pair<String,String> accountInfo) { |
Walter Jang | 03cea2e | 2015-09-18 17:04:21 -0700 | [diff] [blame] | 442 | if (TextUtils.isEmpty(accountInfo.first)) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 443 | // Hide this view so the other text view will be centered vertically |
| 444 | mAccountHeaderName.setVisibility(View.GONE); |
| 445 | } else { |
| 446 | mAccountHeaderName.setVisibility(View.VISIBLE); |
| 447 | mAccountHeaderName.setText(accountInfo.first); |
| 448 | } |
| 449 | mAccountHeaderType.setText(accountInfo.second); |
| 450 | |
| 451 | mAccountHeaderContainer.setContentDescription( |
| 452 | EditorUiUtils.getAccountInfoContentDescription( |
| 453 | accountInfo.first, accountInfo.second)); |
| 454 | } |
| 455 | |
| 456 | private void addAccountSelector(Pair<String,String> accountInfo) { |
| 457 | mAccountSelectorContainer.setVisibility(View.VISIBLE); |
| 458 | |
Walter Jang | 03cea2e | 2015-09-18 17:04:21 -0700 | [diff] [blame] | 459 | if (TextUtils.isEmpty(accountInfo.first)) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 460 | // Hide this view so the other text view will be centered vertically |
| 461 | mAccountSelectorName.setVisibility(View.GONE); |
| 462 | } else { |
| 463 | mAccountSelectorName.setVisibility(View.VISIBLE); |
| 464 | mAccountSelectorName.setText(accountInfo.first); |
| 465 | } |
| 466 | mAccountSelectorType.setText(accountInfo.second); |
| 467 | |
| 468 | mAccountSelectorContainer.setContentDescription( |
| 469 | EditorUiUtils.getAccountInfoContentDescription( |
| 470 | accountInfo.first, accountInfo.second)); |
| 471 | |
| 472 | mAccountSelector.setOnClickListener(new View.OnClickListener() { |
| 473 | @Override |
| 474 | public void onClick(View v) { |
| 475 | final ListPopupWindow popup = new ListPopupWindow(getContext(), null); |
| 476 | final AccountsListAdapter adapter = |
| 477 | new AccountsListAdapter(getContext(), |
| 478 | AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE, |
| 479 | mPrimaryAccount); |
| 480 | popup.setWidth(mAccountSelectorContainer.getWidth()); |
| 481 | popup.setAnchorView(mAccountSelectorContainer); |
| 482 | popup.setAdapter(adapter); |
| 483 | popup.setModal(true); |
| 484 | popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 485 | popup.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 486 | @Override |
| 487 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 488 | long id) { |
| 489 | UiClosables.closeQuietly(popup); |
| 490 | final AccountWithDataSet newAccount = adapter.getItem(position); |
| 491 | if (mListener != null && !mPrimaryAccount.equals(newAccount)) { |
| 492 | mListener.onRebindEditorsForNewContact( |
| 493 | mPrimaryRawContactDelta, |
| 494 | mPrimaryAccount, |
| 495 | newAccount); |
| 496 | } |
| 497 | } |
| 498 | }); |
| 499 | popup.show(); |
| 500 | } |
| 501 | }); |
| 502 | } |
| 503 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 504 | private void addPhotoView() { |
| 505 | // Get the kind section data and values delta that will back the photo view |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 506 | Pair<KindSectionData,ValuesDelta> pair = getPrimaryKindSectionData(mPhotoId); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 507 | if (pair == null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 508 | wlog("photo: no kind section data parsed"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 509 | return; |
| 510 | } |
| 511 | final KindSectionData kindSectionData = pair.first; |
| 512 | final ValuesDelta valuesDelta = pair.second; |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 513 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 514 | // If we're editing a read-only contact we want to display the photo from the |
| 515 | // read-only contact in a photo editor backed by the new raw contact |
| 516 | // that was created. The new raw contact is the first writable one. |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 517 | if (mHasNewContact) { |
| 518 | mPhotoRawContactId = mPrimaryRawContactDelta == null |
| 519 | ? null : mPrimaryRawContactDelta.getRawContactId(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 520 | } |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 521 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 522 | mPhotoRawContactId = kindSectionData.getRawContactDelta().getRawContactId(); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 523 | mPhotoView.setValues(kindSectionData.getDataKind(), valuesDelta, |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 524 | kindSectionData.getRawContactDelta(), |
| 525 | !kindSectionData.getAccountType().areContactsWritable(), mMaterialPalette, |
| 526 | mViewIdGenerator); |
| 527 | } |
| 528 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 529 | private Pair<KindSectionData,ValuesDelta> getPrimaryKindSectionData(long id) { |
| 530 | final String mimeType = Photo.CONTENT_ITEM_TYPE; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 531 | final List<KindSectionData> kindSectionDataList = mKindSectionDataMap.get(mimeType); |
| 532 | if (kindSectionDataList == null || kindSectionDataList.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 533 | wlog("photo: no kind section data parsed"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 534 | return null; |
| 535 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 536 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 537 | KindSectionData resultKindSectionData = null; |
| 538 | ValuesDelta resultValuesDelta = null; |
| 539 | if (id > 0) { |
| 540 | // Look for a match for the ID that was passed in |
| 541 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 542 | resultValuesDelta = kindSectionData.getValuesDeltaById(id); |
| 543 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 544 | vlog("photo: matched kind section data by ID"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 545 | resultKindSectionData = kindSectionData; |
| 546 | break; |
Walter Jang | 398cd4b | 2015-06-16 11:17:53 -0700 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 550 | if (resultKindSectionData == null) { |
| 551 | // Look for a super primary photo |
| 552 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 553 | resultValuesDelta = kindSectionData.getSuperPrimaryValuesDelta(); |
| 554 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 555 | wlog("photo: matched super primary kind section data"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 556 | resultKindSectionData = kindSectionData; |
| 557 | break; |
| 558 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 559 | } |
| 560 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 561 | if (resultKindSectionData == null) { |
| 562 | // Fall back to the first non-empty value |
| 563 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 564 | resultValuesDelta = kindSectionData.getFirstNonEmptyValuesDelta(); |
| 565 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 566 | vlog("photo: using first non empty value"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 567 | resultKindSectionData = kindSectionData; |
| 568 | break; |
| 569 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 570 | } |
| 571 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 572 | if (resultKindSectionData == null || resultValuesDelta == null) { |
| 573 | final List<ValuesDelta> valuesDeltaList = kindSectionDataList.get(0).getValuesDeltas(); |
| 574 | if (valuesDeltaList != null && !valuesDeltaList.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 575 | vlog("photo: falling back to first empty entry"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 576 | resultValuesDelta = valuesDeltaList.get(0); |
| 577 | resultKindSectionData = kindSectionDataList.get(0); |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 578 | } |
| 579 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 580 | return resultKindSectionData != null && resultValuesDelta != null |
| 581 | ? new Pair<>(resultKindSectionData, resultValuesDelta) : null; |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 584 | private void addKindSectionViews() { |
| 585 | // Sort the kinds |
| 586 | final TreeSet<Map.Entry<String,List<KindSectionData>>> entries = |
| 587 | new TreeSet<>(MIME_TYPE_COMPARATOR); |
| 588 | entries.addAll(mKindSectionDataMap.entrySet()); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 589 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 590 | vlog("kind: " + entries.size() + " kindSection(s)"); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 591 | int i = -1; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 592 | for (Map.Entry<String, List<KindSectionData>> entry : entries) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 593 | i++; |
| 594 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 595 | final String mimeType = entry.getKey(); |
| 596 | final List<KindSectionData> kindSectionDataList = entry.getValue(); |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 597 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 598 | // Ignore mime types that we've already handled |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 599 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 600 | vlog("kind: " + i + " " + mimeType + " dropped"); |
| 601 | continue; |
| 602 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 603 | |
| 604 | // Ignore mime types that we don't handle |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 605 | if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType) |
| 606 | || DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)) { |
| 607 | vlog("kind: " + i + " " + mimeType + " dropped"); |
| 608 | continue; |
| 609 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 610 | |
| 611 | if (kindSectionDataList != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 612 | vlog("kind: " + i + " " + mimeType + ": " + kindSectionDataList.size() + |
| 613 | " kindSectionData(s)"); |
| 614 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 615 | final CompactKindSectionView kindSectionView = inflateKindSectionView( |
| 616 | mKindSectionViews, kindSectionDataList, mimeType); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 617 | mKindSectionViews.addView(kindSectionView); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 622 | private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup, |
| 623 | List<KindSectionData> kindSectionDataList, String mimeType) { |
| 624 | final CompactKindSectionView kindSectionView = (CompactKindSectionView) |
| 625 | mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup, |
| 626 | /* attachToRoot =*/ false); |
| 627 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 628 | if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType) |
| 629 | || Phone.CONTENT_ITEM_TYPE.equals(mimeType) |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 630 | || Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 631 | // Names, phone numbers, and email addresses are always displayed. |
| 632 | // Phone numbers and email addresses are the only types you add new values |
| 633 | // to initially. |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 634 | kindSectionView.setHideWhenEmpty(false); |
| 635 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 636 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame^] | 637 | // Prevent the user from adding any new names |
| 638 | if (!StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 639 | kindSectionView.setShowOneEmptyEditor(true); |
| 640 | } |
| 641 | |
| 642 | kindSectionView.setState(kindSectionDataList, /* readOnly =*/ false, mViewIdGenerator, |
| 643 | mListener); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 644 | |
| 645 | return kindSectionView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 648 | private void updateMoreFieldsButton() { |
| 649 | // If any kind section views are hidden then show the link |
| 650 | for (int i = 0; i < mKindSectionViews.getChildCount(); i++) { |
| 651 | final CompactKindSectionView kindSectionView = |
| 652 | (CompactKindSectionView) mKindSectionViews.getChildAt(i); |
| 653 | if (kindSectionView.getVisibility() == View.GONE) { |
| 654 | // Show the more fields button |
| 655 | mMoreFields.setVisibility(View.VISIBLE); |
| 656 | return; |
| 657 | } |
| 658 | } |
| 659 | // Hide the more fields button |
| 660 | mMoreFields.setVisibility(View.GONE); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 663 | private static void vlog(String message) { |
| 664 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 665 | Log.v(TAG, message); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 666 | } |
| 667 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 668 | |
| 669 | private static void wlog(String message) { |
| 670 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 671 | Log.w(TAG, message); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | private static void elog(String message) { |
| 676 | Log.e(TAG, message); |
| 677 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 678 | } |