blob: 7a52a37460e1ffadf532f32ff54fe574b5c97f57 [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 Jang3f18d612015-10-07 16:01:05 -070024import android.os.Bundle;
Walter Jang79658e12015-09-24 10:36:26 -070025import android.os.Parcel;
26import android.os.Parcelable;
Walter Jangcab3dce2015-02-09 17:48:03 -080027import android.provider.ContactsContract.CommonDataKinds.Email;
Walter Jangf5dfea42015-09-16 12:30:36 -070028import android.provider.ContactsContract.CommonDataKinds.Event;
Walter Jangcab3dce2015-02-09 17:48:03 -080029import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
Walter Jangf5dfea42015-09-16 12:30:36 -070030import android.provider.ContactsContract.CommonDataKinds.Im;
Walter Jang3efae4a2015-02-18 11:12:00 -080031import android.provider.ContactsContract.CommonDataKinds.Nickname;
Walter Jangf5dfea42015-09-16 12:30:36 -070032import android.provider.ContactsContract.CommonDataKinds.Note;
33import android.provider.ContactsContract.CommonDataKinds.Organization;
Walter Jangcab3dce2015-02-09 17:48:03 -080034import android.provider.ContactsContract.CommonDataKinds.Phone;
35import android.provider.ContactsContract.CommonDataKinds.Photo;
Walter Jangf5dfea42015-09-16 12:30:36 -070036import android.provider.ContactsContract.CommonDataKinds.Relation;
37import android.provider.ContactsContract.CommonDataKinds.SipAddress;
Walter Jangcab3dce2015-02-09 17:48:03 -080038import android.provider.ContactsContract.CommonDataKinds.StructuredName;
Walter Jangf5dfea42015-09-16 12:30:36 -070039import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
40import android.provider.ContactsContract.CommonDataKinds.Website;
Walter Jangcab3dce2015-02-09 17:48:03 -080041import android.text.TextUtils;
42import android.util.AttributeSet;
43import android.util.Log;
44import android.view.LayoutInflater;
Walter Jangb1c87622015-02-13 17:51:38 -080045import android.view.View;
Walter Jangcab3dce2015-02-09 17:48:03 -080046import android.view.ViewGroup;
Walter Jang708ea9e2015-09-10 15:42:05 -070047import android.widget.AdapterView;
Tingting Wang655ad1a2015-10-05 17:51:14 -070048import android.widget.BaseAdapter;
49import android.widget.ImageView;
Walter Jangcab3dce2015-02-09 17:48:03 -080050import android.widget.LinearLayout;
Walter Jang708ea9e2015-09-10 15:42:05 -070051import android.widget.ListPopupWindow;
Walter Jang2d3f31c2015-06-18 23:15:31 -070052import android.widget.TextView;
Walter Jangcab3dce2015-02-09 17:48:03 -080053
Gary Maib6e28d22016-09-12 14:04:53 -070054import com.android.contacts.R;
Gary Mai65971d02016-09-15 15:00:16 -070055import com.android.contacts.common.GeoUtil;
56import com.android.contacts.common.compat.PhoneNumberUtilsCompat;
Gary Maib6e28d22016-09-12 14:04:53 -070057import com.android.contacts.common.model.AccountTypeManager;
58import com.android.contacts.common.model.RawContactDelta;
59import com.android.contacts.common.model.RawContactDeltaList;
60import com.android.contacts.common.model.RawContactModifier;
61import com.android.contacts.common.model.ValuesDelta;
62import com.android.contacts.common.model.account.AccountDisplayInfo;
63import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
64import com.android.contacts.common.model.account.AccountType;
65import com.android.contacts.common.model.account.AccountWithDataSet;
Gary Mai98868d32016-09-14 11:55:04 -070066import com.android.contacts.common.model.dataitem.CustomDataItem;
Gary Maib6e28d22016-09-12 14:04:53 -070067import com.android.contacts.common.model.dataitem.DataKind;
68import com.android.contacts.common.util.AccountsListAdapter;
69import com.android.contacts.common.util.MaterialColorMapUtils;
70import com.android.contacts.util.UiClosables;
71
Walter Jang708774a2015-10-16 09:32:06 -070072import java.io.FileNotFoundException;
Walter Jangcab3dce2015-02-09 17:48:03 -080073import java.util.ArrayList;
Walter Jangf5dfea42015-09-16 12:30:36 -070074import java.util.Arrays;
Walter Jang192a01c2015-09-22 15:23:55 -070075import java.util.Collections;
Walter Jangf5dfea42015-09-16 12:30:36 -070076import java.util.Comparator;
Walter Jang19d985f2015-07-01 13:36:27 -070077import java.util.HashMap;
Walter Jangcab3dce2015-02-09 17:48:03 -080078import java.util.List;
Walter Jang19d985f2015-07-01 13:36:27 -070079import java.util.Map;
Gary Mai98868d32016-09-14 11:55:04 -070080import java.util.Set;
Walter Jangf5dfea42015-09-16 12:30:36 -070081import java.util.TreeSet;
Walter Jangcab3dce2015-02-09 17:48:03 -080082
83/**
Walter Jangf5dfea42015-09-16 12:30:36 -070084 * View to display information from multiple {@link RawContactDelta}s grouped together.
Walter Jangcab3dce2015-02-09 17:48:03 -080085 */
Walter Jangb6ca2722015-02-20 11:10:25 -080086public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener {
Walter Jangcab3dce2015-02-09 17:48:03 -080087
Walter Jang4f5594a2015-10-06 18:40:31 -070088 static final String TAG = "CompactEditorView";
Walter Jangcab3dce2015-02-09 17:48:03 -080089
Walter Jangb6ca2722015-02-20 11:10:25 -080090 /**
91 * Callbacks for hosts of {@link CompactRawContactsEditorView}s.
92 */
93 public interface Listener {
94
95 /**
Walter Jang151f3e62015-02-26 15:29:40 -080096 * Invoked when the structured name editor field has changed.
97 *
98 * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}.
99 * @param valuesDelta The values from the underlying {@link RawContactDelta}.
100 */
101 public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta);
Walter Jang708ea9e2015-09-10 15:42:05 -0700102
103 /**
104 * Invoked when the compact editor should rebind editors for a new account.
105 *
106 * @param oldState Old data being edited.
107 * @param oldAccount Old account associated with oldState.
108 * @param newAccount New account to be used.
109 */
110 public void onRebindEditorsForNewContact(RawContactDelta oldState,
111 AccountWithDataSet oldAccount, AccountWithDataSet newAccount);
Walter Jang79658e12015-09-24 10:36:26 -0700112
113 /**
114 * Invoked when no editors could be bound for the contact.
115 */
116 public void onBindEditorsFailed();
Walter Jangd6753152015-10-02 09:23:13 -0700117
118 /**
119 * Invoked after editors have been bound for the contact.
120 */
121 public void onEditorsBound();
Tingting Wang655ad1a2015-10-05 17:51:14 -0700122
123 /**
Tingting Wang5585c6e2015-10-14 11:19:58 -0700124 * Invoked when a rawcontact from linked contacts is selected in editor.
Tingting Wang655ad1a2015-10-05 17:51:14 -0700125 */
Gary Mai4ceabed2016-09-16 12:14:13 -0700126 void onRawContactSelected(long rawContactId, boolean isReadOnly);
Walter Jang3f18d612015-10-07 16:01:05 -0700127
128 /**
129 * Returns the map of raw contact IDs to newly taken or selected photos that have not
130 * yet been saved to CP2.
131 */
132 public Bundle getUpdatedPhotos();
Tingting Wang655ad1a2015-10-05 17:51:14 -0700133 }
134
135 /**
136 * Used to list the account info for the given raw contacts list.
137 */
138 private static final class RawContactAccountListAdapter extends BaseAdapter {
139 private final LayoutInflater mInflater;
140 private final Context mContext;
141 private final RawContactDeltaList mRawContactDeltas;
142
143 public RawContactAccountListAdapter(Context context, RawContactDeltaList rawContactDeltas) {
144 mContext = context;
145 mRawContactDeltas = new RawContactDeltaList();
146 for (RawContactDelta rawContactDelta : rawContactDeltas) {
Walter Jang23709542015-10-22 12:50:58 -0700147 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700148 mRawContactDeltas.add(rawContactDelta);
149 }
150 }
151 mInflater = LayoutInflater.from(context);
152 }
153
154 @Override
155 public View getView(int position, View convertView, ViewGroup parent) {
156 final View resultView = convertView != null ? convertView
157 : mInflater.inflate(R.layout.account_selector_list_item, parent, false);
158
159 final RawContactDelta rawContactDelta = mRawContactDeltas.get(position);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700160
161 final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
Walter Jang9488a762015-10-16 13:42:48 -0700162 final AccountType accountType = rawContactDelta.getRawContactAccountType(mContext);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700163 text1.setText(accountType.getDisplayLabel(mContext));
164
Tingting Wang655ad1a2015-10-05 17:51:14 -0700165 final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
Walter Jang9488a762015-10-16 13:42:48 -0700166 final String accountName = rawContactDelta.getAccountName();
Tingting Wanga0b69402015-10-13 14:27:58 -0700167 if (TextUtils.isEmpty(accountName)) {
168 text2.setVisibility(View.GONE);
169 } else {
Walter Jang9488a762015-10-16 13:42:48 -0700170 // Truncate email addresses in the middle so we don't lose the domain
Tingting Wanga0b69402015-10-13 14:27:58 -0700171 text2.setText(accountName);
172 text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
173 }
Tingting Wang655ad1a2015-10-05 17:51:14 -0700174
175 final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
176 icon.setImageDrawable(accountType.getDisplayIcon(mContext));
177
178 return resultView;
179 }
180
181 @Override
182 public int getCount() {
183 return mRawContactDeltas.size();
184 }
185
186 @Override
187 public RawContactDelta getItem(int position) {
188 return mRawContactDeltas.get(position);
189 }
190
191 @Override
192 public long getItemId(int position) {
193 return getItem(position).getRawContactId();
194 }
Walter Jang151f3e62015-02-26 15:29:40 -0800195 }
196
Walter Jang192a01c2015-09-22 15:23:55 -0700197 /**
198 * Sorts kinds roughly the same as quick contacts; we diverge in the following ways:
199 * <ol>
200 * <li>All names are together at the top.</li>
201 * <li>IM is moved up after addresses</li>
202 * <li>SIP addresses are moved to below phone numbers</li>
Walter Jang79658e12015-09-24 10:36:26 -0700203 * <li>Group membership is placed at the end</li>
Walter Jang192a01c2015-09-22 15:23:55 -0700204 * </ol>
205 */
206 private static final class MimeTypeComparator implements Comparator<String> {
207
Walter Jangf5dfea42015-09-16 12:30:36 -0700208 private static final List<String> MIME_TYPE_ORDER = Arrays.asList(new String[] {
209 StructuredName.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700210 Nickname.CONTENT_ITEM_TYPE,
Walter Jang2ae21752015-09-25 11:25:38 -0700211 Organization.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700212 Phone.CONTENT_ITEM_TYPE,
213 SipAddress.CONTENT_ITEM_TYPE,
214 Email.CONTENT_ITEM_TYPE,
215 StructuredPostal.CONTENT_ITEM_TYPE,
216 Im.CONTENT_ITEM_TYPE,
217 Website.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700218 Event.CONTENT_ITEM_TYPE,
219 Relation.CONTENT_ITEM_TYPE,
Walter Jangf10ca152015-09-22 15:23:55 -0700220 Note.CONTENT_ITEM_TYPE,
221 GroupMembership.CONTENT_ITEM_TYPE
Walter Jangf5dfea42015-09-16 12:30:36 -0700222 });
223
224 @Override
Walter Jang192a01c2015-09-22 15:23:55 -0700225 public int compare(String mimeType1, String mimeType2) {
226 if (mimeType1 == mimeType2) return 0;
227 if (mimeType1 == null) return -1;
228 if (mimeType2 == null) return 1;
Walter Jangf5dfea42015-09-16 12:30:36 -0700229
230 int index1 = MIME_TYPE_ORDER.indexOf(mimeType1);
231 int index2 = MIME_TYPE_ORDER.indexOf(mimeType2);
232
233 // Fallback to alphabetical ordering of the mime type if both are not found
234 if (index1 < 0 && index2 < 0) return mimeType1.compareTo(mimeType2);
235 if (index1 < 0) return 1;
236 if (index2 < 0) return -1;
237
238 return index1 < index2 ? -1 : 1;
239 }
240 }
241
Walter Jang79658e12015-09-24 10:36:26 -0700242 public static class SavedState extends BaseSavedState {
243
244 public static final Parcelable.Creator<SavedState> CREATOR =
245 new Parcelable.Creator<SavedState>() {
246 public SavedState createFromParcel(Parcel in) {
247 return new SavedState(in);
248 }
249 public SavedState[] newArray(int size) {
250 return new SavedState[size];
251 }
252 };
253
254 private boolean mIsExpanded;
255
256 public SavedState(Parcelable superState) {
257 super(superState);
258 }
259
260 private SavedState(Parcel in) {
261 super(in);
262 mIsExpanded = in.readInt() != 0;
263 }
264
265 @Override
266 public void writeToParcel(Parcel out, int flags) {
267 super.writeToParcel(out, flags);
268 out.writeInt(mIsExpanded ? 1 : 0);
269 }
270 }
271
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700272 private CompactRawContactsEditorView.Listener mListener;
Walter Jangb6ca2722015-02-20 11:10:25 -0800273
Walter Jangcab3dce2015-02-09 17:48:03 -0800274 private AccountTypeManager mAccountTypeManager;
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700275 private AccountDisplayInfoFactory mAccountDisplayInfoFactory;
Walter Jangcab3dce2015-02-09 17:48:03 -0800276 private LayoutInflater mLayoutInflater;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800277
Walter Jangcab3dce2015-02-09 17:48:03 -0800278 private ViewIdGenerator mViewIdGenerator;
Walter Jangf46abd82015-02-20 16:52:04 -0800279 private MaterialColorMapUtils.MaterialPalette mMaterialPalette;
Gary Mai98868d32016-09-14 11:55:04 -0700280 private long mAggregatePhotoId = -1;
Walter Jang708ea9e2015-09-10 15:42:05 -0700281 private boolean mHasNewContact;
282 private boolean mIsUserProfile;
283 private AccountWithDataSet mPrimaryAccount;
Walter Jang9a552372016-08-24 11:51:05 -0700284 private RawContactDeltaList mRawContactDeltas;
Gary Mai4ceabed2016-09-16 12:14:13 -0700285 private RawContactDelta mCurrentRawContactDelta;
Walter Jang9a552372016-08-24 11:51:05 -0700286 private long mRawContactIdToDisplayAlone = -1;
287 private boolean mRawContactDisplayAloneIsReadOnly;
288 private boolean mIsEditingReadOnlyRawContactWithNewContact;
Gary Mai98868d32016-09-14 11:55:04 -0700289 private Map<String, KindSectionData> mKindSectionDataMap = new HashMap<>();
290 private Set<String> mSortedMimetypes = new TreeSet<>(new MimeTypeComparator());
Walter Jangcab3dce2015-02-09 17:48:03 -0800291
Walter Jang708ea9e2015-09-10 15:42:05 -0700292 // Account header
293 private View mAccountHeaderContainer;
294 private TextView mAccountHeaderType;
295 private TextView mAccountHeaderName;
Tingting Wang91cee282015-10-07 13:48:17 -0700296 private ImageView mAccountHeaderIcon;
Gary Mai46cb3102016-08-10 18:14:09 -0700297 private ImageView mAccountHeaderExpanderIcon;
Walter Jang708ea9e2015-09-10 15:42:05 -0700298
Tingting Wang655ad1a2015-10-05 17:51:14 -0700299 // Raw contacts selector
300 private View mRawContactContainer;
301 private TextView mRawContactSummary;
Tingting Wang655ad1a2015-10-05 17:51:14 -0700302
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700303 private CompactPhotoEditorView mPhotoView;
Walter Jangf5dfea42015-09-16 12:30:36 -0700304 private ViewGroup mKindSectionViews;
Gary Mai98868d32016-09-14 11:55:04 -0700305 private Map<String, CompactKindSectionView> mKindSectionViewMap = new HashMap<>();
Walter Jangb6ca2722015-02-20 11:10:25 -0800306 private View mMoreFields;
Walter Jangcab3dce2015-02-09 17:48:03 -0800307
Walter Jang79658e12015-09-24 10:36:26 -0700308 private boolean mIsExpanded;
Walter Jangcbd431d2015-10-06 13:07:30 -0700309
Walter Jang3efae4a2015-02-18 11:12:00 -0800310 private long mPhotoRawContactId;
Walter Jang31a74ad2015-10-02 19:17:39 -0700311 private ValuesDelta mPhotoValuesDelta;
Walter Jang82acd422015-10-17 14:01:27 -0700312
Walter Jangcab3dce2015-02-09 17:48:03 -0800313 public CompactRawContactsEditorView(Context context) {
314 super(context);
315 }
316
317 public CompactRawContactsEditorView(Context context, AttributeSet attrs) {
318 super(context, attrs);
319 }
320
Walter Jangb6ca2722015-02-20 11:10:25 -0800321 /**
322 * Sets the receiver for {@link CompactRawContactsEditorView} callbacks.
323 */
324 public void setListener(Listener listener) {
325 mListener = listener;
326 }
327
Walter Jangcab3dce2015-02-09 17:48:03 -0800328 @Override
329 protected void onFinishInflate() {
330 super.onFinishInflate();
331
332 mAccountTypeManager = AccountTypeManager.getInstance(getContext());
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700333 mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(getContext());
Walter Jangcab3dce2015-02-09 17:48:03 -0800334 mLayoutInflater = (LayoutInflater)
335 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
336
Walter Jang708ea9e2015-09-10 15:42:05 -0700337 // Account header
Gary Mai46cb3102016-08-10 18:14:09 -0700338 mAccountHeaderContainer = findViewById(R.id.account_header_container);
Walter Jang708ea9e2015-09-10 15:42:05 -0700339 mAccountHeaderType = (TextView) findViewById(R.id.account_type);
340 mAccountHeaderName = (TextView) findViewById(R.id.account_name);
Tingting Wang91cee282015-10-07 13:48:17 -0700341 mAccountHeaderIcon = (ImageView) findViewById(R.id.account_type_icon);
Gary Mai46cb3102016-08-10 18:14:09 -0700342 mAccountHeaderExpanderIcon = (ImageView) findViewById(R.id.account_expander_icon);
Walter Jang708ea9e2015-09-10 15:42:05 -0700343
Tingting Wang655ad1a2015-10-05 17:51:14 -0700344 // Raw contacts selector
345 mRawContactContainer = findViewById(R.id.all_rawcontacts_accounts_container);
346 mRawContactSummary = (TextView) findViewById(R.id.rawcontacts_accounts_summary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700347
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700348 mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor);
Walter Jangf5dfea42015-09-16 12:30:36 -0700349 mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views);
Walter Jangb6ca2722015-02-20 11:10:25 -0800350 mMoreFields = findViewById(R.id.more_fields);
351 mMoreFields.setOnClickListener(this);
352 }
353
Walter Jangb6ca2722015-02-20 11:10:25 -0800354 @Override
355 public void onClick(View view) {
Walter Jangf5dfea42015-09-16 12:30:36 -0700356 if (view.getId() == R.id.more_fields) {
Walter Jangd6753152015-10-02 09:23:13 -0700357 showAllFields();
Walter Jangb6ca2722015-02-20 11:10:25 -0800358 }
Walter Jangcab3dce2015-02-09 17:48:03 -0800359 }
360
361 @Override
362 public void setEnabled(boolean enabled) {
363 super.setEnabled(enabled);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700364 final int childCount = mKindSectionViews.getChildCount();
365 for (int i = 0; i < childCount; i++) {
366 mKindSectionViews.getChildAt(i).setEnabled(enabled);
Walter Jangcab3dce2015-02-09 17:48:03 -0800367 }
368 }
369
Walter Jang79658e12015-09-24 10:36:26 -0700370 @Override
371 public Parcelable onSaveInstanceState() {
372 final Parcelable superState = super.onSaveInstanceState();
373 final SavedState savedState = new SavedState(superState);
374 savedState.mIsExpanded = mIsExpanded;
375 return savedState;
376 }
377
378 @Override
379 public void onRestoreInstanceState(Parcelable state) {
380 if(!(state instanceof SavedState)) {
381 super.onRestoreInstanceState(state);
382 return;
383 }
384 final SavedState savedState = (SavedState) state;
385 super.onRestoreInstanceState(savedState.getSuperState());
386 mIsExpanded = savedState.mIsExpanded;
Walter Jangd6753152015-10-02 09:23:13 -0700387 if (mIsExpanded) {
388 showAllFields();
Walter Jang79658e12015-09-24 10:36:26 -0700389 }
390 }
391
Walter Jang3efae4a2015-02-18 11:12:00 -0800392 /**
Walter Jang31a74ad2015-10-02 19:17:39 -0700393 * Pass through to {@link CompactPhotoEditorView#setListener}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800394 */
Walter Jang31a74ad2015-10-02 19:17:39 -0700395 public void setPhotoListener(CompactPhotoEditorView.Listener listener) {
396 mPhotoView.setListener(listener);
Walter Jang3efae4a2015-02-18 11:12:00 -0800397 }
398
Walter Jang31a74ad2015-10-02 19:17:39 -0700399 public void removePhoto() {
Walter Jang45b86d52015-10-15 15:23:16 -0700400 mPhotoValuesDelta.setFromTemplate(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700401 mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null);
402
403 mPhotoView.removePhoto();
Walter Jang3efae4a2015-02-18 11:12:00 -0800404 }
405
406 /**
Walter Jang41b3ea12015-03-09 17:30:06 -0700407 * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}.
408 */
409 public void setFullSizePhoto(Uri photoUri) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700410 mPhotoView.setFullSizedPhoto(photoUri);
Walter Jang41b3ea12015-03-09 17:30:06 -0700411 }
412
Walter Jang31a74ad2015-10-02 19:17:39 -0700413 public void updatePhoto(Uri photoUri) {
Walter Jang45b86d52015-10-15 15:23:16 -0700414 mPhotoValuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700415 // Unset primary for all photos
Wenyi Wang9086fb92015-11-16 09:58:20 -0800416 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700417 // Mark the currently displayed photo as primary
418 mPhotoValuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700419
Wenyi Wang4c9cf3a2015-10-15 17:58:23 -0700420 // Even though high-res photos cannot be saved by passing them via
421 // an EntityDeltaList (since they cause the Bundle size limit to be
422 // exceeded), we still pass a low-res thumbnail. This simplifies
423 // code all over the place, because we don't have to test whether
424 // there is a change in EITHER the delta-list OR a changed photo...
425 // this way, there is always a change in the delta-list.
426 try {
427 final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
428 getContext(), photoUri);
429 if (bytes != null) {
430 mPhotoValuesDelta.setPhoto(bytes);
431 }
432 } catch (FileNotFoundException e) {
433 elog("Failed to get bitmap from photo Uri");
434 }
435
Walter Jang31a74ad2015-10-02 19:17:39 -0700436 mPhotoView.setFullSizedPhoto(photoUri);
437 }
438
Wenyi Wang9086fb92015-11-16 09:58:20 -0800439 private void unsetSuperPrimaryFromAllPhotos() {
Gary Mai4ceabed2016-09-16 12:14:13 -0700440 for (int i = 0; i < mRawContactDeltas.size(); i++) {
441 final RawContactDelta rawContactDelta = mRawContactDeltas.get(i);
442 if (!rawContactDelta.hasMimeEntries(Photo.CONTENT_ITEM_TYPE)) {
443 continue;
444 }
445 final List<ValuesDelta> photosDeltas =
446 mRawContactDeltas.get(i).getMimeEntries(Photo.CONTENT_ITEM_TYPE);
447 if (photosDeltas == null) {
448 continue;
449 }
450 for (int j = 0; j < photosDeltas.size(); j++) {
451 photosDeltas.get(j).setSuperPrimary(false);
Walter Jang3f18d612015-10-07 16:01:05 -0700452 }
453 }
454 }
455
Walter Jang41b3ea12015-03-09 17:30:06 -0700456 /**
Walter Janga5e4bb22015-02-24 13:08:16 -0800457 * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800458 */
459 public boolean isWritablePhotoSet() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700460 return mPhotoView.isWritablePhotoSet();
Walter Jang3efae4a2015-02-18 11:12:00 -0800461 }
462
463 /**
Walter Jang3efae4a2015-02-18 11:12:00 -0800464 * Get the raw contact ID for the CompactHeaderView photo.
465 */
Walter Jang3efae4a2015-02-18 11:12:00 -0800466 public long getPhotoRawContactId() {
467 return mPhotoRawContactId;
468 }
469
Walter Jang4f5594a2015-10-06 18:40:31 -0700470 public StructuredNameEditorView getPrimaryNameEditorView() {
Walter Jange3945952015-10-27 12:44:54 -0700471 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
472 return primaryNameKindSectionView == null
473 ? null : primaryNameKindSectionView.getPrimaryNameEditorView();
Walter Jang4f5594a2015-10-06 18:40:31 -0700474 }
475
Walter Jang31a74ad2015-10-02 19:17:39 -0700476
477 /**
478 * Marks the raw contact photo given as primary for the aggregate contact and updates the
479 * UI.
480 */
481 public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
Walter Jangda258ff2015-11-17 11:06:02 -0800482
483 // Update values delta
Gary Mai4ceabed2016-09-16 12:14:13 -0700484 final ValuesDelta valuesDelta = mCurrentRawContactDelta
485 .getSuperPrimaryEntry(Photo.CONTENT_ITEM_TYPE);
Walter Jang31a74ad2015-10-02 19:17:39 -0700486 valuesDelta.setFromTemplate(false);
Wenyi Wang9086fb92015-11-16 09:58:20 -0800487 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700488 valuesDelta.setSuperPrimary(true);
Walter Jangda258ff2015-11-17 11:06:02 -0800489
Walter Jang31a74ad2015-10-02 19:17:39 -0700490 // Update the UI
491 mPhotoView.setPhoto(valuesDelta, mMaterialPalette);
492 }
493
Walter Jangd35e5ef2015-02-24 09:18:16 -0800494 public View getAggregationAnchorView() {
Gary Mai710802c2016-09-19 15:05:44 -0700495 final StructuredNameEditorView nameEditorView = getPrimaryNameEditorView();
496 return nameEditorView != null ? nameEditorView.findViewById(R.id.anchor_view) : null;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800497 }
498
Walter Jangf10ca152015-09-22 15:23:55 -0700499 public void setGroupMetaData(Cursor groupMetaData) {
Gary Mai98868d32016-09-14 11:55:04 -0700500 final CompactKindSectionView groupKindSectionView =
501 mKindSectionViewMap.get(GroupMembership.CONTENT_ITEM_TYPE);
502 if (groupKindSectionView == null) {
503 return;
504 }
505 groupKindSectionView.setGroupMetaData(groupMetaData);
506 if (mIsExpanded) {
507 groupKindSectionView.setHideWhenEmpty(false);
508 groupKindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
Walter Jangf10ca152015-09-22 15:23:55 -0700509 }
510 }
511
Walter Jangf46abd82015-02-20 16:52:04 -0800512 public void setState(RawContactDeltaList rawContactDeltas,
Walter Jang06f73a12015-06-17 11:15:48 -0700513 MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
Walter Jange3945952015-10-27 12:44:54 -0700514 long photoId, boolean hasNewContact, boolean isUserProfile,
Walter Jang9a552372016-08-24 11:51:05 -0700515 AccountWithDataSet primaryAccount, long rawContactIdToDisplayAlone,
516 boolean rawContactDisplayAloneIsReadOnly,
517 boolean isEditingReadOnlyRawContactWithNewContact) {
Marcus Hagerottf04c9752016-09-08 15:22:57 -0700518
Walter Jang9a552372016-08-24 11:51:05 -0700519 mRawContactDeltas = rawContactDeltas;
520 mRawContactIdToDisplayAlone = rawContactIdToDisplayAlone;
521 mRawContactDisplayAloneIsReadOnly = rawContactDisplayAloneIsReadOnly;
522 mIsEditingReadOnlyRawContactWithNewContact = isEditingReadOnlyRawContactWithNewContact;
523
Gary Mai98868d32016-09-14 11:55:04 -0700524 mKindSectionViewMap.clear();
Walter Jangf5dfea42015-09-16 12:30:36 -0700525 mKindSectionViews.removeAllViews();
Walter Jang363d3fd2015-09-16 10:29:07 -0700526 mMoreFields.setVisibility(View.VISIBLE);
Walter Jangcab3dce2015-02-09 17:48:03 -0800527
Walter Jangf46abd82015-02-20 16:52:04 -0800528 mMaterialPalette = materialPalette;
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700529 mViewIdGenerator = viewIdGenerator;
Gary Mai98868d32016-09-14 11:55:04 -0700530 mAggregatePhotoId = photoId;
Tingting Wangf695eb32015-10-15 18:45:15 -0700531
Walter Jang708ea9e2015-09-10 15:42:05 -0700532 mHasNewContact = hasNewContact;
533 mIsUserProfile = isUserProfile;
534 mPrimaryAccount = primaryAccount;
535 if (mPrimaryAccount == null) {
536 mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount();
537 }
538 vlog("state: primary " + mPrimaryAccount);
Walter Jangcab3dce2015-02-09 17:48:03 -0800539
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700540 // Parse the given raw contact deltas
541 if (rawContactDeltas == null || rawContactDeltas.isEmpty()) {
542 elog("No raw contact deltas");
Walter Jang79658e12015-09-24 10:36:26 -0700543 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jangf5dfea42015-09-16 12:30:36 -0700544 return;
545 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700546 pickRawContactDelta();
547 parseRawContactDelta();
Walter Jang79658e12015-09-24 10:36:26 -0700548 if (mKindSectionDataMap.isEmpty()) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700549 elog("No kind section data parsed from RawContactDelta(s)");
Walter Jang79658e12015-09-24 10:36:26 -0700550 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700551 return;
552 }
Walter Jang82acd422015-10-17 14:01:27 -0700553
Gary Mai98868d32016-09-14 11:55:04 -0700554 final KindSectionData nameSectionData =
555 mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE);
556 // Ensure that a structured name and photo exists
557 if (nameSectionData != null) {
Walter Jang82acd422015-10-17 14:01:27 -0700558 final RawContactDelta rawContactDelta =
Gary Mai98868d32016-09-14 11:55:04 -0700559 nameSectionData.getRawContactDelta();
Walter Jang82acd422015-10-17 14:01:27 -0700560 RawContactModifier.ensureKindExists(
561 rawContactDelta,
562 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700563 StructuredName.CONTENT_ITEM_TYPE);
Walter Jang82acd422015-10-17 14:01:27 -0700564 RawContactModifier.ensureKindExists(
565 rawContactDelta,
566 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700567 Photo.CONTENT_ITEM_TYPE);
568 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700569
570 // Setup the view
Walter Jang363d3fd2015-09-16 10:29:07 -0700571 addPhotoView();
Gary Mai4ceabed2016-09-16 12:14:13 -0700572 if (isReadOnlyRawContact()) {
Walter Jang9a552372016-08-24 11:51:05 -0700573 // We're want to display the inputs fields for a single read only raw contact
574 addReadOnlyRawContactEditorViews();
Walter Jang9a552372016-08-24 11:51:05 -0700575 } else {
576 setupCompactEditorNormally();
577 }
Walter Jangd6753152015-10-02 09:23:13 -0700578 if (mListener != null) mListener.onEditorsBound();
Walter Jangcab3dce2015-02-09 17:48:03 -0800579 }
580
Walter Jang9a552372016-08-24 11:51:05 -0700581 private void setupCompactEditorNormally() {
Gary Mai98868d32016-09-14 11:55:04 -0700582 addAccountInfo();
Walter Jang9a552372016-08-24 11:51:05 -0700583 addKindSectionViews();
Walter Jangcab3dce2015-02-09 17:48:03 -0800584
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700585 mMoreFields.setVisibility(hasMoreFields() ? View.VISIBLE : View.GONE);
586
Walter Jang9a552372016-08-24 11:51:05 -0700587 if (mIsExpanded) showAllFields();
588 }
589
Gary Mai4ceabed2016-09-16 12:14:13 -0700590 private boolean isReadOnlyRawContact() {
591 return !mCurrentRawContactDelta.getAccountType(mAccountTypeManager).areContactsWritable();
Walter Jang9a552372016-08-24 11:51:05 -0700592 }
593
Gary Mai4ceabed2016-09-16 12:14:13 -0700594 private void pickRawContactDelta() {
Walter Jang363d3fd2015-09-16 10:29:07 -0700595 // Build the kind section data list map
Gary Mai4ceabed2016-09-16 12:14:13 -0700596 vlog("parse: " + mRawContactDeltas.size() + " rawContactDelta(s)");
597 for (int j = 0; j < mRawContactDeltas.size(); j++) {
598 final RawContactDelta rawContactDelta = mRawContactDeltas.get(j);
Walter Jang192a01c2015-09-22 15:23:55 -0700599 vlog("parse: " + j + " rawContactDelta" + rawContactDelta);
Walter Jang363d3fd2015-09-16 10:29:07 -0700600 if (rawContactDelta == null || !rawContactDelta.isVisible()) continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800601 final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
Walter Jang363d3fd2015-09-16 10:29:07 -0700602 if (accountType == null) continue;
Gary Mai4ceabed2016-09-16 12:14:13 -0700603
604 if (mRawContactIdToDisplayAlone > 0) {
605 // Look for the raw contact if specified.
606 if (rawContactDelta.getRawContactId().equals(mRawContactIdToDisplayAlone)) {
607 mCurrentRawContactDelta = rawContactDelta;
608 return;
Walter Jang192a01c2015-09-22 15:23:55 -0700609 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700610 } else if (mPrimaryAccount != null
611 && mPrimaryAccount.equals(rawContactDelta.getAccountWithDataSet())) {
612 // Otherwise try to find the one that matches the default.
613 mCurrentRawContactDelta = rawContactDelta;
614 return;
615 } else if (accountType.areContactsWritable()){
616 // TODO: Find better raw contact delta
617 // Just select an arbitrary writable contact.
618 mCurrentRawContactDelta = rawContactDelta;
Walter Jangac679af2015-06-01 12:17:06 -0700619 }
620 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700621
622 }
623
624 private void parseRawContactDelta() {
625 mKindSectionDataMap.clear();
626 mSortedMimetypes.clear();
627
628 final AccountType accountType = mCurrentRawContactDelta.getAccountType(mAccountTypeManager);
629 final List<DataKind> dataKinds = accountType.getSortedDataKinds();
630 final int dataKindSize = dataKinds == null ? 0 : dataKinds.size();
631 vlog("parse: " + dataKindSize + " dataKinds(s)");
632
633 for (int i = 0; i < dataKindSize; i++) {
634 final DataKind dataKind = dataKinds.get(i);
635 if (dataKind == null) {
636 vlog("parse: " + i + " " + dataKind.mimeType + " dropped null data kind");
637 continue;
638 }
639 final String mimeType = dataKind.mimeType;
640
641 // Skip psuedo mime types
642 if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
643 || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
644 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
645 continue;
646 }
647
648 // Skip custom fields
649 // TODO: Handle them when we implement editing custom fields.
650 if (CustomDataItem.MIMETYPE_CUSTOM_FIELD.equals(mimeType)) {
651 vlog("parse: " + i + " " + dataKind.mimeType + " dropped custom field");
652 continue;
653 }
654
655 final KindSectionData kindSectionData =
656 new KindSectionData(accountType, dataKind, mCurrentRawContactDelta);
657 mKindSectionDataMap.put(mimeType, kindSectionData);
658 mSortedMimetypes.add(mimeType);
659
660 vlog("parse: " + i + " " + dataKind.mimeType + " " +
661 kindSectionData.getValuesDeltas().size() + " value(s) " +
662 kindSectionData.getNonEmptyValuesDeltas().size() + " non-empty value(s) " +
663 kindSectionData.getVisibleValuesDeltas().size() +
664 " visible value(s)");
665 }
Walter Jang3efae4a2015-02-18 11:12:00 -0800666 }
667
Walter Jang9a552372016-08-24 11:51:05 -0700668 private void addReadOnlyRawContactEditorViews() {
Gary Mai65971d02016-09-15 15:00:16 -0700669 mKindSectionViews.removeAllViews();
Gary Mai4ceabed2016-09-16 12:14:13 -0700670 addAccountInfo();
Walter Jang9a552372016-08-24 11:51:05 -0700671 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
672 getContext());
Gary Mai4ceabed2016-09-16 12:14:13 -0700673 final AccountType type = mCurrentRawContactDelta.getAccountType(accountTypes);
Walter Jang9a552372016-08-24 11:51:05 -0700674
Gary Mai65971d02016-09-15 15:00:16 -0700675 // Bail if invalid state or source
Gary Mai4ceabed2016-09-16 12:14:13 -0700676 if (type == null) return;
Walter Jang9a552372016-08-24 11:51:05 -0700677
Gary Mai65971d02016-09-15 15:00:16 -0700678 // Make sure we have StructuredName
Gary Mai4ceabed2016-09-16 12:14:13 -0700679 RawContactModifier.ensureKindExists(
680 mCurrentRawContactDelta, type, StructuredName.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700681
682 ValuesDelta primary;
683
684 // Name
685 final Context context = getContext();
686 final Resources res = context.getResources();
Gary Mai4ceabed2016-09-16 12:14:13 -0700687 primary = mCurrentRawContactDelta.getPrimaryEntry(StructuredName.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700688 final String name = primary != null ? primary.getAsString(StructuredName.DISPLAY_NAME) :
689 getContext().getString(R.string.missing_name);
690 final Drawable nameDrawable = context.getDrawable(R.drawable.ic_person_24dp);
691 final String nameContentDescription = res.getString(R.string.header_name_entry);
692 bindData(nameDrawable, nameContentDescription, name, /* type */ null,
693 /* isFirstEntry */ true);
694
695 // Phones
Gary Mai4ceabed2016-09-16 12:14:13 -0700696 final ArrayList<ValuesDelta> phones = mCurrentRawContactDelta
697 .getMimeEntries(Phone.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700698 final Drawable phoneDrawable = context.getDrawable(R.drawable.ic_phone_24dp);
699 final String phoneContentDescription = res.getString(R.string.header_phone_entry);
700 if (phones != null) {
701 boolean isFirstPhoneBound = true;
702 for (ValuesDelta phone : phones) {
703 final String phoneNumber = phone.getPhoneNumber();
704 if (TextUtils.isEmpty(phoneNumber)) {
705 continue;
706 }
707 final String formattedNumber = PhoneNumberUtilsCompat.formatNumber(
708 phoneNumber, phone.getPhoneNormalizedNumber(),
709 GeoUtil.getCurrentCountryIso(getContext()));
710 CharSequence phoneType = null;
711 if (phone.hasPhoneType()) {
712 phoneType = Phone.getTypeLabel(
713 res, phone.getPhoneType(), phone.getPhoneLabel());
714 }
715 bindData(phoneDrawable, phoneContentDescription, formattedNumber, phoneType,
716 isFirstPhoneBound, true);
717 isFirstPhoneBound = false;
718 }
719 }
720
721 // Emails
Gary Mai4ceabed2016-09-16 12:14:13 -0700722 final ArrayList<ValuesDelta> emails = mCurrentRawContactDelta
723 .getMimeEntries(Email.CONTENT_ITEM_TYPE);
Gary Mai65971d02016-09-15 15:00:16 -0700724 final Drawable emailDrawable = context.getDrawable(R.drawable.ic_email_24dp);
725 final String emailContentDescription = res.getString(R.string.header_email_entry);
726 if (emails != null) {
727 boolean isFirstEmailBound = true;
728 for (ValuesDelta email : emails) {
729 final String emailAddress = email.getEmailData();
730 if (TextUtils.isEmpty(emailAddress)) {
731 continue;
732 }
733 CharSequence emailType = null;
734 if (email.hasEmailType()) {
735 emailType = Email.getTypeLabel(
736 res, email.getEmailType(), email.getEmailLabel());
737 }
738 bindData(emailDrawable, emailContentDescription, emailAddress, emailType,
739 isFirstEmailBound);
740 isFirstEmailBound = false;
741 }
742 }
743
744 mKindSectionViews.setVisibility(mKindSectionViews.getChildCount() > 0 ? VISIBLE : GONE);
745 // Hide the "More fields" link
746 mMoreFields.setVisibility(GONE);
747 }
748
Gary Mai65971d02016-09-15 15:00:16 -0700749 private void bindData(Drawable icon, String iconContentDescription, CharSequence data,
750 CharSequence type, boolean isFirstEntry) {
751 bindData(icon, iconContentDescription, data, type, isFirstEntry, false);
752 }
753
754 private void bindData(Drawable icon, String iconContentDescription, CharSequence data,
755 CharSequence type, boolean isFirstEntry, boolean forceLTR) {
756 final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
757 Context.LAYOUT_INFLATER_SERVICE);
758 final View field = inflater.inflate(R.layout.item_read_only_field, mKindSectionViews,
759 false);
760 if (isFirstEntry) {
761 final ImageView imageView = (ImageView) field.findViewById(R.id.kind_icon);
762 imageView.setImageDrawable(icon);
763 imageView.setContentDescription(iconContentDescription);
764 } else {
765 final ImageView imageView = (ImageView) field.findViewById(R.id.kind_icon);
766 imageView.setVisibility(View.INVISIBLE);
767 imageView.setContentDescription(null);
768 }
769 final TextView dataView = (TextView) field.findViewById(R.id.data);
770 dataView.setText(data);
771 if (forceLTR) {
772 dataView.setTextDirection(View.TEXT_DIRECTION_LTR);
773 }
774 final TextView typeView = (TextView) field.findViewById(R.id.type);
775 if (!TextUtils.isEmpty(type)) {
776 typeView.setText(type);
777 } else {
778 typeView.setVisibility(View.GONE);
779 }
780 mKindSectionViews.addView(field);
Walter Jang9a552372016-08-24 11:51:05 -0700781 }
782
Gary Mai98868d32016-09-14 11:55:04 -0700783 private void addAccountInfo() {
Walter Jang82acd422015-10-17 14:01:27 -0700784 mAccountHeaderContainer.setVisibility(View.GONE);
Walter Jang82acd422015-10-17 14:01:27 -0700785 mRawContactContainer.setVisibility(View.GONE);
Walter Jang708ea9e2015-09-10 15:42:05 -0700786
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700787 final AccountDisplayInfo account =
Gary Mai4ceabed2016-09-16 12:14:13 -0700788 mAccountDisplayInfoFactory.getAccountDisplayInfoFor(mCurrentRawContactDelta);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700789
Walter Jang708ea9e2015-09-10 15:42:05 -0700790 // Get the account information for the primary raw contact delta
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700791 final String accountLabel = mIsUserProfile
792 ? EditorUiUtils.getAccountHeaderLabelForMyProfile(getContext(), account)
793 : account.getNameLabel().toString();
Walter Jang708ea9e2015-09-10 15:42:05 -0700794
Walter Jang23709542015-10-22 12:50:58 -0700795 // Either the account header or selector should be shown, not both.
Walter Jang708ea9e2015-09-10 15:42:05 -0700796 final List<AccountWithDataSet> accounts =
797 AccountTypeManager.getInstance(getContext()).getAccounts(true);
Gary Mai4ceabed2016-09-16 12:14:13 -0700798
Walter Jang82acd422015-10-17 14:01:27 -0700799 if (mHasNewContact && !mIsUserProfile) {
800 if (accounts.size() > 1) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700801 addAccountSelector(mCurrentRawContactDelta, accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700802 } else {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700803 addAccountHeader(accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700804 }
Gary Mai98868d32016-09-14 11:55:04 -0700805 } else if (mIsUserProfile || !shouldHideAccountContainer(mRawContactDeltas)) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700806 addAccountHeader(accountLabel);
Walter Jang708ea9e2015-09-10 15:42:05 -0700807 }
Walter Jang23709542015-10-22 12:50:58 -0700808
809 // The raw contact selector should only display linked raw contacts that can be edited in
810 // the full editor (i.e. they are not newly created raw contacts)
Tingting Wang2bb85d22015-10-23 13:01:09 -0700811 final RawContactAccountListAdapter adapter = new RawContactAccountListAdapter(getContext(),
Gary Mai98868d32016-09-14 11:55:04 -0700812 getRawContactDeltaListForSelector(mRawContactDeltas));
Gary Mai4ceabed2016-09-16 12:14:13 -0700813 if (adapter.getCount() > 0 && !mIsEditingReadOnlyRawContactWithNewContact) {
814 addRawContactAccountSelector(accountLabel, adapter);
Walter Jang23709542015-10-22 12:50:58 -0700815 }
Walter Jang708ea9e2015-09-10 15:42:05 -0700816 }
817
Tingting Wang2bb85d22015-10-23 13:01:09 -0700818 private RawContactDeltaList getRawContactDeltaListForSelector(
819 RawContactDeltaList rawContactDeltas) {
820 // Sort raw contacts so google accounts come first
821 Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
822
823 final RawContactDeltaList result = new RawContactDeltaList();
Gary Mai98868d32016-09-14 11:55:04 -0700824 for (int i = 0; i < rawContactDeltas.size(); i++) {
825 final RawContactDelta rawContactDelta = rawContactDeltas.get(i);
Tingting Wang2bb85d22015-10-23 13:01:09 -0700826 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
827 // Only add raw contacts that can be opened in the editor
828 result.add(rawContactDelta);
829 }
830 }
831 // Don't return a list of size 1 that would just open the raw contact being edited
832 // in the compact editor in the full editor
833 if (result.size() == 1 && result.get(0).getRawContactAccountType(
834 getContext()).areContactsWritable()) {
835 result.clear();
836 return result;
837 }
838 return result;
839 }
840
Tingting Wang1594c6f2015-10-26 17:39:16 -0700841 // Returns true if there are multiple writable rawcontacts and no read-only ones,
842 // or there are both writable and read-only rawcontacts.
843 private boolean shouldHideAccountContainer(RawContactDeltaList rawContactDeltas) {
Tingting Wang2bb85d22015-10-23 13:01:09 -0700844 int writable = 0;
845 int readonly = 0;
846 for (RawContactDelta rawContactDelta : rawContactDeltas) {
847 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
848 if (rawContactDelta.getRawContactAccountType(getContext()).areContactsWritable()) {
849 writable++;
850 } else {
851 readonly++;
852 }
853 }
854 }
855 return (writable > 1 || (writable > 0 && readonly > 0));
856 }
857
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700858 private void addAccountHeader(String accountLabel) {
Walter Jang82acd422015-10-17 14:01:27 -0700859 mAccountHeaderContainer.setVisibility(View.VISIBLE);
860
Walter Jangbe5e1b12015-10-17 11:38:29 -0700861 // Set the account name
Walter Jangbe5e1b12015-10-17 11:38:29 -0700862 mAccountHeaderName.setVisibility(View.VISIBLE);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700863 mAccountHeaderName.setText(accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700864
865 // Set the account type
Gary Mai4ceabed2016-09-16 12:14:13 -0700866 final String selectorTitle = getResources().getString(isReadOnlyRawContact() ?
867 R.string.compact_editor_account_selector_read_only_title :
Walter Jangbe5e1b12015-10-17 11:38:29 -0700868 R.string.compact_editor_account_selector_title);
869 mAccountHeaderType.setText(selectorTitle);
870
871 // Set the icon
Gary Mai4ceabed2016-09-16 12:14:13 -0700872 final AccountType accountType =
873 mCurrentRawContactDelta.getRawContactAccountType(getContext());
874 mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext()));
Walter Jangbe5e1b12015-10-17 11:38:29 -0700875
876 // Set the content description
877 mAccountHeaderContainer.setContentDescription(
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700878 EditorUiUtils.getAccountInfoContentDescription(accountLabel,
879 selectorTitle));
Walter Jangbe5e1b12015-10-17 11:38:29 -0700880 }
881
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700882 private void addAccountSelector(final RawContactDelta rawContactDelta, CharSequence nameLabel) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700883 final View.OnClickListener onClickListener = new View.OnClickListener() {
Walter Jang708ea9e2015-09-10 15:42:05 -0700884 @Override
885 public void onClick(View v) {
886 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
887 final AccountsListAdapter adapter =
888 new AccountsListAdapter(getContext(),
889 AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE,
890 mPrimaryAccount);
Gary Mai46cb3102016-08-10 18:14:09 -0700891 popup.setWidth(mAccountHeaderContainer.getWidth());
892 popup.setAnchorView(mAccountHeaderContainer);
Walter Jang708ea9e2015-09-10 15:42:05 -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);
901 final AccountWithDataSet newAccount = adapter.getItem(position);
902 if (mListener != null && !mPrimaryAccount.equals(newAccount)) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700903 mIsExpanded = false;
Walter Jang708ea9e2015-09-10 15:42:05 -0700904 mListener.onRebindEditorsForNewContact(
Walter Jang82acd422015-10-17 14:01:27 -0700905 rawContactDelta,
Walter Jang708ea9e2015-09-10 15:42:05 -0700906 mPrimaryAccount,
907 newAccount);
908 }
909 }
910 });
911 popup.show();
912 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700913 };
914 setUpAccountSelector(nameLabel.toString(), onClickListener);
Walter Jang708ea9e2015-09-10 15:42:05 -0700915 }
916
Gary Mai4ceabed2016-09-16 12:14:13 -0700917 private void addRawContactAccountSelector(String nameLabel,
Walter Jang23709542015-10-22 12:50:58 -0700918 final RawContactAccountListAdapter adapter) {
Gary Mai4ceabed2016-09-16 12:14:13 -0700919 final View.OnClickListener onClickListener = new OnClickListener() {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700920 @Override
921 public void onClick(View v) {
922 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
Gary Mai4ceabed2016-09-16 12:14:13 -0700923 popup.setWidth(mAccountHeaderContainer.getWidth());
924 popup.setAnchorView(mAccountHeaderContainer);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700925 popup.setAdapter(adapter);
926 popup.setModal(true);
927 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
928 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
929 @Override
930 public void onItemClick(AdapterView<?> parent, View view, int position,
931 long id) {
932 UiClosables.closeQuietly(popup);
Gary Mai4ceabed2016-09-16 12:14:13 -0700933 final long rawContactId = adapter.getItemId(position);
934 // Only switch if it's actually a different raw contact.
935 if (rawContactId != mCurrentRawContactDelta.getRawContactId()
936 && mListener != null) {
Tingting Wang91cee282015-10-07 13:48:17 -0700937 final RawContactDelta rawContactDelta = adapter.getItem(position);
938 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
939 getContext());
940 final AccountType accountType = rawContactDelta.getAccountType(
941 accountTypes);
942 final boolean isReadOnly = !accountType.areContactsWritable();
Gary Mai4ceabed2016-09-16 12:14:13 -0700943 // Reset state.
944 mIsExpanded = false;
945 mListener.onRawContactSelected(rawContactId, isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700946 }
947 }
948 });
949 popup.show();
950 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700951 };
952 setUpAccountSelector(nameLabel, onClickListener);
953 }
954
955 private void setUpAccountSelector(String nameLabel, OnClickListener listener) {
956 addAccountHeader(nameLabel);
957 // Add handlers for choosing another account to save to.
958 mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE);
959 mAccountHeaderContainer.setOnClickListener(listener);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700960 }
961
Walter Jang363d3fd2015-09-16 10:29:07 -0700962 private void addPhotoView() {
Gary Mai4ceabed2016-09-16 12:14:13 -0700963 if (!mCurrentRawContactDelta.hasMimeEntries(Photo.CONTENT_ITEM_TYPE)) {
964 wlog("No photo mimetype for this raw contact.");
Walter Jangcbd431d2015-10-06 13:07:30 -0700965 mPhotoView.setVisibility(View.GONE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700966 return;
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700967 } else {
968 mPhotoView.setVisibility(View.VISIBLE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700969 }
Walter Jang06f73a12015-06-17 11:15:48 -0700970
Gary Mai4ceabed2016-09-16 12:14:13 -0700971 final ValuesDelta superPrimaryDelta = mCurrentRawContactDelta
972 .getSuperPrimaryEntry(Photo.CONTENT_ITEM_TYPE);
Walter Jang31a74ad2015-10-02 19:17:39 -0700973 // Set the photo view
Gary Mai4ceabed2016-09-16 12:14:13 -0700974 mPhotoView.setPhoto(superPrimaryDelta, mMaterialPalette);
Walter Jang31a74ad2015-10-02 19:17:39 -0700975
Gary Mai4ceabed2016-09-16 12:14:13 -0700976 if (isReadOnlyRawContact()) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700977 mPhotoView.setReadOnly(true);
978 return;
979 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700980 mPhotoView.setReadOnly(false);
Gary Mai4ceabed2016-09-16 12:14:13 -0700981 mPhotoRawContactId = mCurrentRawContactDelta.getRawContactId();
982 mPhotoValuesDelta = superPrimaryDelta;
Walter Jang31a74ad2015-10-02 19:17:39 -0700983 }
984
Walter Jangf5dfea42015-09-16 12:30:36 -0700985 private void addKindSectionViews() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700986 int i = -1;
Gary Mai98868d32016-09-14 11:55:04 -0700987
988 for (String mimeType : mSortedMimetypes) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700989 i++;
Gary Mai4ceabed2016-09-16 12:14:13 -0700990 // Ignore mime types that we've already handled
991 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
992 vlog("kind: " + i + " " + mimeType + " dropped");
993 continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800994 }
Gary Mai4ceabed2016-09-16 12:14:13 -0700995 final CompactKindSectionView kindSectionView;
996 final KindSectionData kindSectionData = mKindSectionDataMap.get(mimeType);
997 final ValuesDelta primaryDelta = mCurrentRawContactDelta.getPrimaryEntry(mimeType);
998 kindSectionView = inflateKindSectionView(mKindSectionViews, kindSectionData, mimeType,
999 primaryDelta);
Gary Mai98868d32016-09-14 11:55:04 -07001000 mKindSectionViews.addView(kindSectionView);
Walter Jangcab3dce2015-02-09 17:48:03 -08001001
Gary Mai98868d32016-09-14 11:55:04 -07001002 // Keep a pointer to the KindSectionView for each mimeType
1003 mKindSectionViewMap.put(mimeType, kindSectionView);
Walter Jangf10ca152015-09-22 15:23:55 -07001004 }
Walter Jangf10ca152015-09-22 15:23:55 -07001005 }
1006
Walter Jangf5dfea42015-09-16 12:30:36 -07001007 private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup,
Gary Mai98868d32016-09-14 11:55:04 -07001008 KindSectionData kindSectionData, String mimeType,
Walter Jang82acd422015-10-17 14:01:27 -07001009 ValuesDelta primaryValuesDelta) {
Walter Jangf5dfea42015-09-16 12:30:36 -07001010 final CompactKindSectionView kindSectionView = (CompactKindSectionView)
1011 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
1012 /* attachToRoot =*/ false);
Walter Jang45b86d52015-10-15 15:23:16 -07001013 kindSectionView.setIsUserProfile(mIsUserProfile);
Walter Jangf5dfea42015-09-16 12:30:36 -07001014
Walter Jang192a01c2015-09-22 15:23:55 -07001015 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Walter Jangf5dfea42015-09-16 12:30:36 -07001016 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001017 // Phone numbers and email addresses are always displayed,
1018 // even if they are empty
Walter Jangf5dfea42015-09-16 12:30:36 -07001019 kindSectionView.setHideWhenEmpty(false);
1020 }
Walter Jangf5dfea42015-09-16 12:30:36 -07001021
Walter Jang192a01c2015-09-22 15:23:55 -07001022 // Since phone numbers and email addresses displayed even if they are empty,
1023 // they will be the only types you add new values to initially for new contacts
1024 kindSectionView.setShowOneEmptyEditor(true);
1025
Gary Mai98868d32016-09-14 11:55:04 -07001026 kindSectionView.setState(kindSectionData, mViewIdGenerator, mListener,
Walter Jang82acd422015-10-17 14:01:27 -07001027 primaryValuesDelta);
Walter Jangf5dfea42015-09-16 12:30:36 -07001028
1029 return kindSectionView;
Walter Jangcab3dce2015-02-09 17:48:03 -08001030 }
1031
Walter Jange3945952015-10-27 12:44:54 -07001032 void maybeSetReadOnlyDisplayNameAsPrimary(String readOnlyDisplayName) {
1033 if (TextUtils.isEmpty(readOnlyDisplayName)) return;
1034 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
1035 if (primaryNameKindSectionView != null && primaryNameKindSectionView.isEmptyName()) {
1036 vlog("name: using read only display name as primary name");
1037 primaryNameKindSectionView.setName(readOnlyDisplayName);
1038 }
1039 }
1040
1041 private CompactKindSectionView getPrimaryNameKindSectionView() {
Gary Mai98868d32016-09-14 11:55:04 -07001042 return mKindSectionViewMap.get(StructuredName.CONTENT_ITEM_TYPE);
Walter Jang4f5594a2015-10-06 18:40:31 -07001043 }
1044
Walter Jangd6753152015-10-02 09:23:13 -07001045 private void showAllFields() {
Walter Jang79658e12015-09-24 10:36:26 -07001046 // Stop hiding empty editors and allow the user to enter values for all kinds now
1047 for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
1048 final CompactKindSectionView kindSectionView =
1049 (CompactKindSectionView) mKindSectionViews.getChildAt(i);
1050 kindSectionView.setHideWhenEmpty(false);
1051 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
1052 }
1053 mIsExpanded = true;
Walter Jang79658e12015-09-24 10:36:26 -07001054
Walter Jangf5dfea42015-09-16 12:30:36 -07001055 // Hide the more fields button
1056 mMoreFields.setVisibility(View.GONE);
Walter Jangcab3dce2015-02-09 17:48:03 -08001057 }
1058
Marcus Hagerottcf7f2952016-09-06 13:49:17 -07001059 private boolean hasMoreFields() {
Gary Mai98868d32016-09-14 11:55:04 -07001060 for (CompactKindSectionView section : mKindSectionViewMap.values()) {
1061 if (section.getVisibility() != View.VISIBLE) {
1062 return true;
Marcus Hagerottcf7f2952016-09-06 13:49:17 -07001063 }
1064 }
1065 return false;
1066 }
1067
Walter Jangbf63a6d2015-05-05 09:14:35 -07001068 private static void vlog(String message) {
1069 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1070 Log.v(TAG, message);
Walter Jangcab3dce2015-02-09 17:48:03 -08001071 }
1072 }
Walter Jang363d3fd2015-09-16 10:29:07 -07001073
1074 private static void wlog(String message) {
1075 if (Log.isLoggable(TAG, Log.WARN)) {
1076 Log.w(TAG, message);
1077 }
1078 }
1079
1080 private static void elog(String message) {
1081 Log.e(TAG, message);
1082 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001083}