blob: 057a988ae9c4543bf4604ee018f511e9107e8691 [file] [log] [blame]
Walter Jangcab3dce2015-02-09 17:48:03 -08001/*
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
17package com.android.contacts.editor;
18
Walter Jangcab3dce2015-02-09 17:48:03 -080019import android.content.Context;
Gary Mai65971d02016-09-15 15:00:16 -070020import android.content.res.Resources;
Walter Jangf10ca152015-09-22 15:23:55 -070021import android.database.Cursor;
Gary Mai65971d02016-09-15 15:00:16 -070022import android.graphics.drawable.Drawable;
Walter Jang41b3ea12015-03-09 17:30:06 -070023import android.net.Uri;
Walter Jang79658e12015-09-24 10:36:26 -070024import android.os.Parcel;
25import android.os.Parcelable;
Walter Jangcab3dce2015-02-09 17:48:03 -080026import android.provider.ContactsContract.CommonDataKinds.Email;
Walter Jangf5dfea42015-09-16 12:30:36 -070027import android.provider.ContactsContract.CommonDataKinds.Event;
Walter Jangcab3dce2015-02-09 17:48:03 -080028import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
Walter Jangf5dfea42015-09-16 12:30:36 -070029import android.provider.ContactsContract.CommonDataKinds.Im;
Walter Jang3efae4a2015-02-18 11:12:00 -080030import android.provider.ContactsContract.CommonDataKinds.Nickname;
Walter Jangf5dfea42015-09-16 12:30:36 -070031import android.provider.ContactsContract.CommonDataKinds.Note;
32import android.provider.ContactsContract.CommonDataKinds.Organization;
Walter Jangcab3dce2015-02-09 17:48:03 -080033import android.provider.ContactsContract.CommonDataKinds.Phone;
34import android.provider.ContactsContract.CommonDataKinds.Photo;
Walter Jangf5dfea42015-09-16 12:30:36 -070035import android.provider.ContactsContract.CommonDataKinds.Relation;
36import android.provider.ContactsContract.CommonDataKinds.SipAddress;
Walter Jangcab3dce2015-02-09 17:48:03 -080037import android.provider.ContactsContract.CommonDataKinds.StructuredName;
Walter Jangf5dfea42015-09-16 12:30:36 -070038import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
39import android.provider.ContactsContract.CommonDataKinds.Website;
Walter Jangcab3dce2015-02-09 17:48:03 -080040import android.text.TextUtils;
41import android.util.AttributeSet;
42import android.util.Log;
43import android.view.LayoutInflater;
Walter Jangb1c87622015-02-13 17:51:38 -080044import android.view.View;
Walter Jangcab3dce2015-02-09 17:48:03 -080045import android.view.ViewGroup;
Walter Jang708ea9e2015-09-10 15:42:05 -070046import android.widget.AdapterView;
Tingting Wang655ad1a2015-10-05 17:51:14 -070047import android.widget.BaseAdapter;
48import android.widget.ImageView;
Walter Jangcab3dce2015-02-09 17:48:03 -080049import android.widget.LinearLayout;
Walter Jang708ea9e2015-09-10 15:42:05 -070050import android.widget.ListPopupWindow;
Walter Jang2d3f31c2015-06-18 23:15:31 -070051import android.widget.TextView;
Walter Jangcab3dce2015-02-09 17:48:03 -080052
Gary Maib6e28d22016-09-12 14:04:53 -070053import com.android.contacts.R;
Gary Mai65971d02016-09-15 15:00:16 -070054import com.android.contacts.common.GeoUtil;
55import com.android.contacts.common.compat.PhoneNumberUtilsCompat;
Gary Maib6e28d22016-09-12 14:04:53 -070056import com.android.contacts.common.model.AccountTypeManager;
57import com.android.contacts.common.model.RawContactDelta;
58import com.android.contacts.common.model.RawContactDeltaList;
59import com.android.contacts.common.model.RawContactModifier;
60import com.android.contacts.common.model.ValuesDelta;
61import com.android.contacts.common.model.account.AccountDisplayInfo;
62import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
63import com.android.contacts.common.model.account.AccountType;
64import com.android.contacts.common.model.account.AccountWithDataSet;
Gary Mai9fa89872016-09-19 15:29:59 -070065import com.android.contacts.common.model.account.DeviceLocalAccountType;
66import com.android.contacts.common.model.account.SimAccountType;
Gary Mai98868d32016-09-14 11:55:04 -070067import com.android.contacts.common.model.dataitem.CustomDataItem;
Gary Maib6e28d22016-09-12 14:04:53 -070068import com.android.contacts.common.model.dataitem.DataKind;
69import com.android.contacts.common.util.AccountsListAdapter;
70import com.android.contacts.common.util.MaterialColorMapUtils;
71import com.android.contacts.util.UiClosables;
72
Walter Jang708774a2015-10-16 09:32:06 -070073import java.io.FileNotFoundException;
Walter Jangcab3dce2015-02-09 17:48:03 -080074import java.util.ArrayList;
Walter Jangf5dfea42015-09-16 12:30:36 -070075import java.util.Arrays;
Walter Jang192a01c2015-09-22 15:23:55 -070076import java.util.Collections;
Walter Jangf5dfea42015-09-16 12:30:36 -070077import java.util.Comparator;
Walter Jang19d985f2015-07-01 13:36:27 -070078import java.util.HashMap;
Walter Jangcab3dce2015-02-09 17:48:03 -080079import java.util.List;
Walter Jang19d985f2015-07-01 13:36:27 -070080import java.util.Map;
Gary Mai98868d32016-09-14 11:55:04 -070081import java.util.Set;
Walter Jangf5dfea42015-09-16 12:30:36 -070082import java.util.TreeSet;
Walter Jangcab3dce2015-02-09 17:48:03 -080083
84/**
Walter Jangf5dfea42015-09-16 12:30:36 -070085 * View to display information from multiple {@link RawContactDelta}s grouped together.
Walter Jangcab3dce2015-02-09 17:48:03 -080086 */
Walter Jangb6ca2722015-02-20 11:10:25 -080087public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener {
Walter Jangcab3dce2015-02-09 17:48:03 -080088
Walter Jang4f5594a2015-10-06 18:40:31 -070089 static final String TAG = "CompactEditorView";
Walter Jangcab3dce2015-02-09 17:48:03 -080090
Walter Jangb6ca2722015-02-20 11:10:25 -080091 /**
92 * Callbacks for hosts of {@link CompactRawContactsEditorView}s.
93 */
94 public interface Listener {
95
96 /**
Walter Jang151f3e62015-02-26 15:29:40 -080097 * Invoked when the structured name editor field has changed.
98 *
99 * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}.
100 * @param valuesDelta The values from the underlying {@link RawContactDelta}.
101 */
102 public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta);
Walter Jang708ea9e2015-09-10 15:42:05 -0700103
104 /**
105 * Invoked when the compact editor should rebind editors for a new account.
106 *
107 * @param oldState Old data being edited.
108 * @param oldAccount Old account associated with oldState.
109 * @param newAccount New account to be used.
110 */
111 public void onRebindEditorsForNewContact(RawContactDelta oldState,
112 AccountWithDataSet oldAccount, AccountWithDataSet newAccount);
Walter Jang79658e12015-09-24 10:36:26 -0700113
114 /**
115 * Invoked when no editors could be bound for the contact.
116 */
117 public void onBindEditorsFailed();
Walter Jangd6753152015-10-02 09:23:13 -0700118
119 /**
120 * Invoked after editors have been bound for the contact.
121 */
122 public void onEditorsBound();
Tingting Wang655ad1a2015-10-05 17:51:14 -0700123
124 /**
Tingting Wang5585c6e2015-10-14 11:19:58 -0700125 * Invoked when a rawcontact from linked contacts is selected in editor.
Tingting Wang655ad1a2015-10-05 17:51:14 -0700126 */
Gary Mai4ceabed2016-09-16 12:14:13 -0700127 void onRawContactSelected(long rawContactId, boolean isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700128 }
129
130 /**
131 * Used to list the account info for the given raw contacts list.
132 */
133 private static final class RawContactAccountListAdapter extends BaseAdapter {
134 private final LayoutInflater mInflater;
135 private final Context mContext;
136 private final RawContactDeltaList mRawContactDeltas;
137
138 public RawContactAccountListAdapter(Context context, RawContactDeltaList rawContactDeltas) {
139 mContext = context;
140 mRawContactDeltas = new RawContactDeltaList();
141 for (RawContactDelta rawContactDelta : rawContactDeltas) {
Walter Jang23709542015-10-22 12:50:58 -0700142 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700143 mRawContactDeltas.add(rawContactDelta);
144 }
145 }
146 mInflater = LayoutInflater.from(context);
147 }
148
149 @Override
150 public View getView(int position, View convertView, ViewGroup parent) {
151 final View resultView = convertView != null ? convertView
152 : mInflater.inflate(R.layout.account_selector_list_item, parent, false);
153
154 final RawContactDelta rawContactDelta = mRawContactDeltas.get(position);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700155
156 final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
Walter Jang9488a762015-10-16 13:42:48 -0700157 final AccountType accountType = rawContactDelta.getRawContactAccountType(mContext);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700158 text1.setText(accountType.getDisplayLabel(mContext));
159
Tingting Wang655ad1a2015-10-05 17:51:14 -0700160 final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
Walter Jang9488a762015-10-16 13:42:48 -0700161 final String accountName = rawContactDelta.getAccountName();
Gary Mai9fa89872016-09-19 15:29:59 -0700162 if (TextUtils.isEmpty(accountName) || accountType instanceof DeviceLocalAccountType
163 || accountType instanceof SimAccountType) {
Tingting Wanga0b69402015-10-13 14:27:58 -0700164 text2.setVisibility(View.GONE);
165 } else {
Walter Jang9488a762015-10-16 13:42:48 -0700166 // Truncate email addresses in the middle so we don't lose the domain
Tingting Wanga0b69402015-10-13 14:27:58 -0700167 text2.setText(accountName);
168 text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
169 }
Tingting Wang655ad1a2015-10-05 17:51:14 -0700170
171 final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
172 icon.setImageDrawable(accountType.getDisplayIcon(mContext));
173
174 return resultView;
175 }
176
177 @Override
178 public int getCount() {
179 return mRawContactDeltas.size();
180 }
181
182 @Override
183 public RawContactDelta getItem(int position) {
184 return mRawContactDeltas.get(position);
185 }
186
187 @Override
188 public long getItemId(int position) {
189 return getItem(position).getRawContactId();
190 }
Walter Jang151f3e62015-02-26 15:29:40 -0800191 }
192
Walter Jang192a01c2015-09-22 15:23:55 -0700193 /**
194 * Sorts kinds roughly the same as quick contacts; we diverge in the following ways:
195 * <ol>
196 * <li>All names are together at the top.</li>
197 * <li>IM is moved up after addresses</li>
198 * <li>SIP addresses are moved to below phone numbers</li>
Walter Jang79658e12015-09-24 10:36:26 -0700199 * <li>Group membership is placed at the end</li>
Walter Jang192a01c2015-09-22 15:23:55 -0700200 * </ol>
201 */
202 private static final class MimeTypeComparator implements Comparator<String> {
203
Walter Jangf5dfea42015-09-16 12:30:36 -0700204 private static final List<String> MIME_TYPE_ORDER = Arrays.asList(new String[] {
205 StructuredName.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700206 Nickname.CONTENT_ITEM_TYPE,
Walter Jang2ae21752015-09-25 11:25:38 -0700207 Organization.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700208 Phone.CONTENT_ITEM_TYPE,
209 SipAddress.CONTENT_ITEM_TYPE,
210 Email.CONTENT_ITEM_TYPE,
211 StructuredPostal.CONTENT_ITEM_TYPE,
212 Im.CONTENT_ITEM_TYPE,
213 Website.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700214 Event.CONTENT_ITEM_TYPE,
215 Relation.CONTENT_ITEM_TYPE,
Walter Jangf10ca152015-09-22 15:23:55 -0700216 Note.CONTENT_ITEM_TYPE,
217 GroupMembership.CONTENT_ITEM_TYPE
Walter Jangf5dfea42015-09-16 12:30:36 -0700218 });
219
220 @Override
Walter Jang192a01c2015-09-22 15:23:55 -0700221 public int compare(String mimeType1, String mimeType2) {
222 if (mimeType1 == mimeType2) return 0;
223 if (mimeType1 == null) return -1;
224 if (mimeType2 == null) return 1;
Walter Jangf5dfea42015-09-16 12:30:36 -0700225
226 int index1 = MIME_TYPE_ORDER.indexOf(mimeType1);
227 int index2 = MIME_TYPE_ORDER.indexOf(mimeType2);
228
229 // Fallback to alphabetical ordering of the mime type if both are not found
230 if (index1 < 0 && index2 < 0) return mimeType1.compareTo(mimeType2);
231 if (index1 < 0) return 1;
232 if (index2 < 0) return -1;
233
234 return index1 < index2 ? -1 : 1;
235 }
236 }
237
Walter Jang79658e12015-09-24 10:36:26 -0700238 public static class SavedState extends BaseSavedState {
239
240 public static final Parcelable.Creator<SavedState> CREATOR =
241 new Parcelable.Creator<SavedState>() {
242 public SavedState createFromParcel(Parcel in) {
243 return new SavedState(in);
244 }
245 public SavedState[] newArray(int size) {
246 return new SavedState[size];
247 }
248 };
249
250 private boolean mIsExpanded;
251
252 public SavedState(Parcelable superState) {
253 super(superState);
254 }
255
256 private SavedState(Parcel in) {
257 super(in);
258 mIsExpanded = in.readInt() != 0;
259 }
260
261 @Override
262 public void writeToParcel(Parcel out, int flags) {
263 super.writeToParcel(out, flags);
264 out.writeInt(mIsExpanded ? 1 : 0);
265 }
266 }
267
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700268 private CompactRawContactsEditorView.Listener mListener;
Walter Jangb6ca2722015-02-20 11:10:25 -0800269
Walter Jangcab3dce2015-02-09 17:48:03 -0800270 private AccountTypeManager mAccountTypeManager;
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700271 private AccountDisplayInfoFactory mAccountDisplayInfoFactory;
Walter Jangcab3dce2015-02-09 17:48:03 -0800272 private LayoutInflater mLayoutInflater;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800273
Walter Jangcab3dce2015-02-09 17:48:03 -0800274 private ViewIdGenerator mViewIdGenerator;
Walter Jangf46abd82015-02-20 16:52:04 -0800275 private MaterialColorMapUtils.MaterialPalette mMaterialPalette;
Walter Jang708ea9e2015-09-10 15:42:05 -0700276 private boolean mHasNewContact;
277 private boolean mIsUserProfile;
278 private AccountWithDataSet mPrimaryAccount;
Walter Jang9a552372016-08-24 11:51:05 -0700279 private RawContactDeltaList mRawContactDeltas;
Gary Mai4ceabed2016-09-16 12:14:13 -0700280 private RawContactDelta mCurrentRawContactDelta;
Walter Jang9a552372016-08-24 11:51:05 -0700281 private long mRawContactIdToDisplayAlone = -1;
Walter Jang9a552372016-08-24 11:51:05 -0700282 private boolean mIsEditingReadOnlyRawContactWithNewContact;
Gary Mai98868d32016-09-14 11:55:04 -0700283 private Map<String, KindSectionData> mKindSectionDataMap = new HashMap<>();
284 private Set<String> mSortedMimetypes = new TreeSet<>(new MimeTypeComparator());
Walter Jangcab3dce2015-02-09 17:48:03 -0800285
Walter Jang708ea9e2015-09-10 15:42:05 -0700286 // Account header
287 private View mAccountHeaderContainer;
288 private TextView mAccountHeaderType;
289 private TextView mAccountHeaderName;
Tingting Wang91cee282015-10-07 13:48:17 -0700290 private ImageView mAccountHeaderIcon;
Gary Mai46cb3102016-08-10 18:14:09 -0700291 private ImageView mAccountHeaderExpanderIcon;
Walter Jang708ea9e2015-09-10 15:42:05 -0700292
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700293 private CompactPhotoEditorView mPhotoView;
Walter Jangf5dfea42015-09-16 12:30:36 -0700294 private ViewGroup mKindSectionViews;
Gary Mai98868d32016-09-14 11:55:04 -0700295 private Map<String, CompactKindSectionView> mKindSectionViewMap = new HashMap<>();
Walter Jangb6ca2722015-02-20 11:10:25 -0800296 private View mMoreFields;
Walter Jangcab3dce2015-02-09 17:48:03 -0800297
Walter Jang79658e12015-09-24 10:36:26 -0700298 private boolean mIsExpanded;
Walter Jangcbd431d2015-10-06 13:07:30 -0700299
Walter Jang31a74ad2015-10-02 19:17:39 -0700300 private ValuesDelta mPhotoValuesDelta;
Walter Jang82acd422015-10-17 14:01:27 -0700301
Walter Jangcab3dce2015-02-09 17:48:03 -0800302 public CompactRawContactsEditorView(Context context) {
303 super(context);
304 }
305
306 public CompactRawContactsEditorView(Context context, AttributeSet attrs) {
307 super(context, attrs);
308 }
309
Walter Jangb6ca2722015-02-20 11:10:25 -0800310 /**
311 * Sets the receiver for {@link CompactRawContactsEditorView} callbacks.
312 */
313 public void setListener(Listener listener) {
314 mListener = listener;
315 }
316
Walter Jangcab3dce2015-02-09 17:48:03 -0800317 @Override
318 protected void onFinishInflate() {
319 super.onFinishInflate();
320
321 mAccountTypeManager = AccountTypeManager.getInstance(getContext());
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700322 mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(getContext());
Walter Jangcab3dce2015-02-09 17:48:03 -0800323 mLayoutInflater = (LayoutInflater)
324 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
325
Walter Jang708ea9e2015-09-10 15:42:05 -0700326 // Account header
Gary Mai46cb3102016-08-10 18:14:09 -0700327 mAccountHeaderContainer = findViewById(R.id.account_header_container);
Walter Jang708ea9e2015-09-10 15:42:05 -0700328 mAccountHeaderType = (TextView) findViewById(R.id.account_type);
329 mAccountHeaderName = (TextView) findViewById(R.id.account_name);
Tingting Wang91cee282015-10-07 13:48:17 -0700330 mAccountHeaderIcon = (ImageView) findViewById(R.id.account_type_icon);
Gary Mai46cb3102016-08-10 18:14:09 -0700331 mAccountHeaderExpanderIcon = (ImageView) findViewById(R.id.account_expander_icon);
Walter Jang708ea9e2015-09-10 15:42:05 -0700332
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700333 mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor);
Walter Jangf5dfea42015-09-16 12:30:36 -0700334 mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views);
Walter Jangb6ca2722015-02-20 11:10:25 -0800335 mMoreFields = findViewById(R.id.more_fields);
336 mMoreFields.setOnClickListener(this);
337 }
338
Walter Jangb6ca2722015-02-20 11:10:25 -0800339 @Override
340 public void onClick(View view) {
Walter Jangf5dfea42015-09-16 12:30:36 -0700341 if (view.getId() == R.id.more_fields) {
Walter Jangd6753152015-10-02 09:23:13 -0700342 showAllFields();
Walter Jangb6ca2722015-02-20 11:10:25 -0800343 }
Walter Jangcab3dce2015-02-09 17:48:03 -0800344 }
345
346 @Override
347 public void setEnabled(boolean enabled) {
348 super.setEnabled(enabled);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700349 final int childCount = mKindSectionViews.getChildCount();
350 for (int i = 0; i < childCount; i++) {
351 mKindSectionViews.getChildAt(i).setEnabled(enabled);
Walter Jangcab3dce2015-02-09 17:48:03 -0800352 }
353 }
354
Walter Jang79658e12015-09-24 10:36:26 -0700355 @Override
356 public Parcelable onSaveInstanceState() {
357 final Parcelable superState = super.onSaveInstanceState();
358 final SavedState savedState = new SavedState(superState);
359 savedState.mIsExpanded = mIsExpanded;
360 return savedState;
361 }
362
363 @Override
364 public void onRestoreInstanceState(Parcelable state) {
365 if(!(state instanceof SavedState)) {
366 super.onRestoreInstanceState(state);
367 return;
368 }
369 final SavedState savedState = (SavedState) state;
370 super.onRestoreInstanceState(savedState.getSuperState());
371 mIsExpanded = savedState.mIsExpanded;
Walter Jangd6753152015-10-02 09:23:13 -0700372 if (mIsExpanded) {
373 showAllFields();
Walter Jang79658e12015-09-24 10:36:26 -0700374 }
375 }
376
Walter Jang3efae4a2015-02-18 11:12:00 -0800377 /**
Walter Jang31a74ad2015-10-02 19:17:39 -0700378 * Pass through to {@link CompactPhotoEditorView#setListener}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800379 */
Walter Jang31a74ad2015-10-02 19:17:39 -0700380 public void setPhotoListener(CompactPhotoEditorView.Listener listener) {
381 mPhotoView.setListener(listener);
Walter Jang3efae4a2015-02-18 11:12:00 -0800382 }
383
Walter Jang31a74ad2015-10-02 19:17:39 -0700384 public void removePhoto() {
Walter Jang45b86d52015-10-15 15:23:16 -0700385 mPhotoValuesDelta.setFromTemplate(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700386 mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null);
387
388 mPhotoView.removePhoto();
Walter Jang3efae4a2015-02-18 11:12:00 -0800389 }
390
391 /**
Walter Jang41b3ea12015-03-09 17:30:06 -0700392 * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}.
393 */
394 public void setFullSizePhoto(Uri photoUri) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700395 mPhotoView.setFullSizedPhoto(photoUri);
Walter Jang41b3ea12015-03-09 17:30:06 -0700396 }
397
Walter Jang31a74ad2015-10-02 19:17:39 -0700398 public void updatePhoto(Uri photoUri) {
Walter Jang45b86d52015-10-15 15:23:16 -0700399 mPhotoValuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700400 // Unset primary for all photos
Wenyi Wang9086fb92015-11-16 09:58:20 -0800401 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700402 // Mark the currently displayed photo as primary
403 mPhotoValuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700404
Wenyi Wang4c9cf3a2015-10-15 17:58:23 -0700405 // Even though high-res photos cannot be saved by passing them via
406 // an EntityDeltaList (since they cause the Bundle size limit to be
407 // exceeded), we still pass a low-res thumbnail. This simplifies
408 // code all over the place, because we don't have to test whether
409 // there is a change in EITHER the delta-list OR a changed photo...
410 // this way, there is always a change in the delta-list.
411 try {
412 final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
413 getContext(), photoUri);
414 if (bytes != null) {
415 mPhotoValuesDelta.setPhoto(bytes);
416 }
417 } catch (FileNotFoundException e) {
418 elog("Failed to get bitmap from photo Uri");
419 }
420
Walter Jang31a74ad2015-10-02 19:17:39 -0700421 mPhotoView.setFullSizedPhoto(photoUri);
422 }
423
Wenyi Wang9086fb92015-11-16 09:58:20 -0800424 private void unsetSuperPrimaryFromAllPhotos() {
Gary Mai4ceabed2016-09-16 12:14:13 -0700425 for (int i = 0; i < mRawContactDeltas.size(); i++) {
426 final RawContactDelta rawContactDelta = mRawContactDeltas.get(i);
427 if (!rawContactDelta.hasMimeEntries(Photo.CONTENT_ITEM_TYPE)) {
428 continue;
429 }
430 final List<ValuesDelta> photosDeltas =
431 mRawContactDeltas.get(i).getMimeEntries(Photo.CONTENT_ITEM_TYPE);
432 if (photosDeltas == null) {
433 continue;
434 }
435 for (int j = 0; j < photosDeltas.size(); j++) {
436 photosDeltas.get(j).setSuperPrimary(false);
Walter Jang3f18d612015-10-07 16:01:05 -0700437 }
438 }
439 }
440
Walter Jang41b3ea12015-03-09 17:30:06 -0700441 /**
Walter Janga5e4bb22015-02-24 13:08:16 -0800442 * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800443 */
444 public boolean isWritablePhotoSet() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700445 return mPhotoView.isWritablePhotoSet();
Walter Jang3efae4a2015-02-18 11:12:00 -0800446 }
447
448 /**
Walter Jang3efae4a2015-02-18 11:12:00 -0800449 * Get the raw contact ID for the CompactHeaderView photo.
450 */
Walter Jang3efae4a2015-02-18 11:12:00 -0800451 public long getPhotoRawContactId() {
Gary Maida20b472016-09-20 14:46:40 -0700452 return mCurrentRawContactDelta.getRawContactId();
Walter Jang3efae4a2015-02-18 11:12:00 -0800453 }
454
Walter Jang4f5594a2015-10-06 18:40:31 -0700455 public StructuredNameEditorView getPrimaryNameEditorView() {
Walter Jange3945952015-10-27 12:44:54 -0700456 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
457 return primaryNameKindSectionView == null
458 ? null : primaryNameKindSectionView.getPrimaryNameEditorView();
Walter Jang4f5594a2015-10-06 18:40:31 -0700459 }
460
Walter Jang31a74ad2015-10-02 19:17:39 -0700461
462 /**
Gary Maida20b472016-09-20 14:46:40 -0700463 * Marks the raw contact photo given as primary for the aggregate contact.
Walter Jang31a74ad2015-10-02 19:17:39 -0700464 */
Gary Maida20b472016-09-20 14:46:40 -0700465 public void setPrimaryPhoto() {
Walter Jangda258ff2015-11-17 11:06:02 -0800466
467 // Update values delta
Gary Mai4ceabed2016-09-16 12:14:13 -0700468 final ValuesDelta valuesDelta = mCurrentRawContactDelta
469 .getSuperPrimaryEntry(Photo.CONTENT_ITEM_TYPE);
Gary Maida20b472016-09-20 14:46:40 -0700470 if (valuesDelta == null) {
471 Log.wtf(TAG, "setPrimaryPhoto: had no ValuesDelta for the current RawContactDelta");
472 return;
473 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700474 valuesDelta.setFromTemplate(false);
Wenyi Wang9086fb92015-11-16 09:58:20 -0800475 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700476 valuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700477 }
478
Walter Jangd35e5ef2015-02-24 09:18:16 -0800479 public View getAggregationAnchorView() {
Gary Mai710802c2016-09-19 15:05:44 -0700480 final StructuredNameEditorView nameEditorView = getPrimaryNameEditorView();
481 return nameEditorView != null ? nameEditorView.findViewById(R.id.anchor_view) : null;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800482 }
483
Walter Jangf10ca152015-09-22 15:23:55 -0700484 public void setGroupMetaData(Cursor groupMetaData) {
Gary Mai98868d32016-09-14 11:55:04 -0700485 final CompactKindSectionView groupKindSectionView =
486 mKindSectionViewMap.get(GroupMembership.CONTENT_ITEM_TYPE);
487 if (groupKindSectionView == null) {
488 return;
489 }
490 groupKindSectionView.setGroupMetaData(groupMetaData);
491 if (mIsExpanded) {
492 groupKindSectionView.setHideWhenEmpty(false);
493 groupKindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
Walter Jangf10ca152015-09-22 15:23:55 -0700494 }
495 }
496
Walter Jangf46abd82015-02-20 16:52:04 -0800497 public void setState(RawContactDeltaList rawContactDeltas,
Walter Jang06f73a12015-06-17 11:15:48 -0700498 MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
Gary Maida20b472016-09-20 14:46:40 -0700499 boolean hasNewContact, boolean isUserProfile, AccountWithDataSet primaryAccount,
500 long rawContactIdToDisplayAlone, boolean isEditingReadOnlyRawContactWithNewContact) {
Marcus Hagerottf04c9752016-09-08 15:22:57 -0700501
Walter Jang9a552372016-08-24 11:51:05 -0700502 mRawContactDeltas = rawContactDeltas;
503 mRawContactIdToDisplayAlone = rawContactIdToDisplayAlone;
Walter Jang9a552372016-08-24 11:51:05 -0700504 mIsEditingReadOnlyRawContactWithNewContact = isEditingReadOnlyRawContactWithNewContact;
505
Gary Mai98868d32016-09-14 11:55:04 -0700506 mKindSectionViewMap.clear();
Walter Jangf5dfea42015-09-16 12:30:36 -0700507 mKindSectionViews.removeAllViews();
Walter Jang363d3fd2015-09-16 10:29:07 -0700508 mMoreFields.setVisibility(View.VISIBLE);
Walter Jangcab3dce2015-02-09 17:48:03 -0800509
Walter Jangf46abd82015-02-20 16:52:04 -0800510 mMaterialPalette = materialPalette;
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700511 mViewIdGenerator = viewIdGenerator;
Tingting Wangf695eb32015-10-15 18:45:15 -0700512
Walter Jang708ea9e2015-09-10 15:42:05 -0700513 mHasNewContact = hasNewContact;
514 mIsUserProfile = isUserProfile;
515 mPrimaryAccount = primaryAccount;
516 if (mPrimaryAccount == null) {
517 mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount();
518 }
519 vlog("state: primary " + mPrimaryAccount);
Walter Jangcab3dce2015-02-09 17:48:03 -0800520
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700521 // Parse the given raw contact deltas
522 if (rawContactDeltas == null || rawContactDeltas.isEmpty()) {
523 elog("No raw contact deltas");
Walter Jang79658e12015-09-24 10:36:26 -0700524 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jangf5dfea42015-09-16 12:30:36 -0700525 return;
526 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700527 pickRawContactDelta();
528 parseRawContactDelta();
Walter Jang79658e12015-09-24 10:36:26 -0700529 if (mKindSectionDataMap.isEmpty()) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700530 elog("No kind section data parsed from RawContactDelta(s)");
Walter Jang79658e12015-09-24 10:36:26 -0700531 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700532 return;
533 }
Walter Jang82acd422015-10-17 14:01:27 -0700534
Gary Mai98868d32016-09-14 11:55:04 -0700535 final KindSectionData nameSectionData =
536 mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE);
537 // Ensure that a structured name and photo exists
538 if (nameSectionData != null) {
Walter Jang82acd422015-10-17 14:01:27 -0700539 final RawContactDelta rawContactDelta =
Gary Mai98868d32016-09-14 11:55:04 -0700540 nameSectionData.getRawContactDelta();
Walter Jang82acd422015-10-17 14:01:27 -0700541 RawContactModifier.ensureKindExists(
542 rawContactDelta,
543 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700544 StructuredName.CONTENT_ITEM_TYPE);
Walter Jang82acd422015-10-17 14:01:27 -0700545 RawContactModifier.ensureKindExists(
546 rawContactDelta,
547 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700548 Photo.CONTENT_ITEM_TYPE);
549 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700550
551 // Setup the view
Walter Jang363d3fd2015-09-16 10:29:07 -0700552 addPhotoView();
Gary Mai4ceabed2016-09-16 12:14:13 -0700553 if (isReadOnlyRawContact()) {
Walter Jang9a552372016-08-24 11:51:05 -0700554 // We're want to display the inputs fields for a single read only raw contact
555 addReadOnlyRawContactEditorViews();
Walter Jang9a552372016-08-24 11:51:05 -0700556 } else {
557 setupCompactEditorNormally();
558 }
Walter Jangd6753152015-10-02 09:23:13 -0700559 if (mListener != null) mListener.onEditorsBound();
Walter Jangcab3dce2015-02-09 17:48:03 -0800560 }
561
Walter Jang9a552372016-08-24 11:51:05 -0700562 private void setupCompactEditorNormally() {
Gary Mai98868d32016-09-14 11:55:04 -0700563 addAccountInfo();
Walter Jang9a552372016-08-24 11:51:05 -0700564 addKindSectionViews();
Walter Jangcab3dce2015-02-09 17:48:03 -0800565
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700566 mMoreFields.setVisibility(hasMoreFields() ? View.VISIBLE : View.GONE);
567
Walter Jang9a552372016-08-24 11:51:05 -0700568 if (mIsExpanded) showAllFields();
569 }
570
Gary Mai4ceabed2016-09-16 12:14:13 -0700571 private boolean isReadOnlyRawContact() {
572 return !mCurrentRawContactDelta.getAccountType(mAccountTypeManager).areContactsWritable();
Walter Jang9a552372016-08-24 11:51:05 -0700573 }
574
Gary Mai4ceabed2016-09-16 12:14:13 -0700575 private void pickRawContactDelta() {
Walter Jang363d3fd2015-09-16 10:29:07 -0700576 // Build the kind section data list map
Gary Mai4ceabed2016-09-16 12:14:13 -0700577 vlog("parse: " + mRawContactDeltas.size() + " rawContactDelta(s)");
578 for (int j = 0; j < mRawContactDeltas.size(); j++) {
579 final RawContactDelta rawContactDelta = mRawContactDeltas.get(j);
Walter Jang192a01c2015-09-22 15:23:55 -0700580 vlog("parse: " + j + " rawContactDelta" + rawContactDelta);
Walter Jang363d3fd2015-09-16 10:29:07 -0700581 if (rawContactDelta == null || !rawContactDelta.isVisible()) continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800582 final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
Walter Jang363d3fd2015-09-16 10:29:07 -0700583 if (accountType == null) continue;
Gary Mai4ceabed2016-09-16 12:14:13 -0700584
585 if (mRawContactIdToDisplayAlone > 0) {
586 // Look for the raw contact if specified.
587 if (rawContactDelta.getRawContactId().equals(mRawContactIdToDisplayAlone)) {
588 mCurrentRawContactDelta = rawContactDelta;
589 return;
Walter Jang192a01c2015-09-22 15:23:55 -0700590 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700591 } else if (mPrimaryAccount != null
592 && mPrimaryAccount.equals(rawContactDelta.getAccountWithDataSet())) {
593 // Otherwise try to find the one that matches the default.
594 mCurrentRawContactDelta = rawContactDelta;
595 return;
596 } else if (accountType.areContactsWritable()){
597 // TODO: Find better raw contact delta
598 // Just select an arbitrary writable contact.
599 mCurrentRawContactDelta = rawContactDelta;
Walter Jangac679af2015-06-01 12:17:06 -0700600 }
601 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700602
603 }
604
605 private void parseRawContactDelta() {
606 mKindSectionDataMap.clear();
607 mSortedMimetypes.clear();
608
609 final AccountType accountType = mCurrentRawContactDelta.getAccountType(mAccountTypeManager);
610 final List<DataKind> dataKinds = accountType.getSortedDataKinds();
611 final int dataKindSize = dataKinds == null ? 0 : dataKinds.size();
612 vlog("parse: " + dataKindSize + " dataKinds(s)");
613
614 for (int i = 0; i < dataKindSize; i++) {
615 final DataKind dataKind = dataKinds.get(i);
616 if (dataKind == null) {
617 vlog("parse: " + i + " " + dataKind.mimeType + " dropped null data kind");
618 continue;
619 }
620 final String mimeType = dataKind.mimeType;
621
622 // Skip psuedo mime types
623 if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
624 || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
625 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
626 continue;
627 }
628
629 // Skip custom fields
630 // TODO: Handle them when we implement editing custom fields.
631 if (CustomDataItem.MIMETYPE_CUSTOM_FIELD.equals(mimeType)) {
632 vlog("parse: " + i + " " + dataKind.mimeType + " dropped custom field");
633 continue;
634 }
635
636 final KindSectionData kindSectionData =
637 new KindSectionData(accountType, dataKind, mCurrentRawContactDelta);
638 mKindSectionDataMap.put(mimeType, kindSectionData);
639 mSortedMimetypes.add(mimeType);
640
641 vlog("parse: " + i + " " + dataKind.mimeType + " " +
642 kindSectionData.getValuesDeltas().size() + " value(s) " +
643 kindSectionData.getNonEmptyValuesDeltas().size() + " non-empty value(s) " +
644 kindSectionData.getVisibleValuesDeltas().size() +
645 " visible value(s)");
646 }
Walter Jang3efae4a2015-02-18 11:12:00 -0800647 }
648
Walter Jang9a552372016-08-24 11:51:05 -0700649 private void addReadOnlyRawContactEditorViews() {
Gary Mai65971d02016-09-15 15:00:16 -0700650 mKindSectionViews.removeAllViews();
Gary Mai4ceabed2016-09-16 12:14:13 -0700651 addAccountInfo();
Walter Jang9a552372016-08-24 11:51:05 -0700652 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
653 getContext());
Gary Mai4ceabed2016-09-16 12:14:13 -0700654 final AccountType type = mCurrentRawContactDelta.getAccountType(accountTypes);
Walter Jang9a552372016-08-24 11:51:05 -0700655
Gary Mai65971d02016-09-15 15:00:16 -0700656 // Bail if invalid state or source
Gary Mai4ceabed2016-09-16 12:14:13 -0700657 if (type == null) return;
Walter Jang9a552372016-08-24 11:51:05 -0700658
Gary Mai65971d02016-09-15 15:00:16 -0700659 // Make sure we have StructuredName
Gary Mai4ceabed2016-09-16 12:14:13 -0700660 RawContactModifier.ensureKindExists(
661 mCurrentRawContactDelta, type, StructuredName.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700662
663 ValuesDelta primary;
664
665 // Name
666 final Context context = getContext();
667 final Resources res = context.getResources();
Gary Mai4ceabed2016-09-16 12:14:13 -0700668 primary = mCurrentRawContactDelta.getPrimaryEntry(StructuredName.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700669 final String name = primary != null ? primary.getAsString(StructuredName.DISPLAY_NAME) :
670 getContext().getString(R.string.missing_name);
671 final Drawable nameDrawable = context.getDrawable(R.drawable.ic_person_24dp);
672 final String nameContentDescription = res.getString(R.string.header_name_entry);
673 bindData(nameDrawable, nameContentDescription, name, /* type */ null,
674 /* isFirstEntry */ true);
675
676 // Phones
Gary Mai4ceabed2016-09-16 12:14:13 -0700677 final ArrayList<ValuesDelta> phones = mCurrentRawContactDelta
678 .getMimeEntries(Phone.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700679 final Drawable phoneDrawable = context.getDrawable(R.drawable.ic_phone_24dp);
680 final String phoneContentDescription = res.getString(R.string.header_phone_entry);
681 if (phones != null) {
682 boolean isFirstPhoneBound = true;
683 for (ValuesDelta phone : phones) {
684 final String phoneNumber = phone.getPhoneNumber();
685 if (TextUtils.isEmpty(phoneNumber)) {
686 continue;
687 }
688 final String formattedNumber = PhoneNumberUtilsCompat.formatNumber(
689 phoneNumber, phone.getPhoneNormalizedNumber(),
690 GeoUtil.getCurrentCountryIso(getContext()));
691 CharSequence phoneType = null;
692 if (phone.hasPhoneType()) {
693 phoneType = Phone.getTypeLabel(
694 res, phone.getPhoneType(), phone.getPhoneLabel());
695 }
696 bindData(phoneDrawable, phoneContentDescription, formattedNumber, phoneType,
697 isFirstPhoneBound, true);
698 isFirstPhoneBound = false;
699 }
700 }
701
702 // Emails
Gary Mai4ceabed2016-09-16 12:14:13 -0700703 final ArrayList<ValuesDelta> emails = mCurrentRawContactDelta
704 .getMimeEntries(Email.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700705 final Drawable emailDrawable = context.getDrawable(R.drawable.ic_email_24dp);
706 final String emailContentDescription = res.getString(R.string.header_email_entry);
707 if (emails != null) {
708 boolean isFirstEmailBound = true;
709 for (ValuesDelta email : emails) {
710 final String emailAddress = email.getEmailData();
711 if (TextUtils.isEmpty(emailAddress)) {
712 continue;
713 }
714 CharSequence emailType = null;
715 if (email.hasEmailType()) {
716 emailType = Email.getTypeLabel(
717 res, email.getEmailType(), email.getEmailLabel());
718 }
719 bindData(emailDrawable, emailContentDescription, emailAddress, emailType,
720 isFirstEmailBound);
721 isFirstEmailBound = false;
722 }
723 }
724
725 mKindSectionViews.setVisibility(mKindSectionViews.getChildCount() > 0 ? VISIBLE : GONE);
726 // Hide the "More fields" link
727 mMoreFields.setVisibility(GONE);
728 }
729
Gary Mai65971d02016-09-15 15:00:16 -0700730 private void bindData(Drawable icon, String iconContentDescription, CharSequence data,
731 CharSequence type, boolean isFirstEntry) {
732 bindData(icon, iconContentDescription, data, type, isFirstEntry, false);
733 }
734
735 private void bindData(Drawable icon, String iconContentDescription, CharSequence data,
736 CharSequence type, boolean isFirstEntry, boolean forceLTR) {
737 final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
738 Context.LAYOUT_INFLATER_SERVICE);
739 final View field = inflater.inflate(R.layout.item_read_only_field, mKindSectionViews,
740 false);
741 if (isFirstEntry) {
742 final ImageView imageView = (ImageView) field.findViewById(R.id.kind_icon);
743 imageView.setImageDrawable(icon);
744 imageView.setContentDescription(iconContentDescription);
745 } else {
746 final ImageView imageView = (ImageView) field.findViewById(R.id.kind_icon);
747 imageView.setVisibility(View.INVISIBLE);
748 imageView.setContentDescription(null);
749 }
750 final TextView dataView = (TextView) field.findViewById(R.id.data);
751 dataView.setText(data);
752 if (forceLTR) {
753 dataView.setTextDirection(View.TEXT_DIRECTION_LTR);
754 }
755 final TextView typeView = (TextView) field.findViewById(R.id.type);
756 if (!TextUtils.isEmpty(type)) {
757 typeView.setText(type);
758 } else {
759 typeView.setVisibility(View.GONE);
760 }
761 mKindSectionViews.addView(field);
Walter Jang9a552372016-08-24 11:51:05 -0700762 }
763
Gary Mai98868d32016-09-14 11:55:04 -0700764 private void addAccountInfo() {
Walter Jang82acd422015-10-17 14:01:27 -0700765 mAccountHeaderContainer.setVisibility(View.GONE);
Walter Jang708ea9e2015-09-10 15:42:05 -0700766
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700767 final AccountDisplayInfo account =
Gary Mai4ceabed2016-09-16 12:14:13 -0700768 mAccountDisplayInfoFactory.getAccountDisplayInfoFor(mCurrentRawContactDelta);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700769
Walter Jang708ea9e2015-09-10 15:42:05 -0700770 // Get the account information for the primary raw contact delta
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700771 final String accountLabel = mIsUserProfile
772 ? EditorUiUtils.getAccountHeaderLabelForMyProfile(getContext(), account)
773 : account.getNameLabel().toString();
Walter Jang708ea9e2015-09-10 15:42:05 -0700774
Walter Jang23709542015-10-22 12:50:58 -0700775 // Either the account header or selector should be shown, not both.
Walter Jang708ea9e2015-09-10 15:42:05 -0700776 final List<AccountWithDataSet> accounts =
777 AccountTypeManager.getInstance(getContext()).getAccounts(true);
Gary Mai4ceabed2016-09-16 12:14:13 -0700778
Walter Jang82acd422015-10-17 14:01:27 -0700779 if (mHasNewContact && !mIsUserProfile) {
780 if (accounts.size() > 1) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700781 addAccountSelector(mCurrentRawContactDelta, accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700782 } else {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700783 addAccountHeader(accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700784 }
Gary Maida20b472016-09-20 14:46:40 -0700785 } else {
786 // The raw contact selector should only display linked raw contacts that can be edited
787 // in the full editor (i.e. they are not newly created raw contacts)
788 final RawContactAccountListAdapter adapter = new RawContactAccountListAdapter(
789 getContext(), getRawContactDeltaListForSelector(mRawContactDeltas));
790 if (adapter.getCount() > 0 && !mIsEditingReadOnlyRawContactWithNewContact) {
791 addRawContactAccountSelector(accountLabel, adapter);
792 } else {
793 addAccountHeader(accountLabel);
794 }
Walter Jang23709542015-10-22 12:50:58 -0700795 }
Walter Jang708ea9e2015-09-10 15:42:05 -0700796 }
797
Tingting Wang2bb85d22015-10-23 13:01:09 -0700798 private RawContactDeltaList getRawContactDeltaListForSelector(
799 RawContactDeltaList rawContactDeltas) {
800 // Sort raw contacts so google accounts come first
801 Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
802
803 final RawContactDeltaList result = new RawContactDeltaList();
Gary Mai98868d32016-09-14 11:55:04 -0700804 for (int i = 0; i < rawContactDeltas.size(); i++) {
805 final RawContactDelta rawContactDelta = rawContactDeltas.get(i);
Tingting Wang2bb85d22015-10-23 13:01:09 -0700806 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
807 // Only add raw contacts that can be opened in the editor
808 result.add(rawContactDelta);
809 }
810 }
811 // Don't return a list of size 1 that would just open the raw contact being edited
812 // in the compact editor in the full editor
813 if (result.size() == 1 && result.get(0).getRawContactAccountType(
814 getContext()).areContactsWritable()) {
815 result.clear();
816 return result;
817 }
818 return result;
819 }
820
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700821 private void addAccountHeader(String accountLabel) {
Walter Jang82acd422015-10-17 14:01:27 -0700822 mAccountHeaderContainer.setVisibility(View.VISIBLE);
823
Walter Jangbe5e1b12015-10-17 11:38:29 -0700824 // Set the account name
Walter Jangbe5e1b12015-10-17 11:38:29 -0700825 mAccountHeaderName.setVisibility(View.VISIBLE);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700826 mAccountHeaderName.setText(accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700827
828 // Set the account type
Gary Mai4ceabed2016-09-16 12:14:13 -0700829 final String selectorTitle = getResources().getString(isReadOnlyRawContact() ?
830 R.string.compact_editor_account_selector_read_only_title :
Walter Jangbe5e1b12015-10-17 11:38:29 -0700831 R.string.compact_editor_account_selector_title);
832 mAccountHeaderType.setText(selectorTitle);
833
834 // Set the icon
Gary Mai4ceabed2016-09-16 12:14:13 -0700835 final AccountType accountType =
836 mCurrentRawContactDelta.getRawContactAccountType(getContext());
837 mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext()));
Walter Jangbe5e1b12015-10-17 11:38:29 -0700838
839 // Set the content description
840 mAccountHeaderContainer.setContentDescription(
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700841 EditorUiUtils.getAccountInfoContentDescription(accountLabel,
842 selectorTitle));
Walter Jangbe5e1b12015-10-17 11:38:29 -0700843 }
844
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700845 private void addAccountSelector(final RawContactDelta rawContactDelta, CharSequence nameLabel) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700846 final View.OnClickListener onClickListener = new View.OnClickListener() {
Walter Jang708ea9e2015-09-10 15:42:05 -0700847 @Override
848 public void onClick(View v) {
849 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
850 final AccountsListAdapter adapter =
851 new AccountsListAdapter(getContext(),
852 AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE,
853 mPrimaryAccount);
Gary Mai46cb3102016-08-10 18:14:09 -0700854 popup.setWidth(mAccountHeaderContainer.getWidth());
855 popup.setAnchorView(mAccountHeaderContainer);
Walter Jang708ea9e2015-09-10 15:42:05 -0700856 popup.setAdapter(adapter);
857 popup.setModal(true);
858 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
859 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
860 @Override
861 public void onItemClick(AdapterView<?> parent, View view, int position,
862 long id) {
863 UiClosables.closeQuietly(popup);
864 final AccountWithDataSet newAccount = adapter.getItem(position);
865 if (mListener != null && !mPrimaryAccount.equals(newAccount)) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700866 mIsExpanded = false;
Walter Jang708ea9e2015-09-10 15:42:05 -0700867 mListener.onRebindEditorsForNewContact(
Walter Jang82acd422015-10-17 14:01:27 -0700868 rawContactDelta,
Walter Jang708ea9e2015-09-10 15:42:05 -0700869 mPrimaryAccount,
870 newAccount);
871 }
872 }
873 });
874 popup.show();
875 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700876 };
877 setUpAccountSelector(nameLabel.toString(), onClickListener);
Walter Jang708ea9e2015-09-10 15:42:05 -0700878 }
879
Gary Mai4ceabed2016-09-16 12:14:13 -0700880 private void addRawContactAccountSelector(String nameLabel,
Walter Jang23709542015-10-22 12:50:58 -0700881 final RawContactAccountListAdapter adapter) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700882 final View.OnClickListener onClickListener = new OnClickListener() {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700883 @Override
884 public void onClick(View v) {
885 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
Gary Mai4ceabed2016-09-16 12:14:13 -0700886 popup.setWidth(mAccountHeaderContainer.getWidth());
887 popup.setAnchorView(mAccountHeaderContainer);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700888 popup.setAdapter(adapter);
889 popup.setModal(true);
890 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
891 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
892 @Override
893 public void onItemClick(AdapterView<?> parent, View view, int position,
894 long id) {
895 UiClosables.closeQuietly(popup);
Gary Mai4ceabed2016-09-16 12:14:13 -0700896 final long rawContactId = adapter.getItemId(position);
897 // Only switch if it's actually a different raw contact.
898 if (rawContactId != mCurrentRawContactDelta.getRawContactId()
899 && mListener != null) {
Tingting Wang91cee282015-10-07 13:48:17 -0700900 final RawContactDelta rawContactDelta = adapter.getItem(position);
901 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
902 getContext());
903 final AccountType accountType = rawContactDelta.getAccountType(
904 accountTypes);
905 final boolean isReadOnly = !accountType.areContactsWritable();
Gary Mai4ceabed2016-09-16 12:14:13 -0700906 // Reset state.
907 mIsExpanded = false;
908 mListener.onRawContactSelected(rawContactId, isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700909 }
910 }
911 });
912 popup.show();
913 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700914 };
915 setUpAccountSelector(nameLabel, onClickListener);
916 }
917
918 private void setUpAccountSelector(String nameLabel, OnClickListener listener) {
919 addAccountHeader(nameLabel);
920 // Add handlers for choosing another account to save to.
921 mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE);
922 mAccountHeaderContainer.setOnClickListener(listener);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700923 }
924
Walter Jang363d3fd2015-09-16 10:29:07 -0700925 private void addPhotoView() {
Gary Mai4ceabed2016-09-16 12:14:13 -0700926 if (!mCurrentRawContactDelta.hasMimeEntries(Photo.CONTENT_ITEM_TYPE)) {
927 wlog("No photo mimetype for this raw contact.");
Gary Maida20b472016-09-20 14:46:40 -0700928 mPhotoView.setVisibility(GONE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700929 return;
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700930 } else {
Gary Maida20b472016-09-20 14:46:40 -0700931 mPhotoView.setVisibility(VISIBLE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700932 }
Walter Jang06f73a12015-06-17 11:15:48 -0700933
Gary Mai4ceabed2016-09-16 12:14:13 -0700934 final ValuesDelta superPrimaryDelta = mCurrentRawContactDelta
935 .getSuperPrimaryEntry(Photo.CONTENT_ITEM_TYPE);
Gary Maida20b472016-09-20 14:46:40 -0700936 if (superPrimaryDelta == null) {
937 Log.wtf(TAG, "addPhotoView: no ValueDelta found for current RawContactDelta"
938 + "that supports a photo.");
939 mPhotoView.setVisibility(GONE);
940 return;
941 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700942 // Set the photo view
Gary Mai4ceabed2016-09-16 12:14:13 -0700943 mPhotoView.setPhoto(superPrimaryDelta, mMaterialPalette);
Walter Jang31a74ad2015-10-02 19:17:39 -0700944
Gary Mai4ceabed2016-09-16 12:14:13 -0700945 if (isReadOnlyRawContact()) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700946 mPhotoView.setReadOnly(true);
947 return;
948 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700949 mPhotoView.setReadOnly(false);
Gary Mai4ceabed2016-09-16 12:14:13 -0700950 mPhotoValuesDelta = superPrimaryDelta;
Walter Jang31a74ad2015-10-02 19:17:39 -0700951 }
952
Walter Jangf5dfea42015-09-16 12:30:36 -0700953 private void addKindSectionViews() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700954 int i = -1;
Gary Mai98868d32016-09-14 11:55:04 -0700955
956 for (String mimeType : mSortedMimetypes) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700957 i++;
Gary Mai4ceabed2016-09-16 12:14:13 -0700958 // Ignore mime types that we've already handled
959 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
960 vlog("kind: " + i + " " + mimeType + " dropped");
961 continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800962 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700963 final CompactKindSectionView kindSectionView;
964 final KindSectionData kindSectionData = mKindSectionDataMap.get(mimeType);
965 final ValuesDelta primaryDelta = mCurrentRawContactDelta.getPrimaryEntry(mimeType);
966 kindSectionView = inflateKindSectionView(mKindSectionViews, kindSectionData, mimeType,
967 primaryDelta);
Gary Mai98868d32016-09-14 11:55:04 -0700968 mKindSectionViews.addView(kindSectionView);
Walter Jangcab3dce2015-02-09 17:48:03 -0800969
Gary Mai98868d32016-09-14 11:55:04 -0700970 // Keep a pointer to the KindSectionView for each mimeType
971 mKindSectionViewMap.put(mimeType, kindSectionView);
Walter Jangf10ca152015-09-22 15:23:55 -0700972 }
Walter Jangf10ca152015-09-22 15:23:55 -0700973 }
974
Walter Jangf5dfea42015-09-16 12:30:36 -0700975 private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup,
Gary Mai98868d32016-09-14 11:55:04 -0700976 KindSectionData kindSectionData, String mimeType,
Walter Jang82acd422015-10-17 14:01:27 -0700977 ValuesDelta primaryValuesDelta) {
Walter Jangf5dfea42015-09-16 12:30:36 -0700978 final CompactKindSectionView kindSectionView = (CompactKindSectionView)
979 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
980 /* attachToRoot =*/ false);
Walter Jang45b86d52015-10-15 15:23:16 -0700981 kindSectionView.setIsUserProfile(mIsUserProfile);
Walter Jangf5dfea42015-09-16 12:30:36 -0700982
Walter Jang192a01c2015-09-22 15:23:55 -0700983 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Walter Jangf5dfea42015-09-16 12:30:36 -0700984 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -0700985 // Phone numbers and email addresses are always displayed,
986 // even if they are empty
Walter Jangf5dfea42015-09-16 12:30:36 -0700987 kindSectionView.setHideWhenEmpty(false);
988 }
Walter Jangf5dfea42015-09-16 12:30:36 -0700989
Walter Jang192a01c2015-09-22 15:23:55 -0700990 // Since phone numbers and email addresses displayed even if they are empty,
991 // they will be the only types you add new values to initially for new contacts
992 kindSectionView.setShowOneEmptyEditor(true);
993
Gary Mai98868d32016-09-14 11:55:04 -0700994 kindSectionView.setState(kindSectionData, mViewIdGenerator, mListener,
Walter Jang82acd422015-10-17 14:01:27 -0700995 primaryValuesDelta);
Walter Jangf5dfea42015-09-16 12:30:36 -0700996
997 return kindSectionView;
Walter Jangcab3dce2015-02-09 17:48:03 -0800998 }
999
Walter Jange3945952015-10-27 12:44:54 -07001000 void maybeSetReadOnlyDisplayNameAsPrimary(String readOnlyDisplayName) {
1001 if (TextUtils.isEmpty(readOnlyDisplayName)) return;
1002 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
1003 if (primaryNameKindSectionView != null && primaryNameKindSectionView.isEmptyName()) {
1004 vlog("name: using read only display name as primary name");
1005 primaryNameKindSectionView.setName(readOnlyDisplayName);
1006 }
1007 }
1008
1009 private CompactKindSectionView getPrimaryNameKindSectionView() {
Gary Mai98868d32016-09-14 11:55:04 -07001010 return mKindSectionViewMap.get(StructuredName.CONTENT_ITEM_TYPE);
Walter Jang4f5594a2015-10-06 18:40:31 -07001011 }
1012
Walter Jangd6753152015-10-02 09:23:13 -07001013 private void showAllFields() {
Walter Jang79658e12015-09-24 10:36:26 -07001014 // Stop hiding empty editors and allow the user to enter values for all kinds now
1015 for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
1016 final CompactKindSectionView kindSectionView =
1017 (CompactKindSectionView) mKindSectionViews.getChildAt(i);
1018 kindSectionView.setHideWhenEmpty(false);
1019 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
1020 }
1021 mIsExpanded = true;
Walter Jang79658e12015-09-24 10:36:26 -07001022
Walter Jangf5dfea42015-09-16 12:30:36 -07001023 // Hide the more fields button
1024 mMoreFields.setVisibility(View.GONE);
Walter Jangcab3dce2015-02-09 17:48:03 -08001025 }
1026
Marcus Hagerottcf7f2952016-09-06 13:49:17 -07001027 private boolean hasMoreFields() {
Gary Mai98868d32016-09-14 11:55:04 -07001028 for (CompactKindSectionView section : mKindSectionViewMap.values()) {
1029 if (section.getVisibility() != View.VISIBLE) {
1030 return true;
Marcus Hagerottcf7f2952016-09-06 13:49:17 -07001031 }
1032 }
1033 return false;
1034 }
1035
Walter Jangbf63a6d2015-05-05 09:14:35 -07001036 private static void vlog(String message) {
1037 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1038 Log.v(TAG, message);
Walter Jangcab3dce2015-02-09 17:48:03 -08001039 }
1040 }
Walter Jang363d3fd2015-09-16 10:29:07 -07001041
1042 private static void wlog(String message) {
1043 if (Log.isLoggable(TAG, Log.WARN)) {
1044 Log.w(TAG, message);
1045 }
1046 }
1047
1048 private static void elog(String message) {
1049 Log.e(TAG, message);
1050 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001051}