blob: f41b33eef766629d962abc098ad2d947a8e05b2e [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);
Gary Mai18724182016-09-20 17:33:30 -0700387 mPhotoValuesDelta.put(Photo.PHOTO_FILE_ID, (String) null);
Walter Jang31a74ad2015-10-02 19:17:39 -0700388
389 mPhotoView.removePhoto();
Walter Jang3efae4a2015-02-18 11:12:00 -0800390 }
391
392 /**
Walter Jang41b3ea12015-03-09 17:30:06 -0700393 * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}.
394 */
395 public void setFullSizePhoto(Uri photoUri) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700396 mPhotoView.setFullSizedPhoto(photoUri);
Walter Jang41b3ea12015-03-09 17:30:06 -0700397 }
398
Walter Jang31a74ad2015-10-02 19:17:39 -0700399 public void updatePhoto(Uri photoUri) {
Walter Jang45b86d52015-10-15 15:23:16 -0700400 mPhotoValuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700401 // Unset primary for all photos
Wenyi Wang9086fb92015-11-16 09:58:20 -0800402 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700403 // Mark the currently displayed photo as primary
404 mPhotoValuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700405
Wenyi Wang4c9cf3a2015-10-15 17:58:23 -0700406 // Even though high-res photos cannot be saved by passing them via
407 // an EntityDeltaList (since they cause the Bundle size limit to be
408 // exceeded), we still pass a low-res thumbnail. This simplifies
409 // code all over the place, because we don't have to test whether
410 // there is a change in EITHER the delta-list OR a changed photo...
411 // this way, there is always a change in the delta-list.
412 try {
413 final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
414 getContext(), photoUri);
415 if (bytes != null) {
416 mPhotoValuesDelta.setPhoto(bytes);
417 }
418 } catch (FileNotFoundException e) {
419 elog("Failed to get bitmap from photo Uri");
420 }
421
Walter Jang31a74ad2015-10-02 19:17:39 -0700422 mPhotoView.setFullSizedPhoto(photoUri);
423 }
424
Wenyi Wang9086fb92015-11-16 09:58:20 -0800425 private void unsetSuperPrimaryFromAllPhotos() {
Gary Mai4ceabed2016-09-16 12:14:13 -0700426 for (int i = 0; i < mRawContactDeltas.size(); i++) {
427 final RawContactDelta rawContactDelta = mRawContactDeltas.get(i);
428 if (!rawContactDelta.hasMimeEntries(Photo.CONTENT_ITEM_TYPE)) {
429 continue;
430 }
431 final List<ValuesDelta> photosDeltas =
432 mRawContactDeltas.get(i).getMimeEntries(Photo.CONTENT_ITEM_TYPE);
433 if (photosDeltas == null) {
434 continue;
435 }
436 for (int j = 0; j < photosDeltas.size(); j++) {
437 photosDeltas.get(j).setSuperPrimary(false);
Walter Jang3f18d612015-10-07 16:01:05 -0700438 }
439 }
440 }
441
Walter Jang41b3ea12015-03-09 17:30:06 -0700442 /**
Walter Janga5e4bb22015-02-24 13:08:16 -0800443 * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800444 */
445 public boolean isWritablePhotoSet() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700446 return mPhotoView.isWritablePhotoSet();
Walter Jang3efae4a2015-02-18 11:12:00 -0800447 }
448
449 /**
Walter Jang3efae4a2015-02-18 11:12:00 -0800450 * Get the raw contact ID for the CompactHeaderView photo.
451 */
Walter Jang3efae4a2015-02-18 11:12:00 -0800452 public long getPhotoRawContactId() {
Gary Maida20b472016-09-20 14:46:40 -0700453 return mCurrentRawContactDelta.getRawContactId();
Walter Jang3efae4a2015-02-18 11:12:00 -0800454 }
455
Gary Mai698cee72016-09-19 16:09:54 -0700456 public StructuredNameEditorView getNameEditorView() {
457 final CompactKindSectionView nameKindSectionView = mKindSectionViewMap
458 .get(StructuredName.CONTENT_ITEM_TYPE);
459 return nameKindSectionView == null
460 ? null : nameKindSectionView.getNameEditorView();
Walter Jang4f5594a2015-10-06 18:40:31 -0700461 }
462
Gary Mai220d10c2016-09-23 13:56:39 -0700463 public RawContactDelta getCurrentRawContactDelta() {
464 return mCurrentRawContactDelta;
465 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700466
467 /**
Gary Maida20b472016-09-20 14:46:40 -0700468 * Marks the raw contact photo given as primary for the aggregate contact.
Walter Jang31a74ad2015-10-02 19:17:39 -0700469 */
Gary Maida20b472016-09-20 14:46:40 -0700470 public void setPrimaryPhoto() {
Walter Jangda258ff2015-11-17 11:06:02 -0800471
472 // Update values delta
Gary Mai4ceabed2016-09-16 12:14:13 -0700473 final ValuesDelta valuesDelta = mCurrentRawContactDelta
474 .getSuperPrimaryEntry(Photo.CONTENT_ITEM_TYPE);
Gary Maida20b472016-09-20 14:46:40 -0700475 if (valuesDelta == null) {
476 Log.wtf(TAG, "setPrimaryPhoto: had no ValuesDelta for the current RawContactDelta");
477 return;
478 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700479 valuesDelta.setFromTemplate(false);
Wenyi Wang9086fb92015-11-16 09:58:20 -0800480 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700481 valuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700482 }
483
Walter Jangd35e5ef2015-02-24 09:18:16 -0800484 public View getAggregationAnchorView() {
Gary Mai698cee72016-09-19 16:09:54 -0700485 final StructuredNameEditorView nameEditorView = getNameEditorView();
Gary Mai710802c2016-09-19 15:05:44 -0700486 return nameEditorView != null ? nameEditorView.findViewById(R.id.anchor_view) : null;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800487 }
488
Walter Jangf10ca152015-09-22 15:23:55 -0700489 public void setGroupMetaData(Cursor groupMetaData) {
Gary Mai98868d32016-09-14 11:55:04 -0700490 final CompactKindSectionView groupKindSectionView =
491 mKindSectionViewMap.get(GroupMembership.CONTENT_ITEM_TYPE);
492 if (groupKindSectionView == null) {
493 return;
494 }
495 groupKindSectionView.setGroupMetaData(groupMetaData);
496 if (mIsExpanded) {
497 groupKindSectionView.setHideWhenEmpty(false);
498 groupKindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
Walter Jangf10ca152015-09-22 15:23:55 -0700499 }
500 }
501
Walter Jangf46abd82015-02-20 16:52:04 -0800502 public void setState(RawContactDeltaList rawContactDeltas,
Walter Jang06f73a12015-06-17 11:15:48 -0700503 MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
Gary Maida20b472016-09-20 14:46:40 -0700504 boolean hasNewContact, boolean isUserProfile, AccountWithDataSet primaryAccount,
505 long rawContactIdToDisplayAlone, boolean isEditingReadOnlyRawContactWithNewContact) {
Marcus Hagerottf04c9752016-09-08 15:22:57 -0700506
Walter Jang9a552372016-08-24 11:51:05 -0700507 mRawContactDeltas = rawContactDeltas;
508 mRawContactIdToDisplayAlone = rawContactIdToDisplayAlone;
Walter Jang9a552372016-08-24 11:51:05 -0700509 mIsEditingReadOnlyRawContactWithNewContact = isEditingReadOnlyRawContactWithNewContact;
510
Gary Mai98868d32016-09-14 11:55:04 -0700511 mKindSectionViewMap.clear();
Walter Jangf5dfea42015-09-16 12:30:36 -0700512 mKindSectionViews.removeAllViews();
Walter Jang363d3fd2015-09-16 10:29:07 -0700513 mMoreFields.setVisibility(View.VISIBLE);
Walter Jangcab3dce2015-02-09 17:48:03 -0800514
Walter Jangf46abd82015-02-20 16:52:04 -0800515 mMaterialPalette = materialPalette;
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700516 mViewIdGenerator = viewIdGenerator;
Tingting Wangf695eb32015-10-15 18:45:15 -0700517
Walter Jang708ea9e2015-09-10 15:42:05 -0700518 mHasNewContact = hasNewContact;
519 mIsUserProfile = isUserProfile;
520 mPrimaryAccount = primaryAccount;
521 if (mPrimaryAccount == null) {
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700522 mPrimaryAccount = ContactEditorUtils.create(getContext()).getOnlyOrDefaultAccount();
Walter Jang708ea9e2015-09-10 15:42:05 -0700523 }
524 vlog("state: primary " + mPrimaryAccount);
Walter Jangcab3dce2015-02-09 17:48:03 -0800525
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700526 // Parse the given raw contact deltas
527 if (rawContactDeltas == null || rawContactDeltas.isEmpty()) {
528 elog("No raw contact deltas");
Walter Jang79658e12015-09-24 10:36:26 -0700529 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jangf5dfea42015-09-16 12:30:36 -0700530 return;
531 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700532 pickRawContactDelta();
533 parseRawContactDelta();
Walter Jang79658e12015-09-24 10:36:26 -0700534 if (mKindSectionDataMap.isEmpty()) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700535 elog("No kind section data parsed from RawContactDelta(s)");
Walter Jang79658e12015-09-24 10:36:26 -0700536 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700537 return;
538 }
Walter Jang82acd422015-10-17 14:01:27 -0700539
Gary Mai98868d32016-09-14 11:55:04 -0700540 final KindSectionData nameSectionData =
541 mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE);
542 // Ensure that a structured name and photo exists
543 if (nameSectionData != null) {
Walter Jang82acd422015-10-17 14:01:27 -0700544 final RawContactDelta rawContactDelta =
Gary Mai98868d32016-09-14 11:55:04 -0700545 nameSectionData.getRawContactDelta();
Walter Jang82acd422015-10-17 14:01:27 -0700546 RawContactModifier.ensureKindExists(
547 rawContactDelta,
548 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700549 StructuredName.CONTENT_ITEM_TYPE);
Walter Jang82acd422015-10-17 14:01:27 -0700550 RawContactModifier.ensureKindExists(
551 rawContactDelta,
552 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700553 Photo.CONTENT_ITEM_TYPE);
554 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700555
556 // Setup the view
Walter Jang363d3fd2015-09-16 10:29:07 -0700557 addPhotoView();
Gary Mai4ceabed2016-09-16 12:14:13 -0700558 if (isReadOnlyRawContact()) {
Walter Jang9a552372016-08-24 11:51:05 -0700559 // We're want to display the inputs fields for a single read only raw contact
560 addReadOnlyRawContactEditorViews();
Walter Jang9a552372016-08-24 11:51:05 -0700561 } else {
562 setupCompactEditorNormally();
563 }
Walter Jangd6753152015-10-02 09:23:13 -0700564 if (mListener != null) mListener.onEditorsBound();
Walter Jangcab3dce2015-02-09 17:48:03 -0800565 }
566
Walter Jang9a552372016-08-24 11:51:05 -0700567 private void setupCompactEditorNormally() {
Gary Mai98868d32016-09-14 11:55:04 -0700568 addAccountInfo();
Walter Jang9a552372016-08-24 11:51:05 -0700569 addKindSectionViews();
Walter Jangcab3dce2015-02-09 17:48:03 -0800570
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700571 mMoreFields.setVisibility(hasMoreFields() ? View.VISIBLE : View.GONE);
572
Walter Jang9a552372016-08-24 11:51:05 -0700573 if (mIsExpanded) showAllFields();
574 }
575
Gary Mai4ceabed2016-09-16 12:14:13 -0700576 private boolean isReadOnlyRawContact() {
577 return !mCurrentRawContactDelta.getAccountType(mAccountTypeManager).areContactsWritable();
Walter Jang9a552372016-08-24 11:51:05 -0700578 }
579
Gary Mai4ceabed2016-09-16 12:14:13 -0700580 private void pickRawContactDelta() {
Walter Jang363d3fd2015-09-16 10:29:07 -0700581 // Build the kind section data list map
Gary Mai4ceabed2016-09-16 12:14:13 -0700582 vlog("parse: " + mRawContactDeltas.size() + " rawContactDelta(s)");
583 for (int j = 0; j < mRawContactDeltas.size(); j++) {
584 final RawContactDelta rawContactDelta = mRawContactDeltas.get(j);
Walter Jang192a01c2015-09-22 15:23:55 -0700585 vlog("parse: " + j + " rawContactDelta" + rawContactDelta);
Walter Jang363d3fd2015-09-16 10:29:07 -0700586 if (rawContactDelta == null || !rawContactDelta.isVisible()) continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800587 final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
Walter Jang363d3fd2015-09-16 10:29:07 -0700588 if (accountType == null) continue;
Gary Mai4ceabed2016-09-16 12:14:13 -0700589
590 if (mRawContactIdToDisplayAlone > 0) {
591 // Look for the raw contact if specified.
592 if (rawContactDelta.getRawContactId().equals(mRawContactIdToDisplayAlone)) {
593 mCurrentRawContactDelta = rawContactDelta;
594 return;
Walter Jang192a01c2015-09-22 15:23:55 -0700595 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700596 } else if (mPrimaryAccount != null
597 && mPrimaryAccount.equals(rawContactDelta.getAccountWithDataSet())) {
598 // Otherwise try to find the one that matches the default.
599 mCurrentRawContactDelta = rawContactDelta;
600 return;
601 } else if (accountType.areContactsWritable()){
602 // TODO: Find better raw contact delta
603 // Just select an arbitrary writable contact.
604 mCurrentRawContactDelta = rawContactDelta;
Walter Jangac679af2015-06-01 12:17:06 -0700605 }
606 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700607
608 }
609
610 private void parseRawContactDelta() {
611 mKindSectionDataMap.clear();
612 mSortedMimetypes.clear();
613
614 final AccountType accountType = mCurrentRawContactDelta.getAccountType(mAccountTypeManager);
615 final List<DataKind> dataKinds = accountType.getSortedDataKinds();
616 final int dataKindSize = dataKinds == null ? 0 : dataKinds.size();
617 vlog("parse: " + dataKindSize + " dataKinds(s)");
618
619 for (int i = 0; i < dataKindSize; i++) {
620 final DataKind dataKind = dataKinds.get(i);
621 if (dataKind == null) {
622 vlog("parse: " + i + " " + dataKind.mimeType + " dropped null data kind");
623 continue;
624 }
625 final String mimeType = dataKind.mimeType;
626
627 // Skip psuedo mime types
628 if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
629 || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
630 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
631 continue;
632 }
633
634 // Skip custom fields
635 // TODO: Handle them when we implement editing custom fields.
636 if (CustomDataItem.MIMETYPE_CUSTOM_FIELD.equals(mimeType)) {
637 vlog("parse: " + i + " " + dataKind.mimeType + " dropped custom field");
638 continue;
639 }
640
641 final KindSectionData kindSectionData =
642 new KindSectionData(accountType, dataKind, mCurrentRawContactDelta);
643 mKindSectionDataMap.put(mimeType, kindSectionData);
644 mSortedMimetypes.add(mimeType);
645
646 vlog("parse: " + i + " " + dataKind.mimeType + " " +
647 kindSectionData.getValuesDeltas().size() + " value(s) " +
648 kindSectionData.getNonEmptyValuesDeltas().size() + " non-empty value(s) " +
649 kindSectionData.getVisibleValuesDeltas().size() +
650 " visible value(s)");
651 }
Walter Jang3efae4a2015-02-18 11:12:00 -0800652 }
653
Walter Jang9a552372016-08-24 11:51:05 -0700654 private void addReadOnlyRawContactEditorViews() {
Gary Mai65971d02016-09-15 15:00:16 -0700655 mKindSectionViews.removeAllViews();
Gary Mai4ceabed2016-09-16 12:14:13 -0700656 addAccountInfo();
Walter Jang9a552372016-08-24 11:51:05 -0700657 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
658 getContext());
Gary Mai4ceabed2016-09-16 12:14:13 -0700659 final AccountType type = mCurrentRawContactDelta.getAccountType(accountTypes);
Walter Jang9a552372016-08-24 11:51:05 -0700660
Gary Mai65971d02016-09-15 15:00:16 -0700661 // Bail if invalid state or source
Gary Mai4ceabed2016-09-16 12:14:13 -0700662 if (type == null) return;
Walter Jang9a552372016-08-24 11:51:05 -0700663
Gary Mai65971d02016-09-15 15:00:16 -0700664 // Make sure we have StructuredName
Gary Mai4ceabed2016-09-16 12:14:13 -0700665 RawContactModifier.ensureKindExists(
666 mCurrentRawContactDelta, type, StructuredName.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700667
668 ValuesDelta primary;
669
670 // Name
671 final Context context = getContext();
672 final Resources res = context.getResources();
Gary Mai4ceabed2016-09-16 12:14:13 -0700673 primary = mCurrentRawContactDelta.getPrimaryEntry(StructuredName.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700674 final String name = primary != null ? primary.getAsString(StructuredName.DISPLAY_NAME) :
675 getContext().getString(R.string.missing_name);
676 final Drawable nameDrawable = context.getDrawable(R.drawable.ic_person_24dp);
677 final String nameContentDescription = res.getString(R.string.header_name_entry);
678 bindData(nameDrawable, nameContentDescription, name, /* type */ null,
679 /* isFirstEntry */ true);
680
681 // Phones
Gary Mai4ceabed2016-09-16 12:14:13 -0700682 final ArrayList<ValuesDelta> phones = mCurrentRawContactDelta
683 .getMimeEntries(Phone.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700684 final Drawable phoneDrawable = context.getDrawable(R.drawable.ic_phone_24dp);
685 final String phoneContentDescription = res.getString(R.string.header_phone_entry);
686 if (phones != null) {
687 boolean isFirstPhoneBound = true;
688 for (ValuesDelta phone : phones) {
689 final String phoneNumber = phone.getPhoneNumber();
690 if (TextUtils.isEmpty(phoneNumber)) {
691 continue;
692 }
693 final String formattedNumber = PhoneNumberUtilsCompat.formatNumber(
694 phoneNumber, phone.getPhoneNormalizedNumber(),
695 GeoUtil.getCurrentCountryIso(getContext()));
696 CharSequence phoneType = null;
697 if (phone.hasPhoneType()) {
698 phoneType = Phone.getTypeLabel(
699 res, phone.getPhoneType(), phone.getPhoneLabel());
700 }
701 bindData(phoneDrawable, phoneContentDescription, formattedNumber, phoneType,
702 isFirstPhoneBound, true);
703 isFirstPhoneBound = false;
704 }
705 }
706
707 // Emails
Gary Mai4ceabed2016-09-16 12:14:13 -0700708 final ArrayList<ValuesDelta> emails = mCurrentRawContactDelta
709 .getMimeEntries(Email.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700710 final Drawable emailDrawable = context.getDrawable(R.drawable.ic_email_24dp);
711 final String emailContentDescription = res.getString(R.string.header_email_entry);
712 if (emails != null) {
713 boolean isFirstEmailBound = true;
714 for (ValuesDelta email : emails) {
715 final String emailAddress = email.getEmailData();
716 if (TextUtils.isEmpty(emailAddress)) {
717 continue;
718 }
719 CharSequence emailType = null;
720 if (email.hasEmailType()) {
721 emailType = Email.getTypeLabel(
722 res, email.getEmailType(), email.getEmailLabel());
723 }
724 bindData(emailDrawable, emailContentDescription, emailAddress, emailType,
725 isFirstEmailBound);
726 isFirstEmailBound = false;
727 }
728 }
729
730 mKindSectionViews.setVisibility(mKindSectionViews.getChildCount() > 0 ? VISIBLE : GONE);
731 // Hide the "More fields" link
732 mMoreFields.setVisibility(GONE);
733 }
734
Gary Mai65971d02016-09-15 15:00:16 -0700735 private void bindData(Drawable icon, String iconContentDescription, CharSequence data,
736 CharSequence type, boolean isFirstEntry) {
737 bindData(icon, iconContentDescription, data, type, isFirstEntry, false);
738 }
739
740 private void bindData(Drawable icon, String iconContentDescription, CharSequence data,
741 CharSequence type, boolean isFirstEntry, boolean forceLTR) {
742 final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
743 Context.LAYOUT_INFLATER_SERVICE);
744 final View field = inflater.inflate(R.layout.item_read_only_field, mKindSectionViews,
745 false);
746 if (isFirstEntry) {
747 final ImageView imageView = (ImageView) field.findViewById(R.id.kind_icon);
748 imageView.setImageDrawable(icon);
749 imageView.setContentDescription(iconContentDescription);
750 } else {
751 final ImageView imageView = (ImageView) field.findViewById(R.id.kind_icon);
752 imageView.setVisibility(View.INVISIBLE);
753 imageView.setContentDescription(null);
754 }
755 final TextView dataView = (TextView) field.findViewById(R.id.data);
756 dataView.setText(data);
757 if (forceLTR) {
758 dataView.setTextDirection(View.TEXT_DIRECTION_LTR);
759 }
760 final TextView typeView = (TextView) field.findViewById(R.id.type);
761 if (!TextUtils.isEmpty(type)) {
762 typeView.setText(type);
763 } else {
764 typeView.setVisibility(View.GONE);
765 }
766 mKindSectionViews.addView(field);
Walter Jang9a552372016-08-24 11:51:05 -0700767 }
768
Gary Mai98868d32016-09-14 11:55:04 -0700769 private void addAccountInfo() {
Walter Jang82acd422015-10-17 14:01:27 -0700770 mAccountHeaderContainer.setVisibility(View.GONE);
Walter Jang708ea9e2015-09-10 15:42:05 -0700771
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700772 final AccountDisplayInfo account =
Gary Mai4ceabed2016-09-16 12:14:13 -0700773 mAccountDisplayInfoFactory.getAccountDisplayInfoFor(mCurrentRawContactDelta);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700774
Walter Jang708ea9e2015-09-10 15:42:05 -0700775 // Get the account information for the primary raw contact delta
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700776 final String accountLabel = mIsUserProfile
777 ? EditorUiUtils.getAccountHeaderLabelForMyProfile(getContext(), account)
778 : account.getNameLabel().toString();
Walter Jang708ea9e2015-09-10 15:42:05 -0700779
Walter Jang23709542015-10-22 12:50:58 -0700780 // Either the account header or selector should be shown, not both.
Walter Jang708ea9e2015-09-10 15:42:05 -0700781 final List<AccountWithDataSet> accounts =
782 AccountTypeManager.getInstance(getContext()).getAccounts(true);
Gary Mai4ceabed2016-09-16 12:14:13 -0700783
Walter Jang82acd422015-10-17 14:01:27 -0700784 if (mHasNewContact && !mIsUserProfile) {
785 if (accounts.size() > 1) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700786 addAccountSelector(mCurrentRawContactDelta, accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700787 } else {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700788 addAccountHeader(accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700789 }
Gary Maida20b472016-09-20 14:46:40 -0700790 } else {
791 // The raw contact selector should only display linked raw contacts that can be edited
792 // in the full editor (i.e. they are not newly created raw contacts)
793 final RawContactAccountListAdapter adapter = new RawContactAccountListAdapter(
794 getContext(), getRawContactDeltaListForSelector(mRawContactDeltas));
795 if (adapter.getCount() > 0 && !mIsEditingReadOnlyRawContactWithNewContact) {
796 addRawContactAccountSelector(accountLabel, adapter);
797 } else {
798 addAccountHeader(accountLabel);
799 }
Walter Jang23709542015-10-22 12:50:58 -0700800 }
Walter Jang708ea9e2015-09-10 15:42:05 -0700801 }
802
Tingting Wang2bb85d22015-10-23 13:01:09 -0700803 private RawContactDeltaList getRawContactDeltaListForSelector(
804 RawContactDeltaList rawContactDeltas) {
805 // Sort raw contacts so google accounts come first
806 Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
807
808 final RawContactDeltaList result = new RawContactDeltaList();
Gary Mai98868d32016-09-14 11:55:04 -0700809 for (int i = 0; i < rawContactDeltas.size(); i++) {
810 final RawContactDelta rawContactDelta = rawContactDeltas.get(i);
Tingting Wang2bb85d22015-10-23 13:01:09 -0700811 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
812 // Only add raw contacts that can be opened in the editor
813 result.add(rawContactDelta);
814 }
815 }
816 // Don't return a list of size 1 that would just open the raw contact being edited
817 // in the compact editor in the full editor
818 if (result.size() == 1 && result.get(0).getRawContactAccountType(
819 getContext()).areContactsWritable()) {
820 result.clear();
821 return result;
822 }
823 return result;
824 }
825
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700826 private void addAccountHeader(String accountLabel) {
Walter Jang82acd422015-10-17 14:01:27 -0700827 mAccountHeaderContainer.setVisibility(View.VISIBLE);
828
Walter Jangbe5e1b12015-10-17 11:38:29 -0700829 // Set the account name
Walter Jangbe5e1b12015-10-17 11:38:29 -0700830 mAccountHeaderName.setVisibility(View.VISIBLE);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700831 mAccountHeaderName.setText(accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700832
833 // Set the account type
Gary Mai4ceabed2016-09-16 12:14:13 -0700834 final String selectorTitle = getResources().getString(isReadOnlyRawContact() ?
835 R.string.compact_editor_account_selector_read_only_title :
Walter Jangbe5e1b12015-10-17 11:38:29 -0700836 R.string.compact_editor_account_selector_title);
837 mAccountHeaderType.setText(selectorTitle);
838
839 // Set the icon
Gary Mai4ceabed2016-09-16 12:14:13 -0700840 final AccountType accountType =
841 mCurrentRawContactDelta.getRawContactAccountType(getContext());
842 mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext()));
Walter Jangbe5e1b12015-10-17 11:38:29 -0700843
844 // Set the content description
845 mAccountHeaderContainer.setContentDescription(
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700846 EditorUiUtils.getAccountInfoContentDescription(accountLabel,
847 selectorTitle));
Walter Jangbe5e1b12015-10-17 11:38:29 -0700848 }
849
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700850 private void addAccountSelector(final RawContactDelta rawContactDelta, CharSequence nameLabel) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700851 final View.OnClickListener onClickListener = new View.OnClickListener() {
Walter Jang708ea9e2015-09-10 15:42:05 -0700852 @Override
853 public void onClick(View v) {
854 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
855 final AccountsListAdapter adapter =
856 new AccountsListAdapter(getContext(),
857 AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE,
858 mPrimaryAccount);
Gary Mai46cb3102016-08-10 18:14:09 -0700859 popup.setWidth(mAccountHeaderContainer.getWidth());
860 popup.setAnchorView(mAccountHeaderContainer);
Walter Jang708ea9e2015-09-10 15:42:05 -0700861 popup.setAdapter(adapter);
862 popup.setModal(true);
863 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
864 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
865 @Override
866 public void onItemClick(AdapterView<?> parent, View view, int position,
867 long id) {
868 UiClosables.closeQuietly(popup);
869 final AccountWithDataSet newAccount = adapter.getItem(position);
870 if (mListener != null && !mPrimaryAccount.equals(newAccount)) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700871 mIsExpanded = false;
Walter Jang708ea9e2015-09-10 15:42:05 -0700872 mListener.onRebindEditorsForNewContact(
Walter Jang82acd422015-10-17 14:01:27 -0700873 rawContactDelta,
Walter Jang708ea9e2015-09-10 15:42:05 -0700874 mPrimaryAccount,
875 newAccount);
876 }
877 }
878 });
879 popup.show();
880 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700881 };
882 setUpAccountSelector(nameLabel.toString(), onClickListener);
Walter Jang708ea9e2015-09-10 15:42:05 -0700883 }
884
Gary Mai4ceabed2016-09-16 12:14:13 -0700885 private void addRawContactAccountSelector(String nameLabel,
Walter Jang23709542015-10-22 12:50:58 -0700886 final RawContactAccountListAdapter adapter) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700887 final View.OnClickListener onClickListener = new OnClickListener() {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700888 @Override
889 public void onClick(View v) {
890 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
Gary Mai4ceabed2016-09-16 12:14:13 -0700891 popup.setWidth(mAccountHeaderContainer.getWidth());
892 popup.setAnchorView(mAccountHeaderContainer);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700893 popup.setAdapter(adapter);
894 popup.setModal(true);
895 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
896 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
897 @Override
898 public void onItemClick(AdapterView<?> parent, View view, int position,
899 long id) {
900 UiClosables.closeQuietly(popup);
Gary Mai4ceabed2016-09-16 12:14:13 -0700901 final long rawContactId = adapter.getItemId(position);
902 // Only switch if it's actually a different raw contact.
903 if (rawContactId != mCurrentRawContactDelta.getRawContactId()
904 && mListener != null) {
Tingting Wang91cee282015-10-07 13:48:17 -0700905 final RawContactDelta rawContactDelta = adapter.getItem(position);
906 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
907 getContext());
908 final AccountType accountType = rawContactDelta.getAccountType(
909 accountTypes);
910 final boolean isReadOnly = !accountType.areContactsWritable();
Gary Mai4ceabed2016-09-16 12:14:13 -0700911 // Reset state.
912 mIsExpanded = false;
913 mListener.onRawContactSelected(rawContactId, isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700914 }
915 }
916 });
917 popup.show();
918 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700919 };
920 setUpAccountSelector(nameLabel, onClickListener);
921 }
922
923 private void setUpAccountSelector(String nameLabel, OnClickListener listener) {
924 addAccountHeader(nameLabel);
925 // Add handlers for choosing another account to save to.
926 mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE);
927 mAccountHeaderContainer.setOnClickListener(listener);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700928 }
929
Walter Jang363d3fd2015-09-16 10:29:07 -0700930 private void addPhotoView() {
Gary Mai4ceabed2016-09-16 12:14:13 -0700931 if (!mCurrentRawContactDelta.hasMimeEntries(Photo.CONTENT_ITEM_TYPE)) {
932 wlog("No photo mimetype for this raw contact.");
Gary Maida20b472016-09-20 14:46:40 -0700933 mPhotoView.setVisibility(GONE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700934 return;
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700935 } else {
Gary Maida20b472016-09-20 14:46:40 -0700936 mPhotoView.setVisibility(VISIBLE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700937 }
Walter Jang06f73a12015-06-17 11:15:48 -0700938
Gary Mai4ceabed2016-09-16 12:14:13 -0700939 final ValuesDelta superPrimaryDelta = mCurrentRawContactDelta
940 .getSuperPrimaryEntry(Photo.CONTENT_ITEM_TYPE);
Gary Maida20b472016-09-20 14:46:40 -0700941 if (superPrimaryDelta == null) {
942 Log.wtf(TAG, "addPhotoView: no ValueDelta found for current RawContactDelta"
943 + "that supports a photo.");
944 mPhotoView.setVisibility(GONE);
945 return;
946 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700947 // Set the photo view
Gary Mai18724182016-09-20 17:33:30 -0700948 mPhotoView.setPalette(mMaterialPalette);
949 mPhotoView.setPhoto(superPrimaryDelta);
Walter Jang31a74ad2015-10-02 19:17:39 -0700950
Gary Mai4ceabed2016-09-16 12:14:13 -0700951 if (isReadOnlyRawContact()) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700952 mPhotoView.setReadOnly(true);
953 return;
954 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700955 mPhotoView.setReadOnly(false);
Gary Mai4ceabed2016-09-16 12:14:13 -0700956 mPhotoValuesDelta = superPrimaryDelta;
Walter Jang31a74ad2015-10-02 19:17:39 -0700957 }
958
Walter Jangf5dfea42015-09-16 12:30:36 -0700959 private void addKindSectionViews() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700960 int i = -1;
Gary Mai98868d32016-09-14 11:55:04 -0700961
962 for (String mimeType : mSortedMimetypes) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700963 i++;
Gary Mai4ceabed2016-09-16 12:14:13 -0700964 // Ignore mime types that we've already handled
965 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
966 vlog("kind: " + i + " " + mimeType + " dropped");
967 continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800968 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700969 final CompactKindSectionView kindSectionView;
970 final KindSectionData kindSectionData = mKindSectionDataMap.get(mimeType);
Gary Mai698cee72016-09-19 16:09:54 -0700971 kindSectionView = inflateKindSectionView(mKindSectionViews, kindSectionData, mimeType);
Gary Mai98868d32016-09-14 11:55:04 -0700972 mKindSectionViews.addView(kindSectionView);
Walter Jangcab3dce2015-02-09 17:48:03 -0800973
Gary Mai98868d32016-09-14 11:55:04 -0700974 // Keep a pointer to the KindSectionView for each mimeType
975 mKindSectionViewMap.put(mimeType, kindSectionView);
Walter Jangf10ca152015-09-22 15:23:55 -0700976 }
Walter Jangf10ca152015-09-22 15:23:55 -0700977 }
978
Walter Jangf5dfea42015-09-16 12:30:36 -0700979 private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup,
Gary Mai698cee72016-09-19 16:09:54 -0700980 KindSectionData kindSectionData, String mimeType) {
Walter Jangf5dfea42015-09-16 12:30:36 -0700981 final CompactKindSectionView kindSectionView = (CompactKindSectionView)
982 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
983 /* attachToRoot =*/ false);
Walter Jang45b86d52015-10-15 15:23:16 -0700984 kindSectionView.setIsUserProfile(mIsUserProfile);
Walter Jangf5dfea42015-09-16 12:30:36 -0700985
Walter Jang192a01c2015-09-22 15:23:55 -0700986 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Walter Jangf5dfea42015-09-16 12:30:36 -0700987 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -0700988 // Phone numbers and email addresses are always displayed,
989 // even if they are empty
Walter Jangf5dfea42015-09-16 12:30:36 -0700990 kindSectionView.setHideWhenEmpty(false);
991 }
Walter Jangf5dfea42015-09-16 12:30:36 -0700992
Walter Jang192a01c2015-09-22 15:23:55 -0700993 // Since phone numbers and email addresses displayed even if they are empty,
994 // they will be the only types you add new values to initially for new contacts
995 kindSectionView.setShowOneEmptyEditor(true);
996
Gary Mai698cee72016-09-19 16:09:54 -0700997 kindSectionView.setState(kindSectionData, mViewIdGenerator, mListener);
Walter Jangf5dfea42015-09-16 12:30:36 -0700998
999 return kindSectionView;
Walter Jangcab3dce2015-02-09 17:48:03 -08001000 }
1001
Walter Jangd6753152015-10-02 09:23:13 -07001002 private void showAllFields() {
Walter Jang79658e12015-09-24 10:36:26 -07001003 // Stop hiding empty editors and allow the user to enter values for all kinds now
1004 for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
1005 final CompactKindSectionView kindSectionView =
1006 (CompactKindSectionView) mKindSectionViews.getChildAt(i);
1007 kindSectionView.setHideWhenEmpty(false);
1008 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
1009 }
1010 mIsExpanded = true;
Walter Jang79658e12015-09-24 10:36:26 -07001011
Walter Jangf5dfea42015-09-16 12:30:36 -07001012 // Hide the more fields button
1013 mMoreFields.setVisibility(View.GONE);
Walter Jangcab3dce2015-02-09 17:48:03 -08001014 }
1015
Marcus Hagerottcf7f2952016-09-06 13:49:17 -07001016 private boolean hasMoreFields() {
Gary Mai98868d32016-09-14 11:55:04 -07001017 for (CompactKindSectionView section : mKindSectionViewMap.values()) {
1018 if (section.getVisibility() != View.VISIBLE) {
1019 return true;
Marcus Hagerottcf7f2952016-09-06 13:49:17 -07001020 }
1021 }
1022 return false;
1023 }
1024
Walter Jangbf63a6d2015-05-05 09:14:35 -07001025 private static void vlog(String message) {
1026 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1027 Log.v(TAG, message);
Walter Jangcab3dce2015-02-09 17:48:03 -08001028 }
1029 }
Walter Jang363d3fd2015-09-16 10:29:07 -07001030
1031 private static void wlog(String message) {
1032 if (Log.isLoggable(TAG, Log.WARN)) {
1033 Log.w(TAG, message);
1034 }
1035 }
1036
1037 private static void elog(String message) {
1038 Log.e(TAG, message);
1039 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001040}