blob: f46516797fca2301a53d08402bfb0f4efe37da75 [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
Marcus Hagerottfac695a2016-08-24 17:02:40 -070019import com.android.contacts.common.model.account.AccountDisplayInfo;
20import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Walter Jangcab3dce2015-02-09 17:48:03 -080021import com.android.contacts.R;
22import com.android.contacts.common.model.AccountTypeManager;
23import com.android.contacts.common.model.RawContactDelta;
24import com.android.contacts.common.model.RawContactDeltaList;
Walter Jange720fde2015-02-17 10:54:14 -080025import com.android.contacts.common.model.RawContactModifier;
Walter Jangcab3dce2015-02-09 17:48:03 -080026import com.android.contacts.common.model.ValuesDelta;
27import com.android.contacts.common.model.account.AccountType;
Walter Jang2d3f31c2015-06-18 23:15:31 -070028import com.android.contacts.common.model.account.AccountWithDataSet;
Walter Jangcab3dce2015-02-09 17:48:03 -080029import com.android.contacts.common.model.dataitem.DataKind;
Walter Jang708ea9e2015-09-10 15:42:05 -070030import com.android.contacts.common.util.AccountsListAdapter;
Walter Jangf46abd82015-02-20 16:52:04 -080031import com.android.contacts.common.util.MaterialColorMapUtils;
Walter Jang708ea9e2015-09-10 15:42:05 -070032import com.android.contacts.util.UiClosables;
Walter Jangcab3dce2015-02-09 17:48:03 -080033
Marcus Hagerottcf7f2952016-09-06 13:49:17 -070034import android.animation.LayoutTransition;
Tingting Wang655ad1a2015-10-05 17:51:14 -070035import android.content.ContentUris;
Walter Jangcab3dce2015-02-09 17:48:03 -080036import android.content.Context;
Walter Jangf10ca152015-09-22 15:23:55 -070037import android.database.Cursor;
Walter Jang3efae4a2015-02-18 11:12:00 -080038import android.graphics.Bitmap;
Walter Jang41b3ea12015-03-09 17:30:06 -070039import android.net.Uri;
Walter Jang3f18d612015-10-07 16:01:05 -070040import android.os.Bundle;
Walter Jang79658e12015-09-24 10:36:26 -070041import android.os.Parcel;
42import android.os.Parcelable;
Tingting Wang655ad1a2015-10-05 17:51:14 -070043import android.provider.ContactsContract;
Walter Jangcab3dce2015-02-09 17:48:03 -080044import android.provider.ContactsContract.CommonDataKinds.Email;
Walter Jangf5dfea42015-09-16 12:30:36 -070045import android.provider.ContactsContract.CommonDataKinds.Event;
Walter Jangcab3dce2015-02-09 17:48:03 -080046import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
Walter Jangf5dfea42015-09-16 12:30:36 -070047import android.provider.ContactsContract.CommonDataKinds.Im;
Walter Jang3efae4a2015-02-18 11:12:00 -080048import android.provider.ContactsContract.CommonDataKinds.Nickname;
Walter Jangf5dfea42015-09-16 12:30:36 -070049import android.provider.ContactsContract.CommonDataKinds.Note;
50import android.provider.ContactsContract.CommonDataKinds.Organization;
Walter Jangcab3dce2015-02-09 17:48:03 -080051import android.provider.ContactsContract.CommonDataKinds.Phone;
52import android.provider.ContactsContract.CommonDataKinds.Photo;
Walter Jangf5dfea42015-09-16 12:30:36 -070053import android.provider.ContactsContract.CommonDataKinds.Relation;
54import android.provider.ContactsContract.CommonDataKinds.SipAddress;
Walter Jangcab3dce2015-02-09 17:48:03 -080055import android.provider.ContactsContract.CommonDataKinds.StructuredName;
Walter Jangf5dfea42015-09-16 12:30:36 -070056import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
57import android.provider.ContactsContract.CommonDataKinds.Website;
Walter Jangcab3dce2015-02-09 17:48:03 -080058import android.text.TextUtils;
59import android.util.AttributeSet;
60import android.util.Log;
Walter Jang2d3f31c2015-06-18 23:15:31 -070061import android.util.Pair;
Walter Jangcab3dce2015-02-09 17:48:03 -080062import android.view.LayoutInflater;
Walter Jangb1c87622015-02-13 17:51:38 -080063import android.view.View;
Walter Jangcab3dce2015-02-09 17:48:03 -080064import android.view.ViewGroup;
Walter Jang708ea9e2015-09-10 15:42:05 -070065import android.widget.AdapterView;
Tingting Wang655ad1a2015-10-05 17:51:14 -070066import android.widget.BaseAdapter;
67import android.widget.ImageView;
Walter Jangcab3dce2015-02-09 17:48:03 -080068import android.widget.LinearLayout;
Walter Jang708ea9e2015-09-10 15:42:05 -070069import android.widget.ListPopupWindow;
Walter Jang2d3f31c2015-06-18 23:15:31 -070070import android.widget.TextView;
Walter Jangcab3dce2015-02-09 17:48:03 -080071
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;
Walter Jangf5dfea42015-09-16 12:30:36 -070080import java.util.TreeSet;
Walter Jangcab3dce2015-02-09 17:48:03 -080081
82/**
Walter Jangf5dfea42015-09-16 12:30:36 -070083 * View to display information from multiple {@link RawContactDelta}s grouped together.
Walter Jangcab3dce2015-02-09 17:48:03 -080084 */
Walter Jangb6ca2722015-02-20 11:10:25 -080085public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener {
Walter Jangcab3dce2015-02-09 17:48:03 -080086
Walter Jang4f5594a2015-10-06 18:40:31 -070087 static final String TAG = "CompactEditorView";
Walter Jangcab3dce2015-02-09 17:48:03 -080088
Walter Jang192a01c2015-09-22 15:23:55 -070089 private static final KindSectionDataMapEntryComparator
90 KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR = new KindSectionDataMapEntryComparator();
Walter Jangf5dfea42015-09-16 12:30:36 -070091
Walter Jangb6ca2722015-02-20 11:10:25 -080092 /**
93 * Callbacks for hosts of {@link CompactRawContactsEditorView}s.
94 */
95 public interface Listener {
96
97 /**
Walter Jang151f3e62015-02-26 15:29:40 -080098 * Invoked when the structured name editor field has changed.
99 *
100 * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}.
101 * @param valuesDelta The values from the underlying {@link RawContactDelta}.
102 */
103 public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta);
Walter Jang708ea9e2015-09-10 15:42:05 -0700104
105 /**
106 * Invoked when the compact editor should rebind editors for a new account.
107 *
108 * @param oldState Old data being edited.
109 * @param oldAccount Old account associated with oldState.
110 * @param newAccount New account to be used.
111 */
112 public void onRebindEditorsForNewContact(RawContactDelta oldState,
113 AccountWithDataSet oldAccount, AccountWithDataSet newAccount);
Walter Jang79658e12015-09-24 10:36:26 -0700114
115 /**
116 * Invoked when no editors could be bound for the contact.
117 */
118 public void onBindEditorsFailed();
Walter Jangd6753152015-10-02 09:23:13 -0700119
120 /**
121 * Invoked after editors have been bound for the contact.
122 */
123 public void onEditorsBound();
Tingting Wang655ad1a2015-10-05 17:51:14 -0700124
125 /**
Tingting Wang5585c6e2015-10-14 11:19:58 -0700126 * Invoked when a rawcontact from linked contacts is selected in editor.
Tingting Wang655ad1a2015-10-05 17:51:14 -0700127 */
Tingting Wang91cee282015-10-07 13:48:17 -0700128 public void onRawContactSelected(Uri uri, long rawContactId, boolean isReadOnly);
Walter Jang3f18d612015-10-07 16:01:05 -0700129
130 /**
131 * Returns the map of raw contact IDs to newly taken or selected photos that have not
132 * yet been saved to CP2.
133 */
134 public Bundle getUpdatedPhotos();
Tingting Wang655ad1a2015-10-05 17:51:14 -0700135 }
136
137 /**
138 * Used to list the account info for the given raw contacts list.
139 */
140 private static final class RawContactAccountListAdapter extends BaseAdapter {
141 private final LayoutInflater mInflater;
142 private final Context mContext;
143 private final RawContactDeltaList mRawContactDeltas;
144
145 public RawContactAccountListAdapter(Context context, RawContactDeltaList rawContactDeltas) {
146 mContext = context;
147 mRawContactDeltas = new RawContactDeltaList();
148 for (RawContactDelta rawContactDelta : rawContactDeltas) {
Walter Jang23709542015-10-22 12:50:58 -0700149 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700150 mRawContactDeltas.add(rawContactDelta);
151 }
152 }
153 mInflater = LayoutInflater.from(context);
154 }
155
156 @Override
157 public View getView(int position, View convertView, ViewGroup parent) {
158 final View resultView = convertView != null ? convertView
159 : mInflater.inflate(R.layout.account_selector_list_item, parent, false);
160
161 final RawContactDelta rawContactDelta = mRawContactDeltas.get(position);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700162
163 final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
Walter Jang9488a762015-10-16 13:42:48 -0700164 final AccountType accountType = rawContactDelta.getRawContactAccountType(mContext);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700165 text1.setText(accountType.getDisplayLabel(mContext));
166
Tingting Wang655ad1a2015-10-05 17:51:14 -0700167 final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
Walter Jang9488a762015-10-16 13:42:48 -0700168 final String accountName = rawContactDelta.getAccountName();
Tingting Wanga0b69402015-10-13 14:27:58 -0700169 if (TextUtils.isEmpty(accountName)) {
170 text2.setVisibility(View.GONE);
171 } else {
Walter Jang9488a762015-10-16 13:42:48 -0700172 // Truncate email addresses in the middle so we don't lose the domain
Tingting Wanga0b69402015-10-13 14:27:58 -0700173 text2.setText(accountName);
174 text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
175 }
Tingting Wang655ad1a2015-10-05 17:51:14 -0700176
177 final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
178 icon.setImageDrawable(accountType.getDisplayIcon(mContext));
179
180 return resultView;
181 }
182
183 @Override
184 public int getCount() {
185 return mRawContactDeltas.size();
186 }
187
188 @Override
189 public RawContactDelta getItem(int position) {
190 return mRawContactDeltas.get(position);
191 }
192
193 @Override
194 public long getItemId(int position) {
195 return getItem(position).getRawContactId();
196 }
Walter Jang151f3e62015-02-26 15:29:40 -0800197 }
198
Walter Jang192a01c2015-09-22 15:23:55 -0700199 /** Used to sort entire kind sections. */
200 private static final class KindSectionDataMapEntryComparator implements
Walter Jangcbd431d2015-10-06 13:07:30 -0700201 Comparator<Map.Entry<String,KindSectionDataList>> {
Walter Jangf5dfea42015-09-16 12:30:36 -0700202
Walter Jang192a01c2015-09-22 15:23:55 -0700203 final MimeTypeComparator mMimeTypeComparator = new MimeTypeComparator();
204
205 @Override
Walter Jangcbd431d2015-10-06 13:07:30 -0700206 public int compare(Map.Entry<String, KindSectionDataList> entry1,
207 Map.Entry<String, KindSectionDataList> entry2) {
Walter Jang192a01c2015-09-22 15:23:55 -0700208 if (entry1 == entry2) return 0;
209 if (entry1 == null) return -1;
210 if (entry2 == null) return 1;
211
212 final String mimeType1 = entry1.getKey();
213 final String mimeType2 = entry2.getKey();
214
215 return mMimeTypeComparator.compare(mimeType1, mimeType2);
216 }
217 }
218
219 /**
220 * Sorts kinds roughly the same as quick contacts; we diverge in the following ways:
221 * <ol>
222 * <li>All names are together at the top.</li>
223 * <li>IM is moved up after addresses</li>
224 * <li>SIP addresses are moved to below phone numbers</li>
Walter Jang79658e12015-09-24 10:36:26 -0700225 * <li>Group membership is placed at the end</li>
Walter Jang192a01c2015-09-22 15:23:55 -0700226 * </ol>
227 */
228 private static final class MimeTypeComparator implements Comparator<String> {
229
Walter Jangf5dfea42015-09-16 12:30:36 -0700230 private static final List<String> MIME_TYPE_ORDER = Arrays.asList(new String[] {
231 StructuredName.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700232 Nickname.CONTENT_ITEM_TYPE,
Walter Jang2ae21752015-09-25 11:25:38 -0700233 Organization.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700234 Phone.CONTENT_ITEM_TYPE,
235 SipAddress.CONTENT_ITEM_TYPE,
236 Email.CONTENT_ITEM_TYPE,
237 StructuredPostal.CONTENT_ITEM_TYPE,
238 Im.CONTENT_ITEM_TYPE,
239 Website.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700240 Event.CONTENT_ITEM_TYPE,
241 Relation.CONTENT_ITEM_TYPE,
Walter Jangf10ca152015-09-22 15:23:55 -0700242 Note.CONTENT_ITEM_TYPE,
243 GroupMembership.CONTENT_ITEM_TYPE
Walter Jangf5dfea42015-09-16 12:30:36 -0700244 });
245
246 @Override
Walter Jang192a01c2015-09-22 15:23:55 -0700247 public int compare(String mimeType1, String mimeType2) {
248 if (mimeType1 == mimeType2) return 0;
249 if (mimeType1 == null) return -1;
250 if (mimeType2 == null) return 1;
Walter Jangf5dfea42015-09-16 12:30:36 -0700251
252 int index1 = MIME_TYPE_ORDER.indexOf(mimeType1);
253 int index2 = MIME_TYPE_ORDER.indexOf(mimeType2);
254
255 // Fallback to alphabetical ordering of the mime type if both are not found
256 if (index1 < 0 && index2 < 0) return mimeType1.compareTo(mimeType2);
257 if (index1 < 0) return 1;
258 if (index2 < 0) return -1;
259
260 return index1 < index2 ? -1 : 1;
261 }
262 }
263
Walter Jang192a01c2015-09-22 15:23:55 -0700264 /**
265 * Sorts primary accounts and google account types before others.
266 */
267 private static final class EditorComparator implements Comparator<KindSectionData> {
268
269 private RawContactDeltaComparator mRawContactDeltaComparator;
270
271 private EditorComparator(Context context) {
272 mRawContactDeltaComparator = new RawContactDeltaComparator(context);
273 }
274
275 @Override
276 public int compare(KindSectionData kindSectionData1, KindSectionData kindSectionData2) {
277 if (kindSectionData1 == kindSectionData2) return 0;
278 if (kindSectionData1 == null) return -1;
279 if (kindSectionData2 == null) return 1;
280
281 final RawContactDelta rawContactDelta1 = kindSectionData1.getRawContactDelta();
282 final RawContactDelta rawContactDelta2 = kindSectionData2.getRawContactDelta();
283
284 if (rawContactDelta1 == rawContactDelta2) return 0;
285 if (rawContactDelta1 == null) return -1;
286 if (rawContactDelta2 == null) return 1;
287
288 return mRawContactDeltaComparator.compare(rawContactDelta1, rawContactDelta2);
289 }
290 }
291
Walter Jang79658e12015-09-24 10:36:26 -0700292 public static class SavedState extends BaseSavedState {
293
294 public static final Parcelable.Creator<SavedState> CREATOR =
295 new Parcelable.Creator<SavedState>() {
296 public SavedState createFromParcel(Parcel in) {
297 return new SavedState(in);
298 }
299 public SavedState[] newArray(int size) {
300 return new SavedState[size];
301 }
302 };
303
304 private boolean mIsExpanded;
305
306 public SavedState(Parcelable superState) {
307 super(superState);
308 }
309
310 private SavedState(Parcel in) {
311 super(in);
312 mIsExpanded = in.readInt() != 0;
313 }
314
315 @Override
316 public void writeToParcel(Parcel out, int flags) {
317 super.writeToParcel(out, flags);
318 out.writeInt(mIsExpanded ? 1 : 0);
319 }
320 }
321
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700322 private CompactRawContactsEditorView.Listener mListener;
Walter Jangb6ca2722015-02-20 11:10:25 -0800323
Walter Jangcab3dce2015-02-09 17:48:03 -0800324 private AccountTypeManager mAccountTypeManager;
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700325 private AccountDisplayInfoFactory mAccountDisplayInfoFactory;
Walter Jangcab3dce2015-02-09 17:48:03 -0800326 private LayoutInflater mLayoutInflater;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800327
Walter Jangcab3dce2015-02-09 17:48:03 -0800328 private ViewIdGenerator mViewIdGenerator;
Walter Jangf46abd82015-02-20 16:52:04 -0800329 private MaterialColorMapUtils.MaterialPalette mMaterialPalette;
Walter Jang82acd422015-10-17 14:01:27 -0700330 private long mPhotoId = -1;
Walter Jang708ea9e2015-09-10 15:42:05 -0700331 private boolean mHasNewContact;
332 private boolean mIsUserProfile;
333 private AccountWithDataSet mPrimaryAccount;
Walter Jangcbd431d2015-10-06 13:07:30 -0700334 private Map<String,KindSectionDataList> mKindSectionDataMap = new HashMap<>();
Walter Jangcab3dce2015-02-09 17:48:03 -0800335
Walter Jang708ea9e2015-09-10 15:42:05 -0700336 // Account header
337 private View mAccountHeaderContainer;
338 private TextView mAccountHeaderType;
339 private TextView mAccountHeaderName;
Tingting Wang91cee282015-10-07 13:48:17 -0700340 private ImageView mAccountHeaderIcon;
Gary Mai46cb3102016-08-10 18:14:09 -0700341 private ImageView mAccountHeaderExpanderIcon;
Walter Jang708ea9e2015-09-10 15:42:05 -0700342
Tingting Wang655ad1a2015-10-05 17:51:14 -0700343 // Raw contacts selector
344 private View mRawContactContainer;
345 private TextView mRawContactSummary;
Tingting Wang655ad1a2015-10-05 17:51:14 -0700346
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700347 private CompactPhotoEditorView mPhotoView;
Walter Jangf5dfea42015-09-16 12:30:36 -0700348 private ViewGroup mKindSectionViews;
Walter Jangf10ca152015-09-22 15:23:55 -0700349 private Map<String,List<CompactKindSectionView>> mKindSectionViewsMap = new HashMap<>();
Walter Jangb6ca2722015-02-20 11:10:25 -0800350 private View mMoreFields;
Walter Jangcab3dce2015-02-09 17:48:03 -0800351
Walter Jang79658e12015-09-24 10:36:26 -0700352 private boolean mIsExpanded;
Walter Jangcbd431d2015-10-06 13:07:30 -0700353
Walter Jang3efae4a2015-02-18 11:12:00 -0800354 private long mPhotoRawContactId;
Walter Jang31a74ad2015-10-02 19:17:39 -0700355 private ValuesDelta mPhotoValuesDelta;
Walter Jang82acd422015-10-17 14:01:27 -0700356
357 private Pair<KindSectionData, ValuesDelta> mPrimaryNameKindSectionData;
Walter Jang3efae4a2015-02-18 11:12:00 -0800358
Walter Jangcab3dce2015-02-09 17:48:03 -0800359 public CompactRawContactsEditorView(Context context) {
360 super(context);
361 }
362
363 public CompactRawContactsEditorView(Context context, AttributeSet attrs) {
364 super(context, attrs);
365 }
366
Walter Jangb6ca2722015-02-20 11:10:25 -0800367 /**
368 * Sets the receiver for {@link CompactRawContactsEditorView} callbacks.
369 */
370 public void setListener(Listener listener) {
371 mListener = listener;
372 }
373
Walter Jangcab3dce2015-02-09 17:48:03 -0800374 @Override
375 protected void onFinishInflate() {
376 super.onFinishInflate();
377
378 mAccountTypeManager = AccountTypeManager.getInstance(getContext());
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700379 mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(getContext());
Walter Jangcab3dce2015-02-09 17:48:03 -0800380 mLayoutInflater = (LayoutInflater)
381 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
382
Walter Jang708ea9e2015-09-10 15:42:05 -0700383 // Account header
Gary Mai46cb3102016-08-10 18:14:09 -0700384 mAccountHeaderContainer = findViewById(R.id.account_header_container);
Walter Jang708ea9e2015-09-10 15:42:05 -0700385 mAccountHeaderType = (TextView) findViewById(R.id.account_type);
386 mAccountHeaderName = (TextView) findViewById(R.id.account_name);
Tingting Wang91cee282015-10-07 13:48:17 -0700387 mAccountHeaderIcon = (ImageView) findViewById(R.id.account_type_icon);
Gary Mai46cb3102016-08-10 18:14:09 -0700388 mAccountHeaderExpanderIcon = (ImageView) findViewById(R.id.account_expander_icon);
Walter Jang708ea9e2015-09-10 15:42:05 -0700389
Tingting Wang655ad1a2015-10-05 17:51:14 -0700390 // Raw contacts selector
391 mRawContactContainer = findViewById(R.id.all_rawcontacts_accounts_container);
392 mRawContactSummary = (TextView) findViewById(R.id.rawcontacts_accounts_summary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700393
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700394 mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor);
Walter Jangf5dfea42015-09-16 12:30:36 -0700395 mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views);
Walter Jangb6ca2722015-02-20 11:10:25 -0800396 mMoreFields = findViewById(R.id.more_fields);
397 mMoreFields.setOnClickListener(this);
398 }
399
Walter Jangb6ca2722015-02-20 11:10:25 -0800400 @Override
401 public void onClick(View view) {
Walter Jangf5dfea42015-09-16 12:30:36 -0700402 if (view.getId() == R.id.more_fields) {
Walter Jangd6753152015-10-02 09:23:13 -0700403 showAllFields();
Walter Jangb6ca2722015-02-20 11:10:25 -0800404 }
Walter Jangcab3dce2015-02-09 17:48:03 -0800405 }
406
407 @Override
408 public void setEnabled(boolean enabled) {
409 super.setEnabled(enabled);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700410 final int childCount = mKindSectionViews.getChildCount();
411 for (int i = 0; i < childCount; i++) {
412 mKindSectionViews.getChildAt(i).setEnabled(enabled);
Walter Jangcab3dce2015-02-09 17:48:03 -0800413 }
414 }
415
Walter Jang79658e12015-09-24 10:36:26 -0700416 @Override
417 public Parcelable onSaveInstanceState() {
418 final Parcelable superState = super.onSaveInstanceState();
419 final SavedState savedState = new SavedState(superState);
420 savedState.mIsExpanded = mIsExpanded;
421 return savedState;
422 }
423
424 @Override
425 public void onRestoreInstanceState(Parcelable state) {
426 if(!(state instanceof SavedState)) {
427 super.onRestoreInstanceState(state);
428 return;
429 }
430 final SavedState savedState = (SavedState) state;
431 super.onRestoreInstanceState(savedState.getSuperState());
432 mIsExpanded = savedState.mIsExpanded;
Walter Jangd6753152015-10-02 09:23:13 -0700433 if (mIsExpanded) {
434 showAllFields();
Walter Jang79658e12015-09-24 10:36:26 -0700435 }
436 }
437
Walter Jang3efae4a2015-02-18 11:12:00 -0800438 /**
Walter Jang31a74ad2015-10-02 19:17:39 -0700439 * Pass through to {@link CompactPhotoEditorView#setListener}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800440 */
Walter Jang31a74ad2015-10-02 19:17:39 -0700441 public void setPhotoListener(CompactPhotoEditorView.Listener listener) {
442 mPhotoView.setListener(listener);
Walter Jang3efae4a2015-02-18 11:12:00 -0800443 }
444
Walter Jang31a74ad2015-10-02 19:17:39 -0700445 public void removePhoto() {
Walter Jang45b86d52015-10-15 15:23:16 -0700446 mPhotoValuesDelta.setFromTemplate(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700447 mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null);
448
449 mPhotoView.removePhoto();
Walter Jang3efae4a2015-02-18 11:12:00 -0800450 }
451
452 /**
Walter Jang41b3ea12015-03-09 17:30:06 -0700453 * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}.
454 */
455 public void setFullSizePhoto(Uri photoUri) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700456 mPhotoView.setFullSizedPhoto(photoUri);
Walter Jang41b3ea12015-03-09 17:30:06 -0700457 }
458
Walter Jang31a74ad2015-10-02 19:17:39 -0700459 public void updatePhoto(Uri photoUri) {
Walter Jang45b86d52015-10-15 15:23:16 -0700460 mPhotoValuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700461 // Unset primary for all photos
Wenyi Wang9086fb92015-11-16 09:58:20 -0800462 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700463 // Mark the currently displayed photo as primary
464 mPhotoValuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700465
Wenyi Wang4c9cf3a2015-10-15 17:58:23 -0700466 // Even though high-res photos cannot be saved by passing them via
467 // an EntityDeltaList (since they cause the Bundle size limit to be
468 // exceeded), we still pass a low-res thumbnail. This simplifies
469 // code all over the place, because we don't have to test whether
470 // there is a change in EITHER the delta-list OR a changed photo...
471 // this way, there is always a change in the delta-list.
472 try {
473 final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
474 getContext(), photoUri);
475 if (bytes != null) {
476 mPhotoValuesDelta.setPhoto(bytes);
477 }
478 } catch (FileNotFoundException e) {
479 elog("Failed to get bitmap from photo Uri");
480 }
481
Walter Jang31a74ad2015-10-02 19:17:39 -0700482 mPhotoView.setFullSizedPhoto(photoUri);
483 }
484
Wenyi Wang9086fb92015-11-16 09:58:20 -0800485 private void unsetSuperPrimaryFromAllPhotos() {
Walter Jang3f18d612015-10-07 16:01:05 -0700486 final List<KindSectionData> kindSectionDataList =
487 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
488 for (KindSectionData kindSectionData : kindSectionDataList) {
Wenyi Wang9086fb92015-11-16 09:58:20 -0800489 for (ValuesDelta valuesDelta : kindSectionData.getNonEmptyValuesDeltas()) {
490 valuesDelta.setSuperPrimary(false);
Walter Jang3f18d612015-10-07 16:01:05 -0700491 }
492 }
493 }
494
Walter Jang41b3ea12015-03-09 17:30:06 -0700495 /**
Walter Janga5e4bb22015-02-24 13:08:16 -0800496 * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800497 */
498 public boolean isWritablePhotoSet() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700499 return mPhotoView.isWritablePhotoSet();
Walter Jang3efae4a2015-02-18 11:12:00 -0800500 }
501
502 /**
Walter Jang3efae4a2015-02-18 11:12:00 -0800503 * Get the raw contact ID for the CompactHeaderView photo.
504 */
Walter Jang3efae4a2015-02-18 11:12:00 -0800505 public long getPhotoRawContactId() {
506 return mPhotoRawContactId;
507 }
508
Walter Jang4f5594a2015-10-06 18:40:31 -0700509 public StructuredNameEditorView getPrimaryNameEditorView() {
Walter Jange3945952015-10-27 12:44:54 -0700510 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
511 return primaryNameKindSectionView == null
512 ? null : primaryNameKindSectionView.getPrimaryNameEditorView();
Walter Jang4f5594a2015-10-06 18:40:31 -0700513 }
514
Walter Jang31a74ad2015-10-02 19:17:39 -0700515 /**
516 * Returns a data holder for every non-default/non-empty photo from each raw contact, whether
517 * the raw contact is writable or not.
518 */
519 public ArrayList<CompactPhotoSelectionFragment.Photo> getPhotos() {
520 final ArrayList<CompactPhotoSelectionFragment.Photo> photos = new ArrayList<>();
521
Walter Jang3f18d612015-10-07 16:01:05 -0700522 final Bundle updatedPhotos = mListener == null ? null : mListener.getUpdatedPhotos();
523
Walter Jang31a74ad2015-10-02 19:17:39 -0700524 final List<KindSectionData> kindSectionDataList =
525 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
526 for (int i = 0; i < kindSectionDataList.size(); i++) {
527 final KindSectionData kindSectionData = kindSectionDataList.get(i);
528 final AccountType accountType = kindSectionData.getAccountType();
Walter Jang228e02f2015-11-13 09:11:01 -0800529 final List<ValuesDelta> valuesDeltas = kindSectionData.getNonEmptyValuesDeltas();
530 if (valuesDeltas.isEmpty()) continue;
531 for (int j = 0; j < valuesDeltas.size(); j++) {
532 final ValuesDelta valuesDelta = valuesDeltas.get(j);
Walter Jang31a74ad2015-10-02 19:17:39 -0700533 final Bitmap bitmap = EditorUiUtils.getPhotoBitmap(valuesDelta);
534 if (bitmap == null) continue;
535
536 final CompactPhotoSelectionFragment.Photo photo =
537 new CompactPhotoSelectionFragment.Photo();
538 photo.titleRes = accountType.titleRes;
539 photo.iconRes = accountType.iconRes;
540 photo.syncAdapterPackageName = accountType.syncAdapterPackageName;
541 photo.valuesDelta = valuesDelta;
542 photo.primary = valuesDelta.isSuperPrimary();
543 photo.kindSectionDataListIndex = i;
544 photo.valuesDeltaListIndex = j;
Walter Jangda258ff2015-11-17 11:06:02 -0800545 photo.photoId = valuesDelta.getId();
Walter Jang3f18d612015-10-07 16:01:05 -0700546
547 if (updatedPhotos != null) {
548 photo.updatedPhotoUri = (Uri) updatedPhotos.get(String.valueOf(
549 kindSectionData.getRawContactDelta().getRawContactId()));
550 }
551
Wenyi Wangd375c352016-03-28 15:12:45 -0700552 final CharSequence accountTypeLabel = accountType.getDisplayLabel(getContext());
553 photo.accountType = accountTypeLabel == null ? "" : accountTypeLabel.toString();
554 final String accountName = kindSectionData.getRawContactDelta().getAccountName();
555 photo.accountName = accountName == null ? "" : accountName;
Wenyi Wangab360172015-12-15 10:35:58 -0800556
Walter Jang31a74ad2015-10-02 19:17:39 -0700557 photos.add(photo);
558 }
559 }
560
561 return photos;
562 }
563
564 /**
565 * Marks the raw contact photo given as primary for the aggregate contact and updates the
566 * UI.
567 */
568 public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700569 // Find the values delta to mark as primary
Walter Jang3f18d612015-10-07 16:01:05 -0700570 final KindSectionDataList kindSectionDataList =
571 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
Walter Jang31a74ad2015-10-02 19:17:39 -0700572 if (photo.kindSectionDataListIndex < 0
573 || photo.kindSectionDataListIndex >= kindSectionDataList.size()) {
574 wlog("Invalid kind section data list index");
575 return;
576 }
577 final KindSectionData kindSectionData =
578 kindSectionDataList.get(photo.kindSectionDataListIndex);
Walter Jang228e02f2015-11-13 09:11:01 -0800579 final List<ValuesDelta> valuesDeltaList = kindSectionData.getNonEmptyValuesDeltas();
Walter Jang31a74ad2015-10-02 19:17:39 -0700580 if (photo.valuesDeltaListIndex >= valuesDeltaList.size()) {
581 wlog("Invalid values delta list index");
582 return;
583 }
Walter Jangda258ff2015-11-17 11:06:02 -0800584
585 // Update values delta
Walter Jang31a74ad2015-10-02 19:17:39 -0700586 final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex);
587 valuesDelta.setFromTemplate(false);
Wenyi Wang9086fb92015-11-16 09:58:20 -0800588 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700589 valuesDelta.setSuperPrimary(true);
Walter Jangda258ff2015-11-17 11:06:02 -0800590
Walter Jang31a74ad2015-10-02 19:17:39 -0700591 // Update the UI
592 mPhotoView.setPhoto(valuesDelta, mMaterialPalette);
593 }
594
Walter Jangd35e5ef2015-02-24 09:18:16 -0800595 public View getAggregationAnchorView() {
Walter Jangf10ca152015-09-22 15:23:55 -0700596 final List<CompactKindSectionView> kindSectionViews = getKindSectionViews(
Walter Jang192a01c2015-09-22 15:23:55 -0700597 StructuredName.CONTENT_ITEM_TYPE);
Walter Jangf10ca152015-09-22 15:23:55 -0700598 if (!kindSectionViews.isEmpty()) {
Walter Jang192a01c2015-09-22 15:23:55 -0700599 return mKindSectionViews.getChildAt(0).findViewById(R.id.anchor_view);
Walter Jangd35e5ef2015-02-24 09:18:16 -0800600 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700601 return null;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800602 }
603
Walter Jangf10ca152015-09-22 15:23:55 -0700604 public void setGroupMetaData(Cursor groupMetaData) {
605 final List<CompactKindSectionView> kindSectionViews = getKindSectionViews(
606 GroupMembership.CONTENT_ITEM_TYPE);
607 for (CompactKindSectionView kindSectionView : kindSectionViews) {
608 kindSectionView.setGroupMetaData(groupMetaData);
Walter Jangd6753152015-10-02 09:23:13 -0700609 if (mIsExpanded) {
610 kindSectionView.setHideWhenEmpty(false);
611 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
612 }
Walter Jangf10ca152015-09-22 15:23:55 -0700613 }
614 }
615
Walter Jangf46abd82015-02-20 16:52:04 -0800616 public void setState(RawContactDeltaList rawContactDeltas,
Walter Jang06f73a12015-06-17 11:15:48 -0700617 MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
Walter Jange3945952015-10-27 12:44:54 -0700618 long photoId, boolean hasNewContact, boolean isUserProfile,
619 AccountWithDataSet primaryAccount) {
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700620 // Enable layout animations for new contacts. This looks nicer when switching to and from
621 // an account that doesn't support profile photos (e.g. SIM accounts).
622 if (hasNewContact && getLayoutTransition() == null) {
623 setLayoutTransition(new LayoutTransition());
624 }
Walter Jang363d3fd2015-09-16 10:29:07 -0700625 mKindSectionDataMap.clear();
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700626 mKindSectionViewsMap.clear();
Walter Jangf5dfea42015-09-16 12:30:36 -0700627 mKindSectionViews.removeAllViews();
Walter Jang363d3fd2015-09-16 10:29:07 -0700628 mMoreFields.setVisibility(View.VISIBLE);
Walter Jangcab3dce2015-02-09 17:48:03 -0800629
Walter Jangf46abd82015-02-20 16:52:04 -0800630 mMaterialPalette = materialPalette;
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700631 mViewIdGenerator = viewIdGenerator;
Walter Jang708ea9e2015-09-10 15:42:05 -0700632 mPhotoId = photoId;
Tingting Wangf695eb32015-10-15 18:45:15 -0700633
Walter Jang708ea9e2015-09-10 15:42:05 -0700634 mHasNewContact = hasNewContact;
635 mIsUserProfile = isUserProfile;
636 mPrimaryAccount = primaryAccount;
637 if (mPrimaryAccount == null) {
638 mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount();
639 }
640 vlog("state: primary " + mPrimaryAccount);
Walter Jangcab3dce2015-02-09 17:48:03 -0800641
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700642 // Parse the given raw contact deltas
643 if (rawContactDeltas == null || rawContactDeltas.isEmpty()) {
644 elog("No raw contact deltas");
Walter Jang79658e12015-09-24 10:36:26 -0700645 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jangf5dfea42015-09-16 12:30:36 -0700646 return;
647 }
Walter Jangcbd431d2015-10-06 13:07:30 -0700648 parseRawContactDeltas(rawContactDeltas);
Walter Jang79658e12015-09-24 10:36:26 -0700649 if (mKindSectionDataMap.isEmpty()) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700650 elog("No kind section data parsed from RawContactDelta(s)");
Walter Jang79658e12015-09-24 10:36:26 -0700651 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700652 return;
653 }
Walter Jang82acd422015-10-17 14:01:27 -0700654
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700655
Walter Jang82acd422015-10-17 14:01:27 -0700656 // Get the primary name kind section data
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700657 mPrimaryNameKindSectionData =
658 getOrCreateKindSectionDataList(StructuredName.CONTENT_ITEM_TYPE)
659 .getEntryToWrite(/* id =*/ -1, mPrimaryAccount, mIsUserProfile);
Walter Jang82acd422015-10-17 14:01:27 -0700660 if (mPrimaryNameKindSectionData != null) {
661 // Ensure that a structured name and photo exists
662 final RawContactDelta rawContactDelta =
663 mPrimaryNameKindSectionData.first.getRawContactDelta();
664 RawContactModifier.ensureKindExists(
665 rawContactDelta,
666 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700667 StructuredName.CONTENT_ITEM_TYPE);
Walter Jang82acd422015-10-17 14:01:27 -0700668 RawContactModifier.ensureKindExists(
669 rawContactDelta,
670 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700671 Photo.CONTENT_ITEM_TYPE);
672 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700673
674 // Setup the view
Tingting Wang655ad1a2015-10-05 17:51:14 -0700675 addAccountInfo(rawContactDeltas);
Walter Jang363d3fd2015-09-16 10:29:07 -0700676 addPhotoView();
Walter Jangf5dfea42015-09-16 12:30:36 -0700677 addKindSectionViews();
Walter Jange3945952015-10-27 12:44:54 -0700678
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700679 mMoreFields.setVisibility(hasMoreFields() ? View.VISIBLE : View.GONE);
680
Walter Jang4f5594a2015-10-06 18:40:31 -0700681 if (mIsExpanded) showAllFields();
Walter Jangd6753152015-10-02 09:23:13 -0700682
683 if (mListener != null) mListener.onEditorsBound();
Walter Jangcab3dce2015-02-09 17:48:03 -0800684 }
685
Walter Jangcbd431d2015-10-06 13:07:30 -0700686 private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700687 // Build the kind section data list map
Walter Jang192a01c2015-09-22 15:23:55 -0700688 vlog("parse: " + rawContactDeltas.size() + " rawContactDelta(s)");
689 for (int j = 0; j < rawContactDeltas.size(); j++) {
690 final RawContactDelta rawContactDelta = rawContactDeltas.get(j);
691 vlog("parse: " + j + " rawContactDelta" + rawContactDelta);
Walter Jang363d3fd2015-09-16 10:29:07 -0700692 if (rawContactDelta == null || !rawContactDelta.isVisible()) continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800693 final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
Walter Jang363d3fd2015-09-16 10:29:07 -0700694 if (accountType == null) continue;
695 final List<DataKind> dataKinds = accountType.getSortedDataKinds();
696 final int dataKindSize = dataKinds == null ? 0 : dataKinds.size();
697 vlog("parse: " + dataKindSize + " dataKinds(s)");
698 for (int i = 0; i < dataKindSize; i++) {
699 final DataKind dataKind = dataKinds.get(i);
Walter Jang192a01c2015-09-22 15:23:55 -0700700 if (dataKind == null || !dataKind.editable) {
701 vlog("parse: " + i + " " + dataKind.mimeType + " dropped read-only");
702 continue;
703 }
704 final String mimeType = dataKind.mimeType;
705
706 // Skip psuedo mime types
707 if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
708 || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
709 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
710 continue;
711 }
712
Walter Jang3f18d612015-10-07 16:01:05 -0700713 final KindSectionDataList kindSectionDataList =
Walter Jangcbd431d2015-10-06 13:07:30 -0700714 getOrCreateKindSectionDataList(mimeType);
Walter Jang363d3fd2015-09-16 10:29:07 -0700715 final KindSectionData kindSectionData =
716 new KindSectionData(accountType, dataKind, rawContactDelta);
717 kindSectionDataList.add(kindSectionData);
Walter Jang192a01c2015-09-22 15:23:55 -0700718
Walter Jang363d3fd2015-09-16 10:29:07 -0700719 vlog("parse: " + i + " " + dataKind.mimeType + " " +
Walter Jang228e02f2015-11-13 09:11:01 -0800720 kindSectionData.getValuesDeltas().size() + " value(s) " +
Tingting Wangd482e0c2015-11-16 10:13:29 -0800721 kindSectionData.getNonEmptyValuesDeltas().size() + " non-empty value(s) " +
Wenyi Wang42d740c2015-11-13 14:23:54 -0800722 kindSectionData.getVisibleValuesDeltas().size() +
Walter Jang228e02f2015-11-13 09:11:01 -0800723 " visible value(s)");
Walter Jangac679af2015-06-01 12:17:06 -0700724 }
725 }
Walter Jang3efae4a2015-02-18 11:12:00 -0800726 }
727
Walter Jang3f18d612015-10-07 16:01:05 -0700728 private KindSectionDataList getOrCreateKindSectionDataList(String mimeType) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700729 KindSectionDataList kindSectionDataList = mKindSectionDataMap.get(mimeType);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700730 if (kindSectionDataList == null) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700731 kindSectionDataList = new KindSectionDataList();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700732 mKindSectionDataMap.put(mimeType, kindSectionDataList);
733 }
734 return kindSectionDataList;
735 }
736
Tingting Wang655ad1a2015-10-05 17:51:14 -0700737 private void addAccountInfo(RawContactDeltaList rawContactDeltas) {
Walter Jang82acd422015-10-17 14:01:27 -0700738 mAccountHeaderContainer.setVisibility(View.GONE);
Walter Jang82acd422015-10-17 14:01:27 -0700739 mRawContactContainer.setVisibility(View.GONE);
740
741 if (mPrimaryNameKindSectionData == null) return;
742 final RawContactDelta rawContactDelta =
743 mPrimaryNameKindSectionData.first.getRawContactDelta();
Walter Jang708ea9e2015-09-10 15:42:05 -0700744
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700745 final AccountDisplayInfo account =
746 mAccountDisplayInfoFactory.getAccountDisplayInfoFor(rawContactDelta);
747
Walter Jang708ea9e2015-09-10 15:42:05 -0700748 // Get the account information for the primary raw contact delta
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700749 final String accountLabel = mIsUserProfile
750 ? EditorUiUtils.getAccountHeaderLabelForMyProfile(getContext(), account)
751 : account.getNameLabel().toString();
Walter Jang708ea9e2015-09-10 15:42:05 -0700752
Walter Jang23709542015-10-22 12:50:58 -0700753 // Either the account header or selector should be shown, not both.
Walter Jang708ea9e2015-09-10 15:42:05 -0700754 final List<AccountWithDataSet> accounts =
755 AccountTypeManager.getInstance(getContext()).getAccounts(true);
Walter Jang82acd422015-10-17 14:01:27 -0700756 if (mHasNewContact && !mIsUserProfile) {
757 if (accounts.size() > 1) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700758 addAccountSelector(rawContactDelta, accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700759 } else {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700760 addAccountHeader(accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700761 }
Tingting Wang1594c6f2015-10-26 17:39:16 -0700762 } else if (mIsUserProfile || !shouldHideAccountContainer(rawContactDeltas)) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700763 addAccountHeader(accountLabel);
Walter Jang708ea9e2015-09-10 15:42:05 -0700764 }
Walter Jang23709542015-10-22 12:50:58 -0700765
766 // The raw contact selector should only display linked raw contacts that can be edited in
767 // the full editor (i.e. they are not newly created raw contacts)
Tingting Wang2bb85d22015-10-23 13:01:09 -0700768 final RawContactAccountListAdapter adapter = new RawContactAccountListAdapter(getContext(),
769 getRawContactDeltaListForSelector(rawContactDeltas));
770 if (adapter.getCount() > 0) {
771 final String accountsSummary = getResources().getQuantityString(
772 R.plurals.compact_editor_linked_contacts_selector_title,
773 adapter.getCount(), adapter.getCount());
Walter Jang23709542015-10-22 12:50:58 -0700774 addRawContactAccountSelector(accountsSummary, adapter);
775 }
Walter Jang708ea9e2015-09-10 15:42:05 -0700776 }
777
Tingting Wang2bb85d22015-10-23 13:01:09 -0700778 private RawContactDeltaList getRawContactDeltaListForSelector(
779 RawContactDeltaList rawContactDeltas) {
780 // Sort raw contacts so google accounts come first
781 Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
782
783 final RawContactDeltaList result = new RawContactDeltaList();
784 for (RawContactDelta rawContactDelta : rawContactDeltas) {
785 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
786 // Only add raw contacts that can be opened in the editor
787 result.add(rawContactDelta);
788 }
789 }
790 // Don't return a list of size 1 that would just open the raw contact being edited
791 // in the compact editor in the full editor
792 if (result.size() == 1 && result.get(0).getRawContactAccountType(
793 getContext()).areContactsWritable()) {
794 result.clear();
795 return result;
796 }
797 return result;
798 }
799
Tingting Wang1594c6f2015-10-26 17:39:16 -0700800 // Returns true if there are multiple writable rawcontacts and no read-only ones,
801 // or there are both writable and read-only rawcontacts.
802 private boolean shouldHideAccountContainer(RawContactDeltaList rawContactDeltas) {
Tingting Wang2bb85d22015-10-23 13:01:09 -0700803 int writable = 0;
804 int readonly = 0;
805 for (RawContactDelta rawContactDelta : rawContactDeltas) {
806 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
807 if (rawContactDelta.getRawContactAccountType(getContext()).areContactsWritable()) {
808 writable++;
809 } else {
810 readonly++;
811 }
812 }
813 }
814 return (writable > 1 || (writable > 0 && readonly > 0));
815 }
816
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700817 private void addAccountHeader(String accountLabel) {
Walter Jang82acd422015-10-17 14:01:27 -0700818 mAccountHeaderContainer.setVisibility(View.VISIBLE);
819
Walter Jangbe5e1b12015-10-17 11:38:29 -0700820 // Set the account name
Walter Jangbe5e1b12015-10-17 11:38:29 -0700821 mAccountHeaderName.setVisibility(View.VISIBLE);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700822 mAccountHeaderName.setText(accountLabel);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700823
824 // Set the account type
825 final String selectorTitle = getResources().getString(
826 R.string.compact_editor_account_selector_title);
827 mAccountHeaderType.setText(selectorTitle);
828
829 // Set the icon
Walter Jang82acd422015-10-17 14:01:27 -0700830 if (mPrimaryNameKindSectionData != null) {
831 final RawContactDelta rawContactDelta =
832 mPrimaryNameKindSectionData.first.getRawContactDelta();
833 if (rawContactDelta != null) {
834 final AccountType accountType =
835 rawContactDelta.getRawContactAccountType(getContext());
836 mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext()));
837 }
838 }
Walter Jangbe5e1b12015-10-17 11:38:29 -0700839
840 // Set the content description
841 mAccountHeaderContainer.setContentDescription(
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700842 EditorUiUtils.getAccountInfoContentDescription(accountLabel,
843 selectorTitle));
Walter Jangbe5e1b12015-10-17 11:38:29 -0700844 }
845
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700846 private void addAccountSelector(final RawContactDelta rawContactDelta, CharSequence nameLabel) {
Gary Mai46cb3102016-08-10 18:14:09 -0700847 // Show save to default account.
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700848 addAccountHeader(nameLabel.toString());
Gary Mai46cb3102016-08-10 18:14:09 -0700849 // Add handlers for choosing another account to save to.
850 mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE);
851 mAccountHeaderContainer.setOnClickListener(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)) {
871 mListener.onRebindEditorsForNewContact(
Walter Jang82acd422015-10-17 14:01:27 -0700872 rawContactDelta,
Walter Jang708ea9e2015-09-10 15:42:05 -0700873 mPrimaryAccount,
874 newAccount);
875 }
876 }
877 });
878 popup.show();
879 }
880 });
881 }
882
Walter Jang23709542015-10-22 12:50:58 -0700883 private void addRawContactAccountSelector(String accountsSummary,
884 final RawContactAccountListAdapter adapter) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700885 mRawContactContainer.setVisibility(View.VISIBLE);
886
Tingting Wang655ad1a2015-10-05 17:51:14 -0700887 mRawContactSummary.setText(accountsSummary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700888
889 mRawContactContainer.setOnClickListener(new View.OnClickListener() {
890 @Override
891 public void onClick(View v) {
892 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700893 popup.setWidth(mRawContactContainer.getWidth());
894 popup.setAnchorView(mRawContactContainer);
895 popup.setAdapter(adapter);
896 popup.setModal(true);
897 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
898 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
899 @Override
900 public void onItemClick(AdapterView<?> parent, View view, int position,
901 long id) {
902 UiClosables.closeQuietly(popup);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700903
904 if (mListener != null) {
Tingting Wang91cee282015-10-07 13:48:17 -0700905 final long rawContactId = adapter.getItemId(position);
906 final Uri rawContactUri = ContentUris.withAppendedId(
907 ContactsContract.RawContacts.CONTENT_URI, rawContactId);
908 final RawContactDelta rawContactDelta = adapter.getItem(position);
909 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
910 getContext());
911 final AccountType accountType = rawContactDelta.getAccountType(
912 accountTypes);
913 final boolean isReadOnly = !accountType.areContactsWritable();
914
915 mListener.onRawContactSelected(rawContactUri, rawContactId, isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700916 }
917 }
918 });
919 popup.show();
920 }
921 });
922 }
923
Walter Jang363d3fd2015-09-16 10:29:07 -0700924 private void addPhotoView() {
Walter Jang31a74ad2015-10-02 19:17:39 -0700925 // Get the kind section data and values delta that we will display in the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700926 final KindSectionDataList kindSectionDataList =
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700927 getOrCreateKindSectionDataList(Photo.CONTENT_ITEM_TYPE);
Walter Jangcbd431d2015-10-06 13:07:30 -0700928 final Pair<KindSectionData,ValuesDelta> photoToDisplay =
929 kindSectionDataList.getEntryToDisplay(mPhotoId);
930 if (photoToDisplay == null) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700931 wlog("photo: no kind section data parsed");
Walter Jangcbd431d2015-10-06 13:07:30 -0700932 mPhotoView.setVisibility(View.GONE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700933 return;
Marcus Hagerottcf7f2952016-09-06 13:49:17 -0700934 } else {
935 mPhotoView.setVisibility(View.VISIBLE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700936 }
Walter Jang06f73a12015-06-17 11:15:48 -0700937
Walter Jang31a74ad2015-10-02 19:17:39 -0700938 // Set the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700939 mPhotoView.setPhoto(photoToDisplay.second, mMaterialPalette);
Walter Jang31a74ad2015-10-02 19:17:39 -0700940
941 // Find the raw contact ID and values delta that will be written when the photo is edited
Walter Jangda258ff2015-11-17 11:06:02 -0800942 final Pair<KindSectionData, ValuesDelta> photoToWrite = kindSectionDataList.getEntryToWrite(
Walter Jang82acd422015-10-17 14:01:27 -0700943 mPhotoId, mPrimaryAccount, mIsUserProfile);
Walter Jangda258ff2015-11-17 11:06:02 -0800944 if (photoToWrite == null) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700945 mPhotoView.setReadOnly(true);
946 return;
947 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700948 mPhotoView.setReadOnly(false);
Walter Jangda258ff2015-11-17 11:06:02 -0800949 mPhotoRawContactId = photoToWrite.first.getRawContactDelta().getRawContactId();
950 mPhotoValuesDelta = photoToWrite.second;
Walter Jang31a74ad2015-10-02 19:17:39 -0700951 }
952
Walter Jangf5dfea42015-09-16 12:30:36 -0700953 private void addKindSectionViews() {
954 // Sort the kinds
Walter Jangcbd431d2015-10-06 13:07:30 -0700955 final TreeSet<Map.Entry<String,KindSectionDataList>> entries =
Walter Jang192a01c2015-09-22 15:23:55 -0700956 new TreeSet<>(KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR);
Walter Jangf5dfea42015-09-16 12:30:36 -0700957 entries.addAll(mKindSectionDataMap.entrySet());
Walter Jang3efae4a2015-02-18 11:12:00 -0800958
Walter Jangf5dfea42015-09-16 12:30:36 -0700959 vlog("kind: " + entries.size() + " kindSection(s)");
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700960 int i = -1;
Walter Jangcbd431d2015-10-06 13:07:30 -0700961 for (Map.Entry<String, KindSectionDataList> entry : entries) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700962 i++;
963
Walter Jangf5dfea42015-09-16 12:30:36 -0700964 final String mimeType = entry.getKey();
Walter Jang82acd422015-10-17 14:01:27 -0700965
Tingting Wangf695eb32015-10-15 18:45:15 -0700966 if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang82acd422015-10-17 14:01:27 -0700967 if (mPrimaryNameKindSectionData == null) {
968 vlog("kind: " + i + " " + mimeType + " dropped");
969 continue;
Tingting Wangf695eb32015-10-15 18:45:15 -0700970 }
Walter Jang82acd422015-10-17 14:01:27 -0700971 vlog("kind: " + i + " " + mimeType + " using first entry only");
972 final KindSectionDataList kindSectionDataList = new KindSectionDataList();
973 kindSectionDataList.add(mPrimaryNameKindSectionData.first);
Walter Jangf5dfea42015-09-16 12:30:36 -0700974 final CompactKindSectionView kindSectionView = inflateKindSectionView(
Walter Jang82acd422015-10-17 14:01:27 -0700975 mKindSectionViews, kindSectionDataList, mimeType,
976 mPrimaryNameKindSectionData.second);
Walter Jangf5dfea42015-09-16 12:30:36 -0700977 mKindSectionViews.addView(kindSectionView);
Walter Jangf10ca152015-09-22 15:23:55 -0700978
979 // Keep a pointer to all the KindSectionsViews for each mimeType
980 getKindSectionViews(mimeType).add(kindSectionView);
Walter Jang82acd422015-10-17 14:01:27 -0700981 } else {
982 final KindSectionDataList kindSectionDataList = entry.getValue();
983
984 // Ignore mime types that we've already handled
985 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
986 vlog("kind: " + i + " " + mimeType + " dropped");
987 continue;
988 }
989
990 // Don't show more than one group editor on the compact editor.
991 // Groups will still be editable for each raw contact individually on the full editor.
992 if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)
993 && kindSectionDataList.size() > 1) {
994 vlog("kind: " + i + " " + mimeType + " dropped");
995 continue;
996 }
997
998 if (kindSectionDataList != null && !kindSectionDataList.isEmpty()) {
999 vlog("kind: " + i + " " + mimeType + " " + kindSectionDataList.size() +
1000 " kindSectionData(s)");
1001
1002 final CompactKindSectionView kindSectionView = inflateKindSectionView(
1003 mKindSectionViews, kindSectionDataList, mimeType,
1004 /* primaryValueDelta =*/ null);
1005 mKindSectionViews.addView(kindSectionView);
1006
1007 // Keep a pointer to all the KindSectionsViews for each mimeType
1008 getKindSectionViews(mimeType).add(kindSectionView);
1009 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001010 }
1011 }
1012 }
1013
Walter Jangf10ca152015-09-22 15:23:55 -07001014 private List<CompactKindSectionView> getKindSectionViews(String mimeType) {
1015 List<CompactKindSectionView> kindSectionViews = mKindSectionViewsMap.get(mimeType);
1016 if (kindSectionViews == null) {
1017 kindSectionViews = new ArrayList<>();
1018 mKindSectionViewsMap.put(mimeType, kindSectionViews);
1019 }
1020 return kindSectionViews;
1021 }
1022
Walter Jangf5dfea42015-09-16 12:30:36 -07001023 private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup,
Walter Jang82acd422015-10-17 14:01:27 -07001024 KindSectionDataList kindSectionDataList, String mimeType,
1025 ValuesDelta primaryValuesDelta) {
Walter Jangf5dfea42015-09-16 12:30:36 -07001026 final CompactKindSectionView kindSectionView = (CompactKindSectionView)
1027 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
1028 /* attachToRoot =*/ false);
Walter Jang45b86d52015-10-15 15:23:16 -07001029 kindSectionView.setIsUserProfile(mIsUserProfile);
Walter Jangf5dfea42015-09-16 12:30:36 -07001030
Walter Jang192a01c2015-09-22 15:23:55 -07001031 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Walter Jangf5dfea42015-09-16 12:30:36 -07001032 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001033 // Phone numbers and email addresses are always displayed,
1034 // even if they are empty
Walter Jangf5dfea42015-09-16 12:30:36 -07001035 kindSectionView.setHideWhenEmpty(false);
1036 }
Walter Jangf5dfea42015-09-16 12:30:36 -07001037
Walter Jang192a01c2015-09-22 15:23:55 -07001038 // Since phone numbers and email addresses displayed even if they are empty,
1039 // they will be the only types you add new values to initially for new contacts
1040 kindSectionView.setShowOneEmptyEditor(true);
1041
Walter Jang82acd422015-10-17 14:01:27 -07001042 // Sort non-name editors so they wind up in the order we want
1043 if (!StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001044 Collections.sort(kindSectionDataList, new EditorComparator(getContext()));
Walter Jang3e5ae0d2015-09-20 12:43:37 -07001045 }
1046
Walter Jang82acd422015-10-17 14:01:27 -07001047 kindSectionView.setState(kindSectionDataList, mViewIdGenerator, mListener,
1048 primaryValuesDelta);
Walter Jangf5dfea42015-09-16 12:30:36 -07001049
1050 return kindSectionView;
Walter Jangcab3dce2015-02-09 17:48:03 -08001051 }
1052
Walter Jange3945952015-10-27 12:44:54 -07001053 void maybeSetReadOnlyDisplayNameAsPrimary(String readOnlyDisplayName) {
1054 if (TextUtils.isEmpty(readOnlyDisplayName)) return;
1055 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
1056 if (primaryNameKindSectionView != null && primaryNameKindSectionView.isEmptyName()) {
1057 vlog("name: using read only display name as primary name");
1058 primaryNameKindSectionView.setName(readOnlyDisplayName);
1059 }
1060 }
1061
1062 private CompactKindSectionView getPrimaryNameKindSectionView() {
Walter Jang4f5594a2015-10-06 18:40:31 -07001063 final List<CompactKindSectionView> kindSectionViews
1064 = mKindSectionViewsMap.get(StructuredName.CONTENT_ITEM_TYPE);
Walter Jange3945952015-10-27 12:44:54 -07001065 return kindSectionViews == null || kindSectionViews.isEmpty()
1066 ? null : kindSectionViews.get(0);
Walter Jang4f5594a2015-10-06 18:40:31 -07001067 }
1068
Walter Jangd6753152015-10-02 09:23:13 -07001069 private void showAllFields() {
Walter Jang79658e12015-09-24 10:36:26 -07001070 // Stop hiding empty editors and allow the user to enter values for all kinds now
1071 for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
1072 final CompactKindSectionView kindSectionView =
1073 (CompactKindSectionView) mKindSectionViews.getChildAt(i);
1074 kindSectionView.setHideWhenEmpty(false);
1075 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
1076 }
1077 mIsExpanded = true;
Walter Jang79658e12015-09-24 10:36:26 -07001078
Walter Jangf5dfea42015-09-16 12:30:36 -07001079 // Hide the more fields button
1080 mMoreFields.setVisibility(View.GONE);
Walter Jangcab3dce2015-02-09 17:48:03 -08001081 }
1082
Marcus Hagerottcf7f2952016-09-06 13:49:17 -07001083 private boolean hasMoreFields() {
1084 for (List<CompactKindSectionView> sections : mKindSectionViewsMap.values()) {
1085 for (CompactKindSectionView section : sections) {
1086 if (section.getVisibility() != View.VISIBLE) {
1087 return true;
1088 }
1089 }
1090 }
1091 return false;
1092 }
1093
Walter Jangbf63a6d2015-05-05 09:14:35 -07001094 private static void vlog(String message) {
1095 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1096 Log.v(TAG, message);
Walter Jangcab3dce2015-02-09 17:48:03 -08001097 }
1098 }
Walter Jang363d3fd2015-09-16 10:29:07 -07001099
1100 private static void wlog(String message) {
1101 if (Log.isLoggable(TAG, Log.WARN)) {
1102 Log.w(TAG, message);
1103 }
1104 }
1105
1106 private static void elog(String message) {
1107 Log.e(TAG, message);
1108 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001109}