blob: ab817801ffdc593a3752eddfb7d9867d7c534472 [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
19import com.android.contacts.R;
20import com.android.contacts.common.model.AccountTypeManager;
21import com.android.contacts.common.model.RawContactDelta;
22import com.android.contacts.common.model.RawContactDeltaList;
Walter Jange720fde2015-02-17 10:54:14 -080023import com.android.contacts.common.model.RawContactModifier;
Walter Jangcab3dce2015-02-09 17:48:03 -080024import com.android.contacts.common.model.ValuesDelta;
25import com.android.contacts.common.model.account.AccountType;
Walter Jang2d3f31c2015-06-18 23:15:31 -070026import com.android.contacts.common.model.account.AccountWithDataSet;
Walter Jangcab3dce2015-02-09 17:48:03 -080027import com.android.contacts.common.model.dataitem.DataKind;
Walter Jang708ea9e2015-09-10 15:42:05 -070028import com.android.contacts.common.util.AccountsListAdapter;
Walter Jangf46abd82015-02-20 16:52:04 -080029import com.android.contacts.common.util.MaterialColorMapUtils;
Walter Jang708ea9e2015-09-10 15:42:05 -070030import com.android.contacts.util.UiClosables;
Walter Jangcab3dce2015-02-09 17:48:03 -080031
Tingting Wang655ad1a2015-10-05 17:51:14 -070032import android.content.ContentUris;
Walter Jangcab3dce2015-02-09 17:48:03 -080033import android.content.Context;
Walter Jangf10ca152015-09-22 15:23:55 -070034import android.database.Cursor;
Walter Jang3efae4a2015-02-18 11:12:00 -080035import android.graphics.Bitmap;
Walter Jang41b3ea12015-03-09 17:30:06 -070036import android.net.Uri;
Walter Jang3f18d612015-10-07 16:01:05 -070037import android.os.Bundle;
Walter Jang79658e12015-09-24 10:36:26 -070038import android.os.Parcel;
39import android.os.Parcelable;
Tingting Wang655ad1a2015-10-05 17:51:14 -070040import android.provider.ContactsContract;
Walter Jangcab3dce2015-02-09 17:48:03 -080041import android.provider.ContactsContract.CommonDataKinds.Email;
Walter Jangf5dfea42015-09-16 12:30:36 -070042import android.provider.ContactsContract.CommonDataKinds.Event;
Walter Jangcab3dce2015-02-09 17:48:03 -080043import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
Walter Jangf5dfea42015-09-16 12:30:36 -070044import android.provider.ContactsContract.CommonDataKinds.Im;
Walter Jang3efae4a2015-02-18 11:12:00 -080045import android.provider.ContactsContract.CommonDataKinds.Nickname;
Walter Jangf5dfea42015-09-16 12:30:36 -070046import android.provider.ContactsContract.CommonDataKinds.Note;
47import android.provider.ContactsContract.CommonDataKinds.Organization;
Walter Jangcab3dce2015-02-09 17:48:03 -080048import android.provider.ContactsContract.CommonDataKinds.Phone;
49import android.provider.ContactsContract.CommonDataKinds.Photo;
Walter Jangf5dfea42015-09-16 12:30:36 -070050import android.provider.ContactsContract.CommonDataKinds.Relation;
51import android.provider.ContactsContract.CommonDataKinds.SipAddress;
Walter Jangcab3dce2015-02-09 17:48:03 -080052import android.provider.ContactsContract.CommonDataKinds.StructuredName;
Walter Jangf5dfea42015-09-16 12:30:36 -070053import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
54import android.provider.ContactsContract.CommonDataKinds.Website;
Walter Jangcab3dce2015-02-09 17:48:03 -080055import android.text.TextUtils;
56import android.util.AttributeSet;
57import android.util.Log;
Walter Jang2d3f31c2015-06-18 23:15:31 -070058import android.util.Pair;
Walter Jangcab3dce2015-02-09 17:48:03 -080059import android.view.LayoutInflater;
Walter Jangb1c87622015-02-13 17:51:38 -080060import android.view.View;
Walter Jangcab3dce2015-02-09 17:48:03 -080061import android.view.ViewGroup;
Walter Jang708ea9e2015-09-10 15:42:05 -070062import android.widget.AdapterView;
Tingting Wang655ad1a2015-10-05 17:51:14 -070063import android.widget.BaseAdapter;
64import android.widget.ImageView;
Walter Jangcab3dce2015-02-09 17:48:03 -080065import android.widget.LinearLayout;
Walter Jang708ea9e2015-09-10 15:42:05 -070066import android.widget.ListPopupWindow;
Walter Jang2d3f31c2015-06-18 23:15:31 -070067import android.widget.TextView;
Walter Jangcab3dce2015-02-09 17:48:03 -080068
Walter Jang708774a2015-10-16 09:32:06 -070069import java.io.FileNotFoundException;
Walter Jangcab3dce2015-02-09 17:48:03 -080070import java.util.ArrayList;
Walter Jangf5dfea42015-09-16 12:30:36 -070071import java.util.Arrays;
Walter Jang192a01c2015-09-22 15:23:55 -070072import java.util.Collections;
Walter Jangf5dfea42015-09-16 12:30:36 -070073import java.util.Comparator;
Walter Jang19d985f2015-07-01 13:36:27 -070074import java.util.HashMap;
Walter Jangcab3dce2015-02-09 17:48:03 -080075import java.util.List;
Walter Jang19d985f2015-07-01 13:36:27 -070076import java.util.Map;
Walter Jangf5dfea42015-09-16 12:30:36 -070077import java.util.TreeSet;
Walter Jangcab3dce2015-02-09 17:48:03 -080078
79/**
Walter Jangf5dfea42015-09-16 12:30:36 -070080 * View to display information from multiple {@link RawContactDelta}s grouped together.
Walter Jangcab3dce2015-02-09 17:48:03 -080081 */
Walter Jangb6ca2722015-02-20 11:10:25 -080082public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener {
Walter Jangcab3dce2015-02-09 17:48:03 -080083
Walter Jang4f5594a2015-10-06 18:40:31 -070084 static final String TAG = "CompactEditorView";
Walter Jangcab3dce2015-02-09 17:48:03 -080085
Walter Jang192a01c2015-09-22 15:23:55 -070086 private static final KindSectionDataMapEntryComparator
87 KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR = new KindSectionDataMapEntryComparator();
Walter Jangf5dfea42015-09-16 12:30:36 -070088
Walter Jangb6ca2722015-02-20 11:10:25 -080089 /**
90 * Callbacks for hosts of {@link CompactRawContactsEditorView}s.
91 */
92 public interface Listener {
93
94 /**
Walter Jang151f3e62015-02-26 15:29:40 -080095 * Invoked when the structured name editor field has changed.
96 *
97 * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}.
98 * @param valuesDelta The values from the underlying {@link RawContactDelta}.
99 */
100 public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta);
Walter Jang708ea9e2015-09-10 15:42:05 -0700101
102 /**
103 * Invoked when the compact editor should rebind editors for a new account.
104 *
105 * @param oldState Old data being edited.
106 * @param oldAccount Old account associated with oldState.
107 * @param newAccount New account to be used.
108 */
109 public void onRebindEditorsForNewContact(RawContactDelta oldState,
110 AccountWithDataSet oldAccount, AccountWithDataSet newAccount);
Walter Jang79658e12015-09-24 10:36:26 -0700111
112 /**
113 * Invoked when no editors could be bound for the contact.
114 */
115 public void onBindEditorsFailed();
Walter Jangd6753152015-10-02 09:23:13 -0700116
117 /**
118 * Invoked after editors have been bound for the contact.
119 */
120 public void onEditorsBound();
Tingting Wang655ad1a2015-10-05 17:51:14 -0700121
122 /**
Tingting Wang5585c6e2015-10-14 11:19:58 -0700123 * Invoked when a rawcontact from linked contacts is selected in editor.
Tingting Wang655ad1a2015-10-05 17:51:14 -0700124 */
Tingting Wang91cee282015-10-07 13:48:17 -0700125 public void onRawContactSelected(Uri uri, long rawContactId, boolean isReadOnly);
Walter Jang3f18d612015-10-07 16:01:05 -0700126
127 /**
128 * Returns the map of raw contact IDs to newly taken or selected photos that have not
129 * yet been saved to CP2.
130 */
131 public Bundle getUpdatedPhotos();
Tingting Wang655ad1a2015-10-05 17:51:14 -0700132 }
133
134 /**
135 * Used to list the account info for the given raw contacts list.
136 */
137 private static final class RawContactAccountListAdapter extends BaseAdapter {
138 private final LayoutInflater mInflater;
139 private final Context mContext;
140 private final RawContactDeltaList mRawContactDeltas;
141
142 public RawContactAccountListAdapter(Context context, RawContactDeltaList rawContactDeltas) {
143 mContext = context;
144 mRawContactDeltas = new RawContactDeltaList();
145 for (RawContactDelta rawContactDelta : rawContactDeltas) {
Walter Jang23709542015-10-22 12:50:58 -0700146 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700147 mRawContactDeltas.add(rawContactDelta);
148 }
149 }
150 mInflater = LayoutInflater.from(context);
151 }
152
153 @Override
154 public View getView(int position, View convertView, ViewGroup parent) {
155 final View resultView = convertView != null ? convertView
156 : mInflater.inflate(R.layout.account_selector_list_item, parent, false);
157
158 final RawContactDelta rawContactDelta = mRawContactDeltas.get(position);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700159
160 final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1);
Walter Jang9488a762015-10-16 13:42:48 -0700161 final AccountType accountType = rawContactDelta.getRawContactAccountType(mContext);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700162 text1.setText(accountType.getDisplayLabel(mContext));
163
Tingting Wang655ad1a2015-10-05 17:51:14 -0700164 final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2);
Walter Jang9488a762015-10-16 13:42:48 -0700165 final String accountName = rawContactDelta.getAccountName();
Tingting Wanga0b69402015-10-13 14:27:58 -0700166 if (TextUtils.isEmpty(accountName)) {
167 text2.setVisibility(View.GONE);
168 } else {
Walter Jang9488a762015-10-16 13:42:48 -0700169 // Truncate email addresses in the middle so we don't lose the domain
Tingting Wanga0b69402015-10-13 14:27:58 -0700170 text2.setText(accountName);
171 text2.setEllipsize(TextUtils.TruncateAt.MIDDLE);
172 }
Tingting Wang655ad1a2015-10-05 17:51:14 -0700173
174 final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon);
175 icon.setImageDrawable(accountType.getDisplayIcon(mContext));
176
177 return resultView;
178 }
179
180 @Override
181 public int getCount() {
182 return mRawContactDeltas.size();
183 }
184
185 @Override
186 public RawContactDelta getItem(int position) {
187 return mRawContactDeltas.get(position);
188 }
189
190 @Override
191 public long getItemId(int position) {
192 return getItem(position).getRawContactId();
193 }
Walter Jang151f3e62015-02-26 15:29:40 -0800194 }
195
Walter Jang192a01c2015-09-22 15:23:55 -0700196 /** Used to sort entire kind sections. */
197 private static final class KindSectionDataMapEntryComparator implements
Walter Jangcbd431d2015-10-06 13:07:30 -0700198 Comparator<Map.Entry<String,KindSectionDataList>> {
Walter Jangf5dfea42015-09-16 12:30:36 -0700199
Walter Jang192a01c2015-09-22 15:23:55 -0700200 final MimeTypeComparator mMimeTypeComparator = new MimeTypeComparator();
201
202 @Override
Walter Jangcbd431d2015-10-06 13:07:30 -0700203 public int compare(Map.Entry<String, KindSectionDataList> entry1,
204 Map.Entry<String, KindSectionDataList> entry2) {
Walter Jang192a01c2015-09-22 15:23:55 -0700205 if (entry1 == entry2) return 0;
206 if (entry1 == null) return -1;
207 if (entry2 == null) return 1;
208
209 final String mimeType1 = entry1.getKey();
210 final String mimeType2 = entry2.getKey();
211
212 return mMimeTypeComparator.compare(mimeType1, mimeType2);
213 }
214 }
215
216 /**
217 * Sorts kinds roughly the same as quick contacts; we diverge in the following ways:
218 * <ol>
219 * <li>All names are together at the top.</li>
220 * <li>IM is moved up after addresses</li>
221 * <li>SIP addresses are moved to below phone numbers</li>
Walter Jang79658e12015-09-24 10:36:26 -0700222 * <li>Group membership is placed at the end</li>
Walter Jang192a01c2015-09-22 15:23:55 -0700223 * </ol>
224 */
225 private static final class MimeTypeComparator implements Comparator<String> {
226
Walter Jangf5dfea42015-09-16 12:30:36 -0700227 private static final List<String> MIME_TYPE_ORDER = Arrays.asList(new String[] {
228 StructuredName.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700229 Nickname.CONTENT_ITEM_TYPE,
Walter Jang2ae21752015-09-25 11:25:38 -0700230 Organization.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700231 Phone.CONTENT_ITEM_TYPE,
232 SipAddress.CONTENT_ITEM_TYPE,
233 Email.CONTENT_ITEM_TYPE,
234 StructuredPostal.CONTENT_ITEM_TYPE,
235 Im.CONTENT_ITEM_TYPE,
236 Website.CONTENT_ITEM_TYPE,
Walter Jangf5dfea42015-09-16 12:30:36 -0700237 Event.CONTENT_ITEM_TYPE,
238 Relation.CONTENT_ITEM_TYPE,
Walter Jangf10ca152015-09-22 15:23:55 -0700239 Note.CONTENT_ITEM_TYPE,
240 GroupMembership.CONTENT_ITEM_TYPE
Walter Jangf5dfea42015-09-16 12:30:36 -0700241 });
242
243 @Override
Walter Jang192a01c2015-09-22 15:23:55 -0700244 public int compare(String mimeType1, String mimeType2) {
245 if (mimeType1 == mimeType2) return 0;
246 if (mimeType1 == null) return -1;
247 if (mimeType2 == null) return 1;
Walter Jangf5dfea42015-09-16 12:30:36 -0700248
249 int index1 = MIME_TYPE_ORDER.indexOf(mimeType1);
250 int index2 = MIME_TYPE_ORDER.indexOf(mimeType2);
251
252 // Fallback to alphabetical ordering of the mime type if both are not found
253 if (index1 < 0 && index2 < 0) return mimeType1.compareTo(mimeType2);
254 if (index1 < 0) return 1;
255 if (index2 < 0) return -1;
256
257 return index1 < index2 ? -1 : 1;
258 }
259 }
260
Walter Jang192a01c2015-09-22 15:23:55 -0700261 /**
262 * Sorts primary accounts and google account types before others.
263 */
264 private static final class EditorComparator implements Comparator<KindSectionData> {
265
266 private RawContactDeltaComparator mRawContactDeltaComparator;
267
268 private EditorComparator(Context context) {
269 mRawContactDeltaComparator = new RawContactDeltaComparator(context);
270 }
271
272 @Override
273 public int compare(KindSectionData kindSectionData1, KindSectionData kindSectionData2) {
274 if (kindSectionData1 == kindSectionData2) return 0;
275 if (kindSectionData1 == null) return -1;
276 if (kindSectionData2 == null) return 1;
277
278 final RawContactDelta rawContactDelta1 = kindSectionData1.getRawContactDelta();
279 final RawContactDelta rawContactDelta2 = kindSectionData2.getRawContactDelta();
280
281 if (rawContactDelta1 == rawContactDelta2) return 0;
282 if (rawContactDelta1 == null) return -1;
283 if (rawContactDelta2 == null) return 1;
284
285 return mRawContactDeltaComparator.compare(rawContactDelta1, rawContactDelta2);
286 }
287 }
288
Walter Jang79658e12015-09-24 10:36:26 -0700289 public static class SavedState extends BaseSavedState {
290
291 public static final Parcelable.Creator<SavedState> CREATOR =
292 new Parcelable.Creator<SavedState>() {
293 public SavedState createFromParcel(Parcel in) {
294 return new SavedState(in);
295 }
296 public SavedState[] newArray(int size) {
297 return new SavedState[size];
298 }
299 };
300
301 private boolean mIsExpanded;
302
303 public SavedState(Parcelable superState) {
304 super(superState);
305 }
306
307 private SavedState(Parcel in) {
308 super(in);
309 mIsExpanded = in.readInt() != 0;
310 }
311
312 @Override
313 public void writeToParcel(Parcel out, int flags) {
314 super.writeToParcel(out, flags);
315 out.writeInt(mIsExpanded ? 1 : 0);
316 }
317 }
318
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700319 private CompactRawContactsEditorView.Listener mListener;
Walter Jangb6ca2722015-02-20 11:10:25 -0800320
Walter Jangcab3dce2015-02-09 17:48:03 -0800321 private AccountTypeManager mAccountTypeManager;
322 private LayoutInflater mLayoutInflater;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800323
Walter Jangcab3dce2015-02-09 17:48:03 -0800324 private ViewIdGenerator mViewIdGenerator;
Walter Jangf46abd82015-02-20 16:52:04 -0800325 private MaterialColorMapUtils.MaterialPalette mMaterialPalette;
Walter Jang82acd422015-10-17 14:01:27 -0700326 private long mPhotoId = -1;
Walter Jang708ea9e2015-09-10 15:42:05 -0700327 private boolean mHasNewContact;
328 private boolean mIsUserProfile;
329 private AccountWithDataSet mPrimaryAccount;
Walter Jangcbd431d2015-10-06 13:07:30 -0700330 private Map<String,KindSectionDataList> mKindSectionDataMap = new HashMap<>();
Walter Jangcab3dce2015-02-09 17:48:03 -0800331
Walter Jang708ea9e2015-09-10 15:42:05 -0700332 // Account header
333 private View mAccountHeaderContainer;
334 private TextView mAccountHeaderType;
335 private TextView mAccountHeaderName;
Tingting Wang91cee282015-10-07 13:48:17 -0700336 private ImageView mAccountHeaderIcon;
Walter Jang708ea9e2015-09-10 15:42:05 -0700337
338 // Account selector
339 private View mAccountSelectorContainer;
340 private View mAccountSelector;
341 private TextView mAccountSelectorType;
342 private TextView mAccountSelectorName;
343
Tingting Wang655ad1a2015-10-05 17:51:14 -0700344 // Raw contacts selector
345 private View mRawContactContainer;
346 private TextView mRawContactSummary;
Tingting Wang655ad1a2015-10-05 17:51:14 -0700347
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700348 private CompactPhotoEditorView mPhotoView;
Walter Jangf5dfea42015-09-16 12:30:36 -0700349 private ViewGroup mKindSectionViews;
Walter Jangf10ca152015-09-22 15:23:55 -0700350 private Map<String,List<CompactKindSectionView>> mKindSectionViewsMap = new HashMap<>();
Walter Jangb6ca2722015-02-20 11:10:25 -0800351 private View mMoreFields;
Walter Jangcab3dce2015-02-09 17:48:03 -0800352
Walter Jang79658e12015-09-24 10:36:26 -0700353 private boolean mIsExpanded;
Walter Jangcbd431d2015-10-06 13:07:30 -0700354
Walter Jang3efae4a2015-02-18 11:12:00 -0800355 private long mPhotoRawContactId;
Walter Jang31a74ad2015-10-02 19:17:39 -0700356 private ValuesDelta mPhotoValuesDelta;
Walter Jang82acd422015-10-17 14:01:27 -0700357
358 private Pair<KindSectionData, ValuesDelta> mPrimaryNameKindSectionData;
Walter Jang3efae4a2015-02-18 11:12:00 -0800359
Walter Jangcab3dce2015-02-09 17:48:03 -0800360 public CompactRawContactsEditorView(Context context) {
361 super(context);
362 }
363
364 public CompactRawContactsEditorView(Context context, AttributeSet attrs) {
365 super(context, attrs);
366 }
367
Walter Jangb6ca2722015-02-20 11:10:25 -0800368 /**
369 * Sets the receiver for {@link CompactRawContactsEditorView} callbacks.
370 */
371 public void setListener(Listener listener) {
372 mListener = listener;
373 }
374
Walter Jangcab3dce2015-02-09 17:48:03 -0800375 @Override
376 protected void onFinishInflate() {
377 super.onFinishInflate();
378
379 mAccountTypeManager = AccountTypeManager.getInstance(getContext());
380 mLayoutInflater = (LayoutInflater)
381 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
382
Walter Jang708ea9e2015-09-10 15:42:05 -0700383 // Account header
384 mAccountHeaderContainer = findViewById(R.id.account_container);
385 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);
Walter Jang708ea9e2015-09-10 15:42:05 -0700388
389 // Account selector
390 mAccountSelectorContainer = findViewById(R.id.account_selector_container);
391 mAccountSelector = findViewById(R.id.account);
392 mAccountSelectorType = (TextView) findViewById(R.id.account_type_selector);
393 mAccountSelectorName = (TextView) findViewById(R.id.account_name_selector);
394
Tingting Wang655ad1a2015-10-05 17:51:14 -0700395 // Raw contacts selector
396 mRawContactContainer = findViewById(R.id.all_rawcontacts_accounts_container);
397 mRawContactSummary = (TextView) findViewById(R.id.rawcontacts_accounts_summary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700398
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700399 mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor);
Walter Jangf5dfea42015-09-16 12:30:36 -0700400 mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views);
Walter Jangb6ca2722015-02-20 11:10:25 -0800401 mMoreFields = findViewById(R.id.more_fields);
402 mMoreFields.setOnClickListener(this);
403 }
404
Walter Jangb6ca2722015-02-20 11:10:25 -0800405 @Override
406 public void onClick(View view) {
Walter Jangf5dfea42015-09-16 12:30:36 -0700407 if (view.getId() == R.id.more_fields) {
Walter Jangd6753152015-10-02 09:23:13 -0700408 showAllFields();
Walter Jangb6ca2722015-02-20 11:10:25 -0800409 }
Walter Jangcab3dce2015-02-09 17:48:03 -0800410 }
411
412 @Override
413 public void setEnabled(boolean enabled) {
414 super.setEnabled(enabled);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700415 final int childCount = mKindSectionViews.getChildCount();
416 for (int i = 0; i < childCount; i++) {
417 mKindSectionViews.getChildAt(i).setEnabled(enabled);
Walter Jangcab3dce2015-02-09 17:48:03 -0800418 }
419 }
420
Walter Jang79658e12015-09-24 10:36:26 -0700421 @Override
422 public Parcelable onSaveInstanceState() {
423 final Parcelable superState = super.onSaveInstanceState();
424 final SavedState savedState = new SavedState(superState);
425 savedState.mIsExpanded = mIsExpanded;
426 return savedState;
427 }
428
429 @Override
430 public void onRestoreInstanceState(Parcelable state) {
431 if(!(state instanceof SavedState)) {
432 super.onRestoreInstanceState(state);
433 return;
434 }
435 final SavedState savedState = (SavedState) state;
436 super.onRestoreInstanceState(savedState.getSuperState());
437 mIsExpanded = savedState.mIsExpanded;
Walter Jangd6753152015-10-02 09:23:13 -0700438 if (mIsExpanded) {
439 showAllFields();
Walter Jang79658e12015-09-24 10:36:26 -0700440 }
441 }
442
Walter Jang3efae4a2015-02-18 11:12:00 -0800443 /**
Walter Jang31a74ad2015-10-02 19:17:39 -0700444 * Pass through to {@link CompactPhotoEditorView#setListener}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800445 */
Walter Jang31a74ad2015-10-02 19:17:39 -0700446 public void setPhotoListener(CompactPhotoEditorView.Listener listener) {
447 mPhotoView.setListener(listener);
Walter Jang3efae4a2015-02-18 11:12:00 -0800448 }
449
Walter Jang31a74ad2015-10-02 19:17:39 -0700450 public void removePhoto() {
Walter Jang45b86d52015-10-15 15:23:16 -0700451 mPhotoValuesDelta.setFromTemplate(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700452 mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null);
453
454 mPhotoView.removePhoto();
Walter Jang3efae4a2015-02-18 11:12:00 -0800455 }
456
457 /**
Walter Jang41b3ea12015-03-09 17:30:06 -0700458 * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}.
459 */
460 public void setFullSizePhoto(Uri photoUri) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700461 mPhotoView.setFullSizedPhoto(photoUri);
Walter Jang41b3ea12015-03-09 17:30:06 -0700462 }
463
Walter Jang31a74ad2015-10-02 19:17:39 -0700464 public void updatePhoto(Uri photoUri) {
Walter Jang45b86d52015-10-15 15:23:16 -0700465 mPhotoValuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700466 // Unset primary for all photos
Wenyi Wang9086fb92015-11-16 09:58:20 -0800467 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700468 // Mark the currently displayed photo as primary
469 mPhotoValuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700470
Wenyi Wang4c9cf3a2015-10-15 17:58:23 -0700471 // Even though high-res photos cannot be saved by passing them via
472 // an EntityDeltaList (since they cause the Bundle size limit to be
473 // exceeded), we still pass a low-res thumbnail. This simplifies
474 // code all over the place, because we don't have to test whether
475 // there is a change in EITHER the delta-list OR a changed photo...
476 // this way, there is always a change in the delta-list.
477 try {
478 final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
479 getContext(), photoUri);
480 if (bytes != null) {
481 mPhotoValuesDelta.setPhoto(bytes);
482 }
483 } catch (FileNotFoundException e) {
484 elog("Failed to get bitmap from photo Uri");
485 }
486
Walter Jang31a74ad2015-10-02 19:17:39 -0700487 mPhotoView.setFullSizedPhoto(photoUri);
488 }
489
Wenyi Wang9086fb92015-11-16 09:58:20 -0800490 private void unsetSuperPrimaryFromAllPhotos() {
Walter Jang3f18d612015-10-07 16:01:05 -0700491 final List<KindSectionData> kindSectionDataList =
492 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
493 for (KindSectionData kindSectionData : kindSectionDataList) {
Wenyi Wang9086fb92015-11-16 09:58:20 -0800494 for (ValuesDelta valuesDelta : kindSectionData.getNonEmptyValuesDeltas()) {
495 valuesDelta.setSuperPrimary(false);
Walter Jang3f18d612015-10-07 16:01:05 -0700496 }
497 }
498 }
499
Walter Jang41b3ea12015-03-09 17:30:06 -0700500 /**
Walter Janga5e4bb22015-02-24 13:08:16 -0800501 * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800502 */
503 public boolean isWritablePhotoSet() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700504 return mPhotoView.isWritablePhotoSet();
Walter Jang3efae4a2015-02-18 11:12:00 -0800505 }
506
507 /**
Walter Jang3efae4a2015-02-18 11:12:00 -0800508 * Get the raw contact ID for the CompactHeaderView photo.
509 */
Walter Jang3efae4a2015-02-18 11:12:00 -0800510 public long getPhotoRawContactId() {
511 return mPhotoRawContactId;
512 }
513
Walter Jang4f5594a2015-10-06 18:40:31 -0700514 public StructuredNameEditorView getPrimaryNameEditorView() {
Walter Jange3945952015-10-27 12:44:54 -0700515 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
516 return primaryNameKindSectionView == null
517 ? null : primaryNameKindSectionView.getPrimaryNameEditorView();
Walter Jang4f5594a2015-10-06 18:40:31 -0700518 }
519
Walter Jang31a74ad2015-10-02 19:17:39 -0700520 /**
521 * Returns a data holder for every non-default/non-empty photo from each raw contact, whether
522 * the raw contact is writable or not.
523 */
524 public ArrayList<CompactPhotoSelectionFragment.Photo> getPhotos() {
525 final ArrayList<CompactPhotoSelectionFragment.Photo> photos = new ArrayList<>();
526
Walter Jang3f18d612015-10-07 16:01:05 -0700527 final Bundle updatedPhotos = mListener == null ? null : mListener.getUpdatedPhotos();
528
Walter Jang31a74ad2015-10-02 19:17:39 -0700529 final List<KindSectionData> kindSectionDataList =
530 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
531 for (int i = 0; i < kindSectionDataList.size(); i++) {
532 final KindSectionData kindSectionData = kindSectionDataList.get(i);
533 final AccountType accountType = kindSectionData.getAccountType();
Walter Jang228e02f2015-11-13 09:11:01 -0800534 final List<ValuesDelta> valuesDeltas = kindSectionData.getNonEmptyValuesDeltas();
535 if (valuesDeltas.isEmpty()) continue;
536 for (int j = 0; j < valuesDeltas.size(); j++) {
537 final ValuesDelta valuesDelta = valuesDeltas.get(j);
Walter Jang31a74ad2015-10-02 19:17:39 -0700538 final Bitmap bitmap = EditorUiUtils.getPhotoBitmap(valuesDelta);
539 if (bitmap == null) continue;
540
541 final CompactPhotoSelectionFragment.Photo photo =
542 new CompactPhotoSelectionFragment.Photo();
543 photo.titleRes = accountType.titleRes;
544 photo.iconRes = accountType.iconRes;
545 photo.syncAdapterPackageName = accountType.syncAdapterPackageName;
546 photo.valuesDelta = valuesDelta;
547 photo.primary = valuesDelta.isSuperPrimary();
548 photo.kindSectionDataListIndex = i;
549 photo.valuesDeltaListIndex = j;
Wenyi Wang0b144562015-11-06 18:17:46 -0800550 photo.accountType = accountType.getDisplayLabel(getContext()).toString();
551 photo.accountName = kindSectionData.getRawContactDelta().getAccountName();
Walter Jang3f18d612015-10-07 16:01:05 -0700552
553 if (updatedPhotos != null) {
554 photo.updatedPhotoUri = (Uri) updatedPhotos.get(String.valueOf(
555 kindSectionData.getRawContactDelta().getRawContactId()));
556 }
557
Walter Jang31a74ad2015-10-02 19:17:39 -0700558 photos.add(photo);
559 }
560 }
561
562 return photos;
563 }
564
565 /**
566 * Marks the raw contact photo given as primary for the aggregate contact and updates the
567 * UI.
568 */
569 public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700570 // Find the values delta to mark as primary
Walter Jang3f18d612015-10-07 16:01:05 -0700571 final KindSectionDataList kindSectionDataList =
572 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
Walter Jang31a74ad2015-10-02 19:17:39 -0700573 if (photo.kindSectionDataListIndex < 0
574 || photo.kindSectionDataListIndex >= kindSectionDataList.size()) {
575 wlog("Invalid kind section data list index");
576 return;
577 }
578 final KindSectionData kindSectionData =
579 kindSectionDataList.get(photo.kindSectionDataListIndex);
Walter Jang228e02f2015-11-13 09:11:01 -0800580 final List<ValuesDelta> valuesDeltaList = kindSectionData.getNonEmptyValuesDeltas();
Walter Jang31a74ad2015-10-02 19:17:39 -0700581 if (photo.valuesDeltaListIndex >= valuesDeltaList.size()) {
582 wlog("Invalid values delta list index");
583 return;
584 }
585 final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex);
586 valuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700587 // Unset primary for all photos
Wenyi Wang9086fb92015-11-16 09:58:20 -0800588 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700589 // Mark the currently displayed photo as primary
590 valuesDelta.setSuperPrimary(true);
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) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700620 mKindSectionDataMap.clear();
Walter Jangf5dfea42015-09-16 12:30:36 -0700621 mKindSectionViews.removeAllViews();
Walter Jang363d3fd2015-09-16 10:29:07 -0700622 mMoreFields.setVisibility(View.VISIBLE);
Walter Jangcab3dce2015-02-09 17:48:03 -0800623
Walter Jangf46abd82015-02-20 16:52:04 -0800624 mMaterialPalette = materialPalette;
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700625 mViewIdGenerator = viewIdGenerator;
Walter Jang708ea9e2015-09-10 15:42:05 -0700626 mPhotoId = photoId;
Tingting Wangf695eb32015-10-15 18:45:15 -0700627
Walter Jang708ea9e2015-09-10 15:42:05 -0700628 mHasNewContact = hasNewContact;
629 mIsUserProfile = isUserProfile;
630 mPrimaryAccount = primaryAccount;
631 if (mPrimaryAccount == null) {
632 mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount();
633 }
634 vlog("state: primary " + mPrimaryAccount);
Walter Jangcab3dce2015-02-09 17:48:03 -0800635
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700636 // Parse the given raw contact deltas
637 if (rawContactDeltas == null || rawContactDeltas.isEmpty()) {
638 elog("No raw contact deltas");
Walter Jang79658e12015-09-24 10:36:26 -0700639 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jangf5dfea42015-09-16 12:30:36 -0700640 return;
641 }
Walter Jangcbd431d2015-10-06 13:07:30 -0700642 parseRawContactDeltas(rawContactDeltas);
Walter Jang79658e12015-09-24 10:36:26 -0700643 if (mKindSectionDataMap.isEmpty()) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700644 elog("No kind section data parsed from RawContactDelta(s)");
Walter Jang79658e12015-09-24 10:36:26 -0700645 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700646 return;
647 }
Walter Jang82acd422015-10-17 14:01:27 -0700648
649 // Get the primary name kind section data
650 mPrimaryNameKindSectionData = mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE)
651 .getEntryToWrite(/* id =*/ -1, mPrimaryAccount, mIsUserProfile);
652 if (mPrimaryNameKindSectionData != null) {
653 // Ensure that a structured name and photo exists
654 final RawContactDelta rawContactDelta =
655 mPrimaryNameKindSectionData.first.getRawContactDelta();
656 RawContactModifier.ensureKindExists(
657 rawContactDelta,
658 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700659 StructuredName.CONTENT_ITEM_TYPE);
Walter Jang82acd422015-10-17 14:01:27 -0700660 RawContactModifier.ensureKindExists(
661 rawContactDelta,
662 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700663 Photo.CONTENT_ITEM_TYPE);
664 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700665
666 // Setup the view
Tingting Wang655ad1a2015-10-05 17:51:14 -0700667 addAccountInfo(rawContactDeltas);
Walter Jang363d3fd2015-09-16 10:29:07 -0700668 addPhotoView();
Walter Jangf5dfea42015-09-16 12:30:36 -0700669 addKindSectionViews();
Walter Jange3945952015-10-27 12:44:54 -0700670
Walter Jang4f5594a2015-10-06 18:40:31 -0700671 if (mIsExpanded) showAllFields();
Walter Jangd6753152015-10-02 09:23:13 -0700672
673 if (mListener != null) mListener.onEditorsBound();
Walter Jangcab3dce2015-02-09 17:48:03 -0800674 }
675
Walter Jangcbd431d2015-10-06 13:07:30 -0700676 private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700677 // Build the kind section data list map
Walter Jang192a01c2015-09-22 15:23:55 -0700678 vlog("parse: " + rawContactDeltas.size() + " rawContactDelta(s)");
679 for (int j = 0; j < rawContactDeltas.size(); j++) {
680 final RawContactDelta rawContactDelta = rawContactDeltas.get(j);
681 vlog("parse: " + j + " rawContactDelta" + rawContactDelta);
Walter Jang363d3fd2015-09-16 10:29:07 -0700682 if (rawContactDelta == null || !rawContactDelta.isVisible()) continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800683 final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
Walter Jang363d3fd2015-09-16 10:29:07 -0700684 if (accountType == null) continue;
685 final List<DataKind> dataKinds = accountType.getSortedDataKinds();
686 final int dataKindSize = dataKinds == null ? 0 : dataKinds.size();
687 vlog("parse: " + dataKindSize + " dataKinds(s)");
688 for (int i = 0; i < dataKindSize; i++) {
689 final DataKind dataKind = dataKinds.get(i);
Walter Jang192a01c2015-09-22 15:23:55 -0700690 if (dataKind == null || !dataKind.editable) {
691 vlog("parse: " + i + " " + dataKind.mimeType + " dropped read-only");
692 continue;
693 }
694 final String mimeType = dataKind.mimeType;
695
696 // Skip psuedo mime types
697 if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
698 || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
699 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
700 continue;
701 }
702
Walter Jang3f18d612015-10-07 16:01:05 -0700703 final KindSectionDataList kindSectionDataList =
Walter Jangcbd431d2015-10-06 13:07:30 -0700704 getOrCreateKindSectionDataList(mimeType);
Walter Jang363d3fd2015-09-16 10:29:07 -0700705 final KindSectionData kindSectionData =
706 new KindSectionData(accountType, dataKind, rawContactDelta);
707 kindSectionDataList.add(kindSectionData);
Walter Jang192a01c2015-09-22 15:23:55 -0700708
Walter Jang363d3fd2015-09-16 10:29:07 -0700709 vlog("parse: " + i + " " + dataKind.mimeType + " " +
Walter Jang228e02f2015-11-13 09:11:01 -0800710 kindSectionData.getValuesDeltas().size() + " value(s) " +
711 kindSectionData.getNonEmptyValuesDeltas().size() + " non-empty value(s)" +
Wenyi Wang42d740c2015-11-13 14:23:54 -0800712 kindSectionData.getVisibleValuesDeltas().size() +
Walter Jang228e02f2015-11-13 09:11:01 -0800713 " visible value(s)");
Walter Jangac679af2015-06-01 12:17:06 -0700714 }
715 }
Walter Jang3efae4a2015-02-18 11:12:00 -0800716 }
717
Walter Jang3f18d612015-10-07 16:01:05 -0700718 private KindSectionDataList getOrCreateKindSectionDataList(String mimeType) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700719 KindSectionDataList kindSectionDataList = mKindSectionDataMap.get(mimeType);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700720 if (kindSectionDataList == null) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700721 kindSectionDataList = new KindSectionDataList();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700722 mKindSectionDataMap.put(mimeType, kindSectionDataList);
723 }
724 return kindSectionDataList;
725 }
726
Tingting Wang655ad1a2015-10-05 17:51:14 -0700727 private void addAccountInfo(RawContactDeltaList rawContactDeltas) {
Walter Jang82acd422015-10-17 14:01:27 -0700728 mAccountHeaderContainer.setVisibility(View.GONE);
729 mAccountSelectorContainer.setVisibility(View.GONE);
730 mRawContactContainer.setVisibility(View.GONE);
731
732 if (mPrimaryNameKindSectionData == null) return;
733 final RawContactDelta rawContactDelta =
734 mPrimaryNameKindSectionData.first.getRawContactDelta();
Walter Jang708ea9e2015-09-10 15:42:05 -0700735
736 // Get the account information for the primary raw contact delta
Walter Jangbe5e1b12015-10-17 11:38:29 -0700737 final Pair<String,String> accountInfo = mIsUserProfile
738 ? EditorUiUtils.getLocalAccountInfo(getContext(),
Walter Jang82acd422015-10-17 14:01:27 -0700739 rawContactDelta.getAccountName(),
740 rawContactDelta.getAccountType(mAccountTypeManager))
Walter Jangbe5e1b12015-10-17 11:38:29 -0700741 : EditorUiUtils.getAccountInfo(getContext(),
Walter Jang82acd422015-10-17 14:01:27 -0700742 rawContactDelta.getAccountName(),
743 rawContactDelta.getAccountType(mAccountTypeManager));
Walter Jang708ea9e2015-09-10 15:42:05 -0700744
Walter Jang23709542015-10-22 12:50:58 -0700745 // Either the account header or selector should be shown, not both.
Walter Jang708ea9e2015-09-10 15:42:05 -0700746 final List<AccountWithDataSet> accounts =
747 AccountTypeManager.getInstance(getContext()).getAccounts(true);
Walter Jang82acd422015-10-17 14:01:27 -0700748 if (mHasNewContact && !mIsUserProfile) {
749 if (accounts.size() > 1) {
750 addAccountSelector(accountInfo, rawContactDelta);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700751 } else {
Tingting Wang1594c6f2015-10-26 17:39:16 -0700752 addAccountHeader(accountInfo);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700753 }
Tingting Wang1594c6f2015-10-26 17:39:16 -0700754 } else if (mIsUserProfile || !shouldHideAccountContainer(rawContactDeltas)) {
755 addAccountHeader(accountInfo);
Walter Jang708ea9e2015-09-10 15:42:05 -0700756 }
Walter Jang23709542015-10-22 12:50:58 -0700757
758 // The raw contact selector should only display linked raw contacts that can be edited in
759 // the full editor (i.e. they are not newly created raw contacts)
Tingting Wang2bb85d22015-10-23 13:01:09 -0700760 final RawContactAccountListAdapter adapter = new RawContactAccountListAdapter(getContext(),
761 getRawContactDeltaListForSelector(rawContactDeltas));
762 if (adapter.getCount() > 0) {
763 final String accountsSummary = getResources().getQuantityString(
764 R.plurals.compact_editor_linked_contacts_selector_title,
765 adapter.getCount(), adapter.getCount());
Walter Jang23709542015-10-22 12:50:58 -0700766 addRawContactAccountSelector(accountsSummary, adapter);
767 }
Walter Jang708ea9e2015-09-10 15:42:05 -0700768 }
769
Tingting Wang2bb85d22015-10-23 13:01:09 -0700770 private RawContactDeltaList getRawContactDeltaListForSelector(
771 RawContactDeltaList rawContactDeltas) {
772 // Sort raw contacts so google accounts come first
773 Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
774
775 final RawContactDeltaList result = new RawContactDeltaList();
776 for (RawContactDelta rawContactDelta : rawContactDeltas) {
777 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
778 // Only add raw contacts that can be opened in the editor
779 result.add(rawContactDelta);
780 }
781 }
782 // Don't return a list of size 1 that would just open the raw contact being edited
783 // in the compact editor in the full editor
784 if (result.size() == 1 && result.get(0).getRawContactAccountType(
785 getContext()).areContactsWritable()) {
786 result.clear();
787 return result;
788 }
789 return result;
790 }
791
Tingting Wang1594c6f2015-10-26 17:39:16 -0700792 // Returns true if there are multiple writable rawcontacts and no read-only ones,
793 // or there are both writable and read-only rawcontacts.
794 private boolean shouldHideAccountContainer(RawContactDeltaList rawContactDeltas) {
Tingting Wang2bb85d22015-10-23 13:01:09 -0700795 int writable = 0;
796 int readonly = 0;
797 for (RawContactDelta rawContactDelta : rawContactDeltas) {
798 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
799 if (rawContactDelta.getRawContactAccountType(getContext()).areContactsWritable()) {
800 writable++;
801 } else {
802 readonly++;
803 }
804 }
805 }
806 return (writable > 1 || (writable > 0 && readonly > 0));
807 }
808
Tingting Wang1594c6f2015-10-26 17:39:16 -0700809 private void addAccountHeader(Pair<String,String> accountInfo) {
Walter Jang82acd422015-10-17 14:01:27 -0700810 mAccountHeaderContainer.setVisibility(View.VISIBLE);
811
Walter Jangbe5e1b12015-10-17 11:38:29 -0700812 // Set the account name
813 final String accountName = TextUtils.isEmpty(accountInfo.first)
814 ? accountInfo.second : accountInfo.first;
815 mAccountHeaderName.setVisibility(View.VISIBLE);
816 mAccountHeaderName.setText(accountName);
817
818 // Set the account type
819 final String selectorTitle = getResources().getString(
820 R.string.compact_editor_account_selector_title);
821 mAccountHeaderType.setText(selectorTitle);
822
823 // Set the icon
Walter Jang82acd422015-10-17 14:01:27 -0700824 if (mPrimaryNameKindSectionData != null) {
825 final RawContactDelta rawContactDelta =
826 mPrimaryNameKindSectionData.first.getRawContactDelta();
827 if (rawContactDelta != null) {
828 final AccountType accountType =
829 rawContactDelta.getRawContactAccountType(getContext());
830 mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext()));
831 }
832 }
Walter Jangbe5e1b12015-10-17 11:38:29 -0700833
834 // Set the content description
835 mAccountHeaderContainer.setContentDescription(
836 EditorUiUtils.getAccountInfoContentDescription(accountName, selectorTitle));
837 }
838
Walter Jang82acd422015-10-17 14:01:27 -0700839 private void addAccountSelector(Pair<String,String> accountInfo,
840 final RawContactDelta rawContactDelta) {
Walter Jang708ea9e2015-09-10 15:42:05 -0700841 mAccountSelectorContainer.setVisibility(View.VISIBLE);
842
Walter Jang03cea2e2015-09-18 17:04:21 -0700843 if (TextUtils.isEmpty(accountInfo.first)) {
Walter Jang708ea9e2015-09-10 15:42:05 -0700844 // Hide this view so the other text view will be centered vertically
845 mAccountSelectorName.setVisibility(View.GONE);
846 } else {
847 mAccountSelectorName.setVisibility(View.VISIBLE);
848 mAccountSelectorName.setText(accountInfo.first);
849 }
Walter Jang9488a762015-10-16 13:42:48 -0700850
851 final String selectorTitle = getResources().getString(
852 R.string.compact_editor_account_selector_title);
853 mAccountSelectorType.setText(selectorTitle);
Walter Jang708ea9e2015-09-10 15:42:05 -0700854
Tingting Wangb7933742015-11-02 10:52:57 -0800855 mAccountSelectorContainer.setContentDescription(getResources().getString(
856 R.string.compact_editor_account_selector_description, accountInfo.first));
Walter Jang708ea9e2015-09-10 15:42:05 -0700857
Wenyi Wang3e8cc352015-10-28 18:14:10 -0700858 mAccountSelectorContainer.setOnClickListener(new View.OnClickListener() {
Walter Jang708ea9e2015-09-10 15:42:05 -0700859 @Override
860 public void onClick(View v) {
861 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
862 final AccountsListAdapter adapter =
863 new AccountsListAdapter(getContext(),
864 AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE,
865 mPrimaryAccount);
866 popup.setWidth(mAccountSelectorContainer.getWidth());
867 popup.setAnchorView(mAccountSelectorContainer);
868 popup.setAdapter(adapter);
869 popup.setModal(true);
870 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
871 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
872 @Override
873 public void onItemClick(AdapterView<?> parent, View view, int position,
874 long id) {
875 UiClosables.closeQuietly(popup);
876 final AccountWithDataSet newAccount = adapter.getItem(position);
877 if (mListener != null && !mPrimaryAccount.equals(newAccount)) {
878 mListener.onRebindEditorsForNewContact(
Walter Jang82acd422015-10-17 14:01:27 -0700879 rawContactDelta,
Walter Jang708ea9e2015-09-10 15:42:05 -0700880 mPrimaryAccount,
881 newAccount);
882 }
883 }
884 });
885 popup.show();
886 }
887 });
888 }
889
Walter Jang23709542015-10-22 12:50:58 -0700890 private void addRawContactAccountSelector(String accountsSummary,
891 final RawContactAccountListAdapter adapter) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700892 mRawContactContainer.setVisibility(View.VISIBLE);
893
Tingting Wang655ad1a2015-10-05 17:51:14 -0700894 mRawContactSummary.setText(accountsSummary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700895
896 mRawContactContainer.setOnClickListener(new View.OnClickListener() {
897 @Override
898 public void onClick(View v) {
899 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700900 popup.setWidth(mRawContactContainer.getWidth());
901 popup.setAnchorView(mRawContactContainer);
902 popup.setAdapter(adapter);
903 popup.setModal(true);
904 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
905 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
906 @Override
907 public void onItemClick(AdapterView<?> parent, View view, int position,
908 long id) {
909 UiClosables.closeQuietly(popup);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700910
911 if (mListener != null) {
Tingting Wang91cee282015-10-07 13:48:17 -0700912 final long rawContactId = adapter.getItemId(position);
913 final Uri rawContactUri = ContentUris.withAppendedId(
914 ContactsContract.RawContacts.CONTENT_URI, rawContactId);
915 final RawContactDelta rawContactDelta = adapter.getItem(position);
916 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
917 getContext());
918 final AccountType accountType = rawContactDelta.getAccountType(
919 accountTypes);
920 final boolean isReadOnly = !accountType.areContactsWritable();
921
922 mListener.onRawContactSelected(rawContactUri, rawContactId, isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700923 }
924 }
925 });
926 popup.show();
927 }
928 });
929 }
930
Walter Jang363d3fd2015-09-16 10:29:07 -0700931 private void addPhotoView() {
Walter Jang31a74ad2015-10-02 19:17:39 -0700932 // Get the kind section data and values delta that we will display in the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700933 final KindSectionDataList kindSectionDataList =
934 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
935 final Pair<KindSectionData,ValuesDelta> photoToDisplay =
936 kindSectionDataList.getEntryToDisplay(mPhotoId);
937 if (photoToDisplay == null) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700938 wlog("photo: no kind section data parsed");
Walter Jangcbd431d2015-10-06 13:07:30 -0700939 mPhotoView.setVisibility(View.GONE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700940 return;
941 }
Walter Jang06f73a12015-06-17 11:15:48 -0700942
Walter Jang31a74ad2015-10-02 19:17:39 -0700943 // Set the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700944 mPhotoView.setPhoto(photoToDisplay.second, mMaterialPalette);
Walter Jang31a74ad2015-10-02 19:17:39 -0700945
946 // Find the raw contact ID and values delta that will be written when the photo is edited
Walter Jang82acd422015-10-17 14:01:27 -0700947 final Pair<KindSectionData, ValuesDelta> pair = kindSectionDataList.getEntryToWrite(
948 mPhotoId, mPrimaryAccount, mIsUserProfile);
949 if (pair == null) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700950 mPhotoView.setReadOnly(true);
951 return;
952 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700953 mPhotoView.setReadOnly(false);
Walter Jang82acd422015-10-17 14:01:27 -0700954 mPhotoRawContactId = pair.first.getRawContactDelta().getRawContactId();
955 mPhotoValuesDelta = pair.second;
Walter Jang31a74ad2015-10-02 19:17:39 -0700956 }
957
Walter Jangf5dfea42015-09-16 12:30:36 -0700958 private void addKindSectionViews() {
959 // Sort the kinds
Walter Jangcbd431d2015-10-06 13:07:30 -0700960 final TreeSet<Map.Entry<String,KindSectionDataList>> entries =
Walter Jang192a01c2015-09-22 15:23:55 -0700961 new TreeSet<>(KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR);
Walter Jangf5dfea42015-09-16 12:30:36 -0700962 entries.addAll(mKindSectionDataMap.entrySet());
Walter Jang3efae4a2015-02-18 11:12:00 -0800963
Walter Jangf5dfea42015-09-16 12:30:36 -0700964 vlog("kind: " + entries.size() + " kindSection(s)");
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700965 int i = -1;
Walter Jangcbd431d2015-10-06 13:07:30 -0700966 for (Map.Entry<String, KindSectionDataList> entry : entries) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700967 i++;
968
Walter Jangf5dfea42015-09-16 12:30:36 -0700969 final String mimeType = entry.getKey();
Walter Jang82acd422015-10-17 14:01:27 -0700970
Tingting Wangf695eb32015-10-15 18:45:15 -0700971 if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang82acd422015-10-17 14:01:27 -0700972 if (mPrimaryNameKindSectionData == null) {
973 vlog("kind: " + i + " " + mimeType + " dropped");
974 continue;
Tingting Wangf695eb32015-10-15 18:45:15 -0700975 }
Walter Jang82acd422015-10-17 14:01:27 -0700976 vlog("kind: " + i + " " + mimeType + " using first entry only");
977 final KindSectionDataList kindSectionDataList = new KindSectionDataList();
978 kindSectionDataList.add(mPrimaryNameKindSectionData.first);
Walter Jangf5dfea42015-09-16 12:30:36 -0700979 final CompactKindSectionView kindSectionView = inflateKindSectionView(
Walter Jang82acd422015-10-17 14:01:27 -0700980 mKindSectionViews, kindSectionDataList, mimeType,
981 mPrimaryNameKindSectionData.second);
Walter Jangf5dfea42015-09-16 12:30:36 -0700982 mKindSectionViews.addView(kindSectionView);
Walter Jangf10ca152015-09-22 15:23:55 -0700983
984 // Keep a pointer to all the KindSectionsViews for each mimeType
985 getKindSectionViews(mimeType).add(kindSectionView);
Walter Jang82acd422015-10-17 14:01:27 -0700986 } else {
987 final KindSectionDataList kindSectionDataList = entry.getValue();
988
989 // Ignore mime types that we've already handled
990 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
991 vlog("kind: " + i + " " + mimeType + " dropped");
992 continue;
993 }
994
995 // Don't show more than one group editor on the compact editor.
996 // Groups will still be editable for each raw contact individually on the full editor.
997 if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)
998 && kindSectionDataList.size() > 1) {
999 vlog("kind: " + i + " " + mimeType + " dropped");
1000 continue;
1001 }
1002
1003 if (kindSectionDataList != null && !kindSectionDataList.isEmpty()) {
1004 vlog("kind: " + i + " " + mimeType + " " + kindSectionDataList.size() +
1005 " kindSectionData(s)");
1006
1007 final CompactKindSectionView kindSectionView = inflateKindSectionView(
1008 mKindSectionViews, kindSectionDataList, mimeType,
1009 /* primaryValueDelta =*/ null);
1010 mKindSectionViews.addView(kindSectionView);
1011
1012 // Keep a pointer to all the KindSectionsViews for each mimeType
1013 getKindSectionViews(mimeType).add(kindSectionView);
1014 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001015 }
1016 }
1017 }
1018
Walter Jangf10ca152015-09-22 15:23:55 -07001019 private List<CompactKindSectionView> getKindSectionViews(String mimeType) {
1020 List<CompactKindSectionView> kindSectionViews = mKindSectionViewsMap.get(mimeType);
1021 if (kindSectionViews == null) {
1022 kindSectionViews = new ArrayList<>();
1023 mKindSectionViewsMap.put(mimeType, kindSectionViews);
1024 }
1025 return kindSectionViews;
1026 }
1027
Walter Jangf5dfea42015-09-16 12:30:36 -07001028 private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup,
Walter Jang82acd422015-10-17 14:01:27 -07001029 KindSectionDataList kindSectionDataList, String mimeType,
1030 ValuesDelta primaryValuesDelta) {
Walter Jangf5dfea42015-09-16 12:30:36 -07001031 final CompactKindSectionView kindSectionView = (CompactKindSectionView)
1032 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
1033 /* attachToRoot =*/ false);
Walter Jang45b86d52015-10-15 15:23:16 -07001034 kindSectionView.setIsUserProfile(mIsUserProfile);
Walter Jangf5dfea42015-09-16 12:30:36 -07001035
Walter Jang192a01c2015-09-22 15:23:55 -07001036 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Walter Jangf5dfea42015-09-16 12:30:36 -07001037 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001038 // Phone numbers and email addresses are always displayed,
1039 // even if they are empty
Walter Jangf5dfea42015-09-16 12:30:36 -07001040 kindSectionView.setHideWhenEmpty(false);
1041 }
Walter Jangf5dfea42015-09-16 12:30:36 -07001042
Walter Jang192a01c2015-09-22 15:23:55 -07001043 // Since phone numbers and email addresses displayed even if they are empty,
1044 // they will be the only types you add new values to initially for new contacts
1045 kindSectionView.setShowOneEmptyEditor(true);
1046
Walter Jang82acd422015-10-17 14:01:27 -07001047 // Sort non-name editors so they wind up in the order we want
1048 if (!StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001049 Collections.sort(kindSectionDataList, new EditorComparator(getContext()));
Walter Jang3e5ae0d2015-09-20 12:43:37 -07001050 }
1051
Walter Jang82acd422015-10-17 14:01:27 -07001052 kindSectionView.setState(kindSectionDataList, mViewIdGenerator, mListener,
1053 primaryValuesDelta);
Walter Jangf5dfea42015-09-16 12:30:36 -07001054
1055 return kindSectionView;
Walter Jangcab3dce2015-02-09 17:48:03 -08001056 }
1057
Walter Jange3945952015-10-27 12:44:54 -07001058 void maybeSetReadOnlyDisplayNameAsPrimary(String readOnlyDisplayName) {
1059 if (TextUtils.isEmpty(readOnlyDisplayName)) return;
1060 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
1061 if (primaryNameKindSectionView != null && primaryNameKindSectionView.isEmptyName()) {
1062 vlog("name: using read only display name as primary name");
1063 primaryNameKindSectionView.setName(readOnlyDisplayName);
1064 }
1065 }
1066
1067 private CompactKindSectionView getPrimaryNameKindSectionView() {
Walter Jang4f5594a2015-10-06 18:40:31 -07001068 final List<CompactKindSectionView> kindSectionViews
1069 = mKindSectionViewsMap.get(StructuredName.CONTENT_ITEM_TYPE);
Walter Jange3945952015-10-27 12:44:54 -07001070 return kindSectionViews == null || kindSectionViews.isEmpty()
1071 ? null : kindSectionViews.get(0);
Walter Jang4f5594a2015-10-06 18:40:31 -07001072 }
1073
Walter Jangd6753152015-10-02 09:23:13 -07001074 private void showAllFields() {
Walter Jang79658e12015-09-24 10:36:26 -07001075 // Stop hiding empty editors and allow the user to enter values for all kinds now
1076 for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
1077 final CompactKindSectionView kindSectionView =
1078 (CompactKindSectionView) mKindSectionViews.getChildAt(i);
1079 kindSectionView.setHideWhenEmpty(false);
1080 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
1081 }
1082 mIsExpanded = true;
Walter Jang79658e12015-09-24 10:36:26 -07001083
Walter Jangf5dfea42015-09-16 12:30:36 -07001084 // Hide the more fields button
1085 mMoreFields.setVisibility(View.GONE);
Walter Jangcab3dce2015-02-09 17:48:03 -08001086 }
1087
Walter Jangbf63a6d2015-05-05 09:14:35 -07001088 private static void vlog(String message) {
1089 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1090 Log.v(TAG, message);
Walter Jangcab3dce2015-02-09 17:48:03 -08001091 }
1092 }
Walter Jang363d3fd2015-09-16 10:29:07 -07001093
1094 private static void wlog(String message) {
1095 if (Log.isLoggable(TAG, Log.WARN)) {
1096 Log.w(TAG, message);
1097 }
1098 }
1099
1100 private static void elog(String message) {
1101 Log.e(TAG, message);
1102 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001103}