blob: d9cc58da18180a351e97dcd81bf1233bebb34285 [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;
Gary Mai46cb3102016-08-10 18:14:09 -0700337 private ImageView mAccountHeaderExpanderIcon;
Walter Jang708ea9e2015-09-10 15:42:05 -0700338
Tingting Wang655ad1a2015-10-05 17:51:14 -0700339 // Raw contacts selector
340 private View mRawContactContainer;
341 private TextView mRawContactSummary;
Tingting Wang655ad1a2015-10-05 17:51:14 -0700342
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700343 private CompactPhotoEditorView mPhotoView;
Walter Jangf5dfea42015-09-16 12:30:36 -0700344 private ViewGroup mKindSectionViews;
Walter Jangf10ca152015-09-22 15:23:55 -0700345 private Map<String,List<CompactKindSectionView>> mKindSectionViewsMap = new HashMap<>();
Walter Jangb6ca2722015-02-20 11:10:25 -0800346 private View mMoreFields;
Walter Jangcab3dce2015-02-09 17:48:03 -0800347
Walter Jang79658e12015-09-24 10:36:26 -0700348 private boolean mIsExpanded;
Walter Jangcbd431d2015-10-06 13:07:30 -0700349
Walter Jang3efae4a2015-02-18 11:12:00 -0800350 private long mPhotoRawContactId;
Walter Jang31a74ad2015-10-02 19:17:39 -0700351 private ValuesDelta mPhotoValuesDelta;
Walter Jang82acd422015-10-17 14:01:27 -0700352
353 private Pair<KindSectionData, ValuesDelta> mPrimaryNameKindSectionData;
Walter Jang3efae4a2015-02-18 11:12:00 -0800354
Walter Jangcab3dce2015-02-09 17:48:03 -0800355 public CompactRawContactsEditorView(Context context) {
356 super(context);
357 }
358
359 public CompactRawContactsEditorView(Context context, AttributeSet attrs) {
360 super(context, attrs);
361 }
362
Walter Jangb6ca2722015-02-20 11:10:25 -0800363 /**
364 * Sets the receiver for {@link CompactRawContactsEditorView} callbacks.
365 */
366 public void setListener(Listener listener) {
367 mListener = listener;
368 }
369
Walter Jangcab3dce2015-02-09 17:48:03 -0800370 @Override
371 protected void onFinishInflate() {
372 super.onFinishInflate();
373
374 mAccountTypeManager = AccountTypeManager.getInstance(getContext());
375 mLayoutInflater = (LayoutInflater)
376 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
377
Walter Jang708ea9e2015-09-10 15:42:05 -0700378 // Account header
Gary Mai46cb3102016-08-10 18:14:09 -0700379 mAccountHeaderContainer = findViewById(R.id.account_header_container);
Walter Jang708ea9e2015-09-10 15:42:05 -0700380 mAccountHeaderType = (TextView) findViewById(R.id.account_type);
381 mAccountHeaderName = (TextView) findViewById(R.id.account_name);
Tingting Wang91cee282015-10-07 13:48:17 -0700382 mAccountHeaderIcon = (ImageView) findViewById(R.id.account_type_icon);
Gary Mai46cb3102016-08-10 18:14:09 -0700383 mAccountHeaderExpanderIcon = (ImageView) findViewById(R.id.account_expander_icon);
Walter Jang708ea9e2015-09-10 15:42:05 -0700384
Tingting Wang655ad1a2015-10-05 17:51:14 -0700385 // Raw contacts selector
386 mRawContactContainer = findViewById(R.id.all_rawcontacts_accounts_container);
387 mRawContactSummary = (TextView) findViewById(R.id.rawcontacts_accounts_summary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700388
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700389 mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor);
Walter Jangf5dfea42015-09-16 12:30:36 -0700390 mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views);
Walter Jangb6ca2722015-02-20 11:10:25 -0800391 mMoreFields = findViewById(R.id.more_fields);
392 mMoreFields.setOnClickListener(this);
393 }
394
Walter Jangb6ca2722015-02-20 11:10:25 -0800395 @Override
396 public void onClick(View view) {
Walter Jangf5dfea42015-09-16 12:30:36 -0700397 if (view.getId() == R.id.more_fields) {
Walter Jangd6753152015-10-02 09:23:13 -0700398 showAllFields();
Walter Jangb6ca2722015-02-20 11:10:25 -0800399 }
Walter Jangcab3dce2015-02-09 17:48:03 -0800400 }
401
402 @Override
403 public void setEnabled(boolean enabled) {
404 super.setEnabled(enabled);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700405 final int childCount = mKindSectionViews.getChildCount();
406 for (int i = 0; i < childCount; i++) {
407 mKindSectionViews.getChildAt(i).setEnabled(enabled);
Walter Jangcab3dce2015-02-09 17:48:03 -0800408 }
409 }
410
Walter Jang79658e12015-09-24 10:36:26 -0700411 @Override
412 public Parcelable onSaveInstanceState() {
413 final Parcelable superState = super.onSaveInstanceState();
414 final SavedState savedState = new SavedState(superState);
415 savedState.mIsExpanded = mIsExpanded;
416 return savedState;
417 }
418
419 @Override
420 public void onRestoreInstanceState(Parcelable state) {
421 if(!(state instanceof SavedState)) {
422 super.onRestoreInstanceState(state);
423 return;
424 }
425 final SavedState savedState = (SavedState) state;
426 super.onRestoreInstanceState(savedState.getSuperState());
427 mIsExpanded = savedState.mIsExpanded;
Walter Jangd6753152015-10-02 09:23:13 -0700428 if (mIsExpanded) {
429 showAllFields();
Walter Jang79658e12015-09-24 10:36:26 -0700430 }
431 }
432
Walter Jang3efae4a2015-02-18 11:12:00 -0800433 /**
Walter Jang31a74ad2015-10-02 19:17:39 -0700434 * Pass through to {@link CompactPhotoEditorView#setListener}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800435 */
Walter Jang31a74ad2015-10-02 19:17:39 -0700436 public void setPhotoListener(CompactPhotoEditorView.Listener listener) {
437 mPhotoView.setListener(listener);
Walter Jang3efae4a2015-02-18 11:12:00 -0800438 }
439
Walter Jang31a74ad2015-10-02 19:17:39 -0700440 public void removePhoto() {
Walter Jang45b86d52015-10-15 15:23:16 -0700441 mPhotoValuesDelta.setFromTemplate(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700442 mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null);
443
444 mPhotoView.removePhoto();
Walter Jang3efae4a2015-02-18 11:12:00 -0800445 }
446
447 /**
Walter Jang41b3ea12015-03-09 17:30:06 -0700448 * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}.
449 */
450 public void setFullSizePhoto(Uri photoUri) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700451 mPhotoView.setFullSizedPhoto(photoUri);
Walter Jang41b3ea12015-03-09 17:30:06 -0700452 }
453
Walter Jang31a74ad2015-10-02 19:17:39 -0700454 public void updatePhoto(Uri photoUri) {
Walter Jang45b86d52015-10-15 15:23:16 -0700455 mPhotoValuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700456 // Unset primary for all photos
Wenyi Wang9086fb92015-11-16 09:58:20 -0800457 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700458 // Mark the currently displayed photo as primary
459 mPhotoValuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700460
Wenyi Wang4c9cf3a2015-10-15 17:58:23 -0700461 // Even though high-res photos cannot be saved by passing them via
462 // an EntityDeltaList (since they cause the Bundle size limit to be
463 // exceeded), we still pass a low-res thumbnail. This simplifies
464 // code all over the place, because we don't have to test whether
465 // there is a change in EITHER the delta-list OR a changed photo...
466 // this way, there is always a change in the delta-list.
467 try {
468 final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
469 getContext(), photoUri);
470 if (bytes != null) {
471 mPhotoValuesDelta.setPhoto(bytes);
472 }
473 } catch (FileNotFoundException e) {
474 elog("Failed to get bitmap from photo Uri");
475 }
476
Walter Jang31a74ad2015-10-02 19:17:39 -0700477 mPhotoView.setFullSizedPhoto(photoUri);
478 }
479
Wenyi Wang9086fb92015-11-16 09:58:20 -0800480 private void unsetSuperPrimaryFromAllPhotos() {
Walter Jang3f18d612015-10-07 16:01:05 -0700481 final List<KindSectionData> kindSectionDataList =
482 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
483 for (KindSectionData kindSectionData : kindSectionDataList) {
Wenyi Wang9086fb92015-11-16 09:58:20 -0800484 for (ValuesDelta valuesDelta : kindSectionData.getNonEmptyValuesDeltas()) {
485 valuesDelta.setSuperPrimary(false);
Walter Jang3f18d612015-10-07 16:01:05 -0700486 }
487 }
488 }
489
Walter Jang41b3ea12015-03-09 17:30:06 -0700490 /**
Walter Janga5e4bb22015-02-24 13:08:16 -0800491 * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800492 */
493 public boolean isWritablePhotoSet() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700494 return mPhotoView.isWritablePhotoSet();
Walter Jang3efae4a2015-02-18 11:12:00 -0800495 }
496
497 /**
Walter Jang3efae4a2015-02-18 11:12:00 -0800498 * Get the raw contact ID for the CompactHeaderView photo.
499 */
Walter Jang3efae4a2015-02-18 11:12:00 -0800500 public long getPhotoRawContactId() {
501 return mPhotoRawContactId;
502 }
503
Walter Jang4f5594a2015-10-06 18:40:31 -0700504 public StructuredNameEditorView getPrimaryNameEditorView() {
Walter Jange3945952015-10-27 12:44:54 -0700505 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
506 return primaryNameKindSectionView == null
507 ? null : primaryNameKindSectionView.getPrimaryNameEditorView();
Walter Jang4f5594a2015-10-06 18:40:31 -0700508 }
509
Walter Jang31a74ad2015-10-02 19:17:39 -0700510 /**
511 * Returns a data holder for every non-default/non-empty photo from each raw contact, whether
512 * the raw contact is writable or not.
513 */
514 public ArrayList<CompactPhotoSelectionFragment.Photo> getPhotos() {
515 final ArrayList<CompactPhotoSelectionFragment.Photo> photos = new ArrayList<>();
516
Walter Jang3f18d612015-10-07 16:01:05 -0700517 final Bundle updatedPhotos = mListener == null ? null : mListener.getUpdatedPhotos();
518
Walter Jang31a74ad2015-10-02 19:17:39 -0700519 final List<KindSectionData> kindSectionDataList =
520 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
521 for (int i = 0; i < kindSectionDataList.size(); i++) {
522 final KindSectionData kindSectionData = kindSectionDataList.get(i);
523 final AccountType accountType = kindSectionData.getAccountType();
Walter Jang228e02f2015-11-13 09:11:01 -0800524 final List<ValuesDelta> valuesDeltas = kindSectionData.getNonEmptyValuesDeltas();
525 if (valuesDeltas.isEmpty()) continue;
526 for (int j = 0; j < valuesDeltas.size(); j++) {
527 final ValuesDelta valuesDelta = valuesDeltas.get(j);
Walter Jang31a74ad2015-10-02 19:17:39 -0700528 final Bitmap bitmap = EditorUiUtils.getPhotoBitmap(valuesDelta);
529 if (bitmap == null) continue;
530
531 final CompactPhotoSelectionFragment.Photo photo =
532 new CompactPhotoSelectionFragment.Photo();
533 photo.titleRes = accountType.titleRes;
534 photo.iconRes = accountType.iconRes;
535 photo.syncAdapterPackageName = accountType.syncAdapterPackageName;
536 photo.valuesDelta = valuesDelta;
537 photo.primary = valuesDelta.isSuperPrimary();
538 photo.kindSectionDataListIndex = i;
539 photo.valuesDeltaListIndex = j;
Walter Jangda258ff2015-11-17 11:06:02 -0800540 photo.photoId = valuesDelta.getId();
Walter Jang3f18d612015-10-07 16:01:05 -0700541
542 if (updatedPhotos != null) {
543 photo.updatedPhotoUri = (Uri) updatedPhotos.get(String.valueOf(
544 kindSectionData.getRawContactDelta().getRawContactId()));
545 }
546
Wenyi Wangd375c352016-03-28 15:12:45 -0700547 final CharSequence accountTypeLabel = accountType.getDisplayLabel(getContext());
548 photo.accountType = accountTypeLabel == null ? "" : accountTypeLabel.toString();
549 final String accountName = kindSectionData.getRawContactDelta().getAccountName();
550 photo.accountName = accountName == null ? "" : accountName;
Wenyi Wangab360172015-12-15 10:35:58 -0800551
Walter Jang31a74ad2015-10-02 19:17:39 -0700552 photos.add(photo);
553 }
554 }
555
556 return photos;
557 }
558
559 /**
560 * Marks the raw contact photo given as primary for the aggregate contact and updates the
561 * UI.
562 */
563 public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700564 // Find the values delta to mark as primary
Walter Jang3f18d612015-10-07 16:01:05 -0700565 final KindSectionDataList kindSectionDataList =
566 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
Walter Jang31a74ad2015-10-02 19:17:39 -0700567 if (photo.kindSectionDataListIndex < 0
568 || photo.kindSectionDataListIndex >= kindSectionDataList.size()) {
569 wlog("Invalid kind section data list index");
570 return;
571 }
572 final KindSectionData kindSectionData =
573 kindSectionDataList.get(photo.kindSectionDataListIndex);
Walter Jang228e02f2015-11-13 09:11:01 -0800574 final List<ValuesDelta> valuesDeltaList = kindSectionData.getNonEmptyValuesDeltas();
Walter Jang31a74ad2015-10-02 19:17:39 -0700575 if (photo.valuesDeltaListIndex >= valuesDeltaList.size()) {
576 wlog("Invalid values delta list index");
577 return;
578 }
Walter Jangda258ff2015-11-17 11:06:02 -0800579
580 // Update values delta
Walter Jang31a74ad2015-10-02 19:17:39 -0700581 final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex);
582 valuesDelta.setFromTemplate(false);
Wenyi Wang9086fb92015-11-16 09:58:20 -0800583 unsetSuperPrimaryFromAllPhotos();
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700584 valuesDelta.setSuperPrimary(true);
Walter Jangda258ff2015-11-17 11:06:02 -0800585
Walter Jang31a74ad2015-10-02 19:17:39 -0700586 // Update the UI
587 mPhotoView.setPhoto(valuesDelta, mMaterialPalette);
588 }
589
Walter Jangd35e5ef2015-02-24 09:18:16 -0800590 public View getAggregationAnchorView() {
Walter Jangf10ca152015-09-22 15:23:55 -0700591 final List<CompactKindSectionView> kindSectionViews = getKindSectionViews(
Walter Jang192a01c2015-09-22 15:23:55 -0700592 StructuredName.CONTENT_ITEM_TYPE);
Walter Jangf10ca152015-09-22 15:23:55 -0700593 if (!kindSectionViews.isEmpty()) {
Walter Jang192a01c2015-09-22 15:23:55 -0700594 return mKindSectionViews.getChildAt(0).findViewById(R.id.anchor_view);
Walter Jangd35e5ef2015-02-24 09:18:16 -0800595 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700596 return null;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800597 }
598
Walter Jangf10ca152015-09-22 15:23:55 -0700599 public void setGroupMetaData(Cursor groupMetaData) {
600 final List<CompactKindSectionView> kindSectionViews = getKindSectionViews(
601 GroupMembership.CONTENT_ITEM_TYPE);
602 for (CompactKindSectionView kindSectionView : kindSectionViews) {
603 kindSectionView.setGroupMetaData(groupMetaData);
Walter Jangd6753152015-10-02 09:23:13 -0700604 if (mIsExpanded) {
605 kindSectionView.setHideWhenEmpty(false);
606 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
607 }
Walter Jangf10ca152015-09-22 15:23:55 -0700608 }
609 }
610
Walter Jangf46abd82015-02-20 16:52:04 -0800611 public void setState(RawContactDeltaList rawContactDeltas,
Walter Jang06f73a12015-06-17 11:15:48 -0700612 MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
Walter Jange3945952015-10-27 12:44:54 -0700613 long photoId, boolean hasNewContact, boolean isUserProfile,
614 AccountWithDataSet primaryAccount) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700615 mKindSectionDataMap.clear();
Walter Jangf5dfea42015-09-16 12:30:36 -0700616 mKindSectionViews.removeAllViews();
Walter Jang363d3fd2015-09-16 10:29:07 -0700617 mMoreFields.setVisibility(View.VISIBLE);
Walter Jangcab3dce2015-02-09 17:48:03 -0800618
Walter Jangf46abd82015-02-20 16:52:04 -0800619 mMaterialPalette = materialPalette;
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700620 mViewIdGenerator = viewIdGenerator;
Walter Jang708ea9e2015-09-10 15:42:05 -0700621 mPhotoId = photoId;
Tingting Wangf695eb32015-10-15 18:45:15 -0700622
Walter Jang708ea9e2015-09-10 15:42:05 -0700623 mHasNewContact = hasNewContact;
624 mIsUserProfile = isUserProfile;
625 mPrimaryAccount = primaryAccount;
626 if (mPrimaryAccount == null) {
627 mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount();
628 }
629 vlog("state: primary " + mPrimaryAccount);
Walter Jangcab3dce2015-02-09 17:48:03 -0800630
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700631 // Parse the given raw contact deltas
632 if (rawContactDeltas == null || rawContactDeltas.isEmpty()) {
633 elog("No raw contact deltas");
Walter Jang79658e12015-09-24 10:36:26 -0700634 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jangf5dfea42015-09-16 12:30:36 -0700635 return;
636 }
Walter Jangcbd431d2015-10-06 13:07:30 -0700637 parseRawContactDeltas(rawContactDeltas);
Walter Jang79658e12015-09-24 10:36:26 -0700638 if (mKindSectionDataMap.isEmpty()) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700639 elog("No kind section data parsed from RawContactDelta(s)");
Walter Jang79658e12015-09-24 10:36:26 -0700640 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700641 return;
642 }
Walter Jang82acd422015-10-17 14:01:27 -0700643
644 // Get the primary name kind section data
645 mPrimaryNameKindSectionData = mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE)
646 .getEntryToWrite(/* id =*/ -1, mPrimaryAccount, mIsUserProfile);
647 if (mPrimaryNameKindSectionData != null) {
648 // Ensure that a structured name and photo exists
649 final RawContactDelta rawContactDelta =
650 mPrimaryNameKindSectionData.first.getRawContactDelta();
651 RawContactModifier.ensureKindExists(
652 rawContactDelta,
653 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700654 StructuredName.CONTENT_ITEM_TYPE);
Walter Jang82acd422015-10-17 14:01:27 -0700655 RawContactModifier.ensureKindExists(
656 rawContactDelta,
657 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700658 Photo.CONTENT_ITEM_TYPE);
659 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700660
661 // Setup the view
Tingting Wang655ad1a2015-10-05 17:51:14 -0700662 addAccountInfo(rawContactDeltas);
Walter Jang363d3fd2015-09-16 10:29:07 -0700663 addPhotoView();
Walter Jangf5dfea42015-09-16 12:30:36 -0700664 addKindSectionViews();
Walter Jange3945952015-10-27 12:44:54 -0700665
Walter Jang4f5594a2015-10-06 18:40:31 -0700666 if (mIsExpanded) showAllFields();
Walter Jangd6753152015-10-02 09:23:13 -0700667
668 if (mListener != null) mListener.onEditorsBound();
Walter Jangcab3dce2015-02-09 17:48:03 -0800669 }
670
Walter Jangcbd431d2015-10-06 13:07:30 -0700671 private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700672 // Build the kind section data list map
Walter Jang192a01c2015-09-22 15:23:55 -0700673 vlog("parse: " + rawContactDeltas.size() + " rawContactDelta(s)");
674 for (int j = 0; j < rawContactDeltas.size(); j++) {
675 final RawContactDelta rawContactDelta = rawContactDeltas.get(j);
676 vlog("parse: " + j + " rawContactDelta" + rawContactDelta);
Walter Jang363d3fd2015-09-16 10:29:07 -0700677 if (rawContactDelta == null || !rawContactDelta.isVisible()) continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800678 final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
Walter Jang363d3fd2015-09-16 10:29:07 -0700679 if (accountType == null) continue;
680 final List<DataKind> dataKinds = accountType.getSortedDataKinds();
681 final int dataKindSize = dataKinds == null ? 0 : dataKinds.size();
682 vlog("parse: " + dataKindSize + " dataKinds(s)");
683 for (int i = 0; i < dataKindSize; i++) {
684 final DataKind dataKind = dataKinds.get(i);
Walter Jang192a01c2015-09-22 15:23:55 -0700685 if (dataKind == null || !dataKind.editable) {
686 vlog("parse: " + i + " " + dataKind.mimeType + " dropped read-only");
687 continue;
688 }
689 final String mimeType = dataKind.mimeType;
690
691 // Skip psuedo mime types
692 if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
693 || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
694 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
695 continue;
696 }
697
Walter Jang3f18d612015-10-07 16:01:05 -0700698 final KindSectionDataList kindSectionDataList =
Walter Jangcbd431d2015-10-06 13:07:30 -0700699 getOrCreateKindSectionDataList(mimeType);
Walter Jang363d3fd2015-09-16 10:29:07 -0700700 final KindSectionData kindSectionData =
701 new KindSectionData(accountType, dataKind, rawContactDelta);
702 kindSectionDataList.add(kindSectionData);
Walter Jang192a01c2015-09-22 15:23:55 -0700703
Walter Jang363d3fd2015-09-16 10:29:07 -0700704 vlog("parse: " + i + " " + dataKind.mimeType + " " +
Walter Jang228e02f2015-11-13 09:11:01 -0800705 kindSectionData.getValuesDeltas().size() + " value(s) " +
Tingting Wangd482e0c2015-11-16 10:13:29 -0800706 kindSectionData.getNonEmptyValuesDeltas().size() + " non-empty value(s) " +
Wenyi Wang42d740c2015-11-13 14:23:54 -0800707 kindSectionData.getVisibleValuesDeltas().size() +
Walter Jang228e02f2015-11-13 09:11:01 -0800708 " visible value(s)");
Walter Jangac679af2015-06-01 12:17:06 -0700709 }
710 }
Walter Jang3efae4a2015-02-18 11:12:00 -0800711 }
712
Walter Jang3f18d612015-10-07 16:01:05 -0700713 private KindSectionDataList getOrCreateKindSectionDataList(String mimeType) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700714 KindSectionDataList kindSectionDataList = mKindSectionDataMap.get(mimeType);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700715 if (kindSectionDataList == null) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700716 kindSectionDataList = new KindSectionDataList();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700717 mKindSectionDataMap.put(mimeType, kindSectionDataList);
718 }
719 return kindSectionDataList;
720 }
721
Tingting Wang655ad1a2015-10-05 17:51:14 -0700722 private void addAccountInfo(RawContactDeltaList rawContactDeltas) {
Walter Jang82acd422015-10-17 14:01:27 -0700723 mAccountHeaderContainer.setVisibility(View.GONE);
Walter Jang82acd422015-10-17 14:01:27 -0700724 mRawContactContainer.setVisibility(View.GONE);
725
726 if (mPrimaryNameKindSectionData == null) return;
727 final RawContactDelta rawContactDelta =
728 mPrimaryNameKindSectionData.first.getRawContactDelta();
Walter Jang708ea9e2015-09-10 15:42:05 -0700729
730 // Get the account information for the primary raw contact delta
Walter Jangbe5e1b12015-10-17 11:38:29 -0700731 final Pair<String,String> accountInfo = mIsUserProfile
732 ? EditorUiUtils.getLocalAccountInfo(getContext(),
Walter Jang82acd422015-10-17 14:01:27 -0700733 rawContactDelta.getAccountName(),
734 rawContactDelta.getAccountType(mAccountTypeManager))
Walter Jangbe5e1b12015-10-17 11:38:29 -0700735 : EditorUiUtils.getAccountInfo(getContext(),
Walter Jang82acd422015-10-17 14:01:27 -0700736 rawContactDelta.getAccountName(),
737 rawContactDelta.getAccountType(mAccountTypeManager));
Walter Jang708ea9e2015-09-10 15:42:05 -0700738
Walter Jang23709542015-10-22 12:50:58 -0700739 // Either the account header or selector should be shown, not both.
Walter Jang708ea9e2015-09-10 15:42:05 -0700740 final List<AccountWithDataSet> accounts =
741 AccountTypeManager.getInstance(getContext()).getAccounts(true);
Walter Jang82acd422015-10-17 14:01:27 -0700742 if (mHasNewContact && !mIsUserProfile) {
743 if (accounts.size() > 1) {
744 addAccountSelector(accountInfo, rawContactDelta);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700745 } else {
Tingting Wang1594c6f2015-10-26 17:39:16 -0700746 addAccountHeader(accountInfo);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700747 }
Tingting Wang1594c6f2015-10-26 17:39:16 -0700748 } else if (mIsUserProfile || !shouldHideAccountContainer(rawContactDeltas)) {
749 addAccountHeader(accountInfo);
Walter Jang708ea9e2015-09-10 15:42:05 -0700750 }
Walter Jang23709542015-10-22 12:50:58 -0700751
752 // The raw contact selector should only display linked raw contacts that can be edited in
753 // the full editor (i.e. they are not newly created raw contacts)
Tingting Wang2bb85d22015-10-23 13:01:09 -0700754 final RawContactAccountListAdapter adapter = new RawContactAccountListAdapter(getContext(),
755 getRawContactDeltaListForSelector(rawContactDeltas));
756 if (adapter.getCount() > 0) {
757 final String accountsSummary = getResources().getQuantityString(
758 R.plurals.compact_editor_linked_contacts_selector_title,
759 adapter.getCount(), adapter.getCount());
Walter Jang23709542015-10-22 12:50:58 -0700760 addRawContactAccountSelector(accountsSummary, adapter);
761 }
Walter Jang708ea9e2015-09-10 15:42:05 -0700762 }
763
Tingting Wang2bb85d22015-10-23 13:01:09 -0700764 private RawContactDeltaList getRawContactDeltaListForSelector(
765 RawContactDeltaList rawContactDeltas) {
766 // Sort raw contacts so google accounts come first
767 Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
768
769 final RawContactDeltaList result = new RawContactDeltaList();
770 for (RawContactDelta rawContactDelta : rawContactDeltas) {
771 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
772 // Only add raw contacts that can be opened in the editor
773 result.add(rawContactDelta);
774 }
775 }
776 // Don't return a list of size 1 that would just open the raw contact being edited
777 // in the compact editor in the full editor
778 if (result.size() == 1 && result.get(0).getRawContactAccountType(
779 getContext()).areContactsWritable()) {
780 result.clear();
781 return result;
782 }
783 return result;
784 }
785
Tingting Wang1594c6f2015-10-26 17:39:16 -0700786 // Returns true if there are multiple writable rawcontacts and no read-only ones,
787 // or there are both writable and read-only rawcontacts.
788 private boolean shouldHideAccountContainer(RawContactDeltaList rawContactDeltas) {
Tingting Wang2bb85d22015-10-23 13:01:09 -0700789 int writable = 0;
790 int readonly = 0;
791 for (RawContactDelta rawContactDelta : rawContactDeltas) {
792 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
793 if (rawContactDelta.getRawContactAccountType(getContext()).areContactsWritable()) {
794 writable++;
795 } else {
796 readonly++;
797 }
798 }
799 }
800 return (writable > 1 || (writable > 0 && readonly > 0));
801 }
802
Tingting Wang1594c6f2015-10-26 17:39:16 -0700803 private void addAccountHeader(Pair<String,String> accountInfo) {
Walter Jang82acd422015-10-17 14:01:27 -0700804 mAccountHeaderContainer.setVisibility(View.VISIBLE);
805
Walter Jangbe5e1b12015-10-17 11:38:29 -0700806 // Set the account name
807 final String accountName = TextUtils.isEmpty(accountInfo.first)
808 ? accountInfo.second : accountInfo.first;
809 mAccountHeaderName.setVisibility(View.VISIBLE);
810 mAccountHeaderName.setText(accountName);
811
812 // Set the account type
813 final String selectorTitle = getResources().getString(
814 R.string.compact_editor_account_selector_title);
815 mAccountHeaderType.setText(selectorTitle);
816
817 // Set the icon
Walter Jang82acd422015-10-17 14:01:27 -0700818 if (mPrimaryNameKindSectionData != null) {
819 final RawContactDelta rawContactDelta =
820 mPrimaryNameKindSectionData.first.getRawContactDelta();
821 if (rawContactDelta != null) {
822 final AccountType accountType =
823 rawContactDelta.getRawContactAccountType(getContext());
824 mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext()));
825 }
826 }
Walter Jangbe5e1b12015-10-17 11:38:29 -0700827
828 // Set the content description
829 mAccountHeaderContainer.setContentDescription(
830 EditorUiUtils.getAccountInfoContentDescription(accountName, selectorTitle));
831 }
832
Walter Jang82acd422015-10-17 14:01:27 -0700833 private void addAccountSelector(Pair<String,String> accountInfo,
834 final RawContactDelta rawContactDelta) {
Gary Mai46cb3102016-08-10 18:14:09 -0700835 // Show save to default account.
836 addAccountHeader(accountInfo);
837 // Add handlers for choosing another account to save to.
838 mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE);
839 mAccountHeaderContainer.setOnClickListener(new View.OnClickListener() {
Walter Jang708ea9e2015-09-10 15:42:05 -0700840 @Override
841 public void onClick(View v) {
842 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
843 final AccountsListAdapter adapter =
844 new AccountsListAdapter(getContext(),
845 AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE,
846 mPrimaryAccount);
Gary Mai46cb3102016-08-10 18:14:09 -0700847 popup.setWidth(mAccountHeaderContainer.getWidth());
848 popup.setAnchorView(mAccountHeaderContainer);
Walter Jang708ea9e2015-09-10 15:42:05 -0700849 popup.setAdapter(adapter);
850 popup.setModal(true);
851 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
852 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
853 @Override
854 public void onItemClick(AdapterView<?> parent, View view, int position,
855 long id) {
856 UiClosables.closeQuietly(popup);
857 final AccountWithDataSet newAccount = adapter.getItem(position);
858 if (mListener != null && !mPrimaryAccount.equals(newAccount)) {
859 mListener.onRebindEditorsForNewContact(
Walter Jang82acd422015-10-17 14:01:27 -0700860 rawContactDelta,
Walter Jang708ea9e2015-09-10 15:42:05 -0700861 mPrimaryAccount,
862 newAccount);
863 }
864 }
865 });
866 popup.show();
867 }
868 });
869 }
870
Walter Jang23709542015-10-22 12:50:58 -0700871 private void addRawContactAccountSelector(String accountsSummary,
872 final RawContactAccountListAdapter adapter) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700873 mRawContactContainer.setVisibility(View.VISIBLE);
874
Tingting Wang655ad1a2015-10-05 17:51:14 -0700875 mRawContactSummary.setText(accountsSummary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700876
877 mRawContactContainer.setOnClickListener(new View.OnClickListener() {
878 @Override
879 public void onClick(View v) {
880 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700881 popup.setWidth(mRawContactContainer.getWidth());
882 popup.setAnchorView(mRawContactContainer);
883 popup.setAdapter(adapter);
884 popup.setModal(true);
885 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
886 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
887 @Override
888 public void onItemClick(AdapterView<?> parent, View view, int position,
889 long id) {
890 UiClosables.closeQuietly(popup);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700891
892 if (mListener != null) {
Tingting Wang91cee282015-10-07 13:48:17 -0700893 final long rawContactId = adapter.getItemId(position);
894 final Uri rawContactUri = ContentUris.withAppendedId(
895 ContactsContract.RawContacts.CONTENT_URI, rawContactId);
896 final RawContactDelta rawContactDelta = adapter.getItem(position);
897 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
898 getContext());
899 final AccountType accountType = rawContactDelta.getAccountType(
900 accountTypes);
901 final boolean isReadOnly = !accountType.areContactsWritable();
902
903 mListener.onRawContactSelected(rawContactUri, rawContactId, isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700904 }
905 }
906 });
907 popup.show();
908 }
909 });
910 }
911
Walter Jang363d3fd2015-09-16 10:29:07 -0700912 private void addPhotoView() {
Walter Jang31a74ad2015-10-02 19:17:39 -0700913 // Get the kind section data and values delta that we will display in the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700914 final KindSectionDataList kindSectionDataList =
915 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
916 final Pair<KindSectionData,ValuesDelta> photoToDisplay =
917 kindSectionDataList.getEntryToDisplay(mPhotoId);
918 if (photoToDisplay == null) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700919 wlog("photo: no kind section data parsed");
Walter Jangcbd431d2015-10-06 13:07:30 -0700920 mPhotoView.setVisibility(View.GONE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700921 return;
922 }
Walter Jang06f73a12015-06-17 11:15:48 -0700923
Walter Jang31a74ad2015-10-02 19:17:39 -0700924 // Set the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700925 mPhotoView.setPhoto(photoToDisplay.second, mMaterialPalette);
Walter Jang31a74ad2015-10-02 19:17:39 -0700926
927 // Find the raw contact ID and values delta that will be written when the photo is edited
Walter Jangda258ff2015-11-17 11:06:02 -0800928 final Pair<KindSectionData, ValuesDelta> photoToWrite = kindSectionDataList.getEntryToWrite(
Walter Jang82acd422015-10-17 14:01:27 -0700929 mPhotoId, mPrimaryAccount, mIsUserProfile);
Walter Jangda258ff2015-11-17 11:06:02 -0800930 if (photoToWrite == null) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700931 mPhotoView.setReadOnly(true);
932 return;
933 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700934 mPhotoView.setReadOnly(false);
Walter Jangda258ff2015-11-17 11:06:02 -0800935 mPhotoRawContactId = photoToWrite.first.getRawContactDelta().getRawContactId();
936 mPhotoValuesDelta = photoToWrite.second;
Walter Jang31a74ad2015-10-02 19:17:39 -0700937 }
938
Walter Jangf5dfea42015-09-16 12:30:36 -0700939 private void addKindSectionViews() {
940 // Sort the kinds
Walter Jangcbd431d2015-10-06 13:07:30 -0700941 final TreeSet<Map.Entry<String,KindSectionDataList>> entries =
Walter Jang192a01c2015-09-22 15:23:55 -0700942 new TreeSet<>(KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR);
Walter Jangf5dfea42015-09-16 12:30:36 -0700943 entries.addAll(mKindSectionDataMap.entrySet());
Walter Jang3efae4a2015-02-18 11:12:00 -0800944
Walter Jangf5dfea42015-09-16 12:30:36 -0700945 vlog("kind: " + entries.size() + " kindSection(s)");
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700946 int i = -1;
Walter Jangcbd431d2015-10-06 13:07:30 -0700947 for (Map.Entry<String, KindSectionDataList> entry : entries) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700948 i++;
949
Walter Jangf5dfea42015-09-16 12:30:36 -0700950 final String mimeType = entry.getKey();
Walter Jang82acd422015-10-17 14:01:27 -0700951
Tingting Wangf695eb32015-10-15 18:45:15 -0700952 if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang82acd422015-10-17 14:01:27 -0700953 if (mPrimaryNameKindSectionData == null) {
954 vlog("kind: " + i + " " + mimeType + " dropped");
955 continue;
Tingting Wangf695eb32015-10-15 18:45:15 -0700956 }
Walter Jang82acd422015-10-17 14:01:27 -0700957 vlog("kind: " + i + " " + mimeType + " using first entry only");
958 final KindSectionDataList kindSectionDataList = new KindSectionDataList();
959 kindSectionDataList.add(mPrimaryNameKindSectionData.first);
Walter Jangf5dfea42015-09-16 12:30:36 -0700960 final CompactKindSectionView kindSectionView = inflateKindSectionView(
Walter Jang82acd422015-10-17 14:01:27 -0700961 mKindSectionViews, kindSectionDataList, mimeType,
962 mPrimaryNameKindSectionData.second);
Walter Jangf5dfea42015-09-16 12:30:36 -0700963 mKindSectionViews.addView(kindSectionView);
Walter Jangf10ca152015-09-22 15:23:55 -0700964
965 // Keep a pointer to all the KindSectionsViews for each mimeType
966 getKindSectionViews(mimeType).add(kindSectionView);
Walter Jang82acd422015-10-17 14:01:27 -0700967 } else {
968 final KindSectionDataList kindSectionDataList = entry.getValue();
969
970 // Ignore mime types that we've already handled
971 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
972 vlog("kind: " + i + " " + mimeType + " dropped");
973 continue;
974 }
975
976 // Don't show more than one group editor on the compact editor.
977 // Groups will still be editable for each raw contact individually on the full editor.
978 if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)
979 && kindSectionDataList.size() > 1) {
980 vlog("kind: " + i + " " + mimeType + " dropped");
981 continue;
982 }
983
984 if (kindSectionDataList != null && !kindSectionDataList.isEmpty()) {
985 vlog("kind: " + i + " " + mimeType + " " + kindSectionDataList.size() +
986 " kindSectionData(s)");
987
988 final CompactKindSectionView kindSectionView = inflateKindSectionView(
989 mKindSectionViews, kindSectionDataList, mimeType,
990 /* primaryValueDelta =*/ null);
991 mKindSectionViews.addView(kindSectionView);
992
993 // Keep a pointer to all the KindSectionsViews for each mimeType
994 getKindSectionViews(mimeType).add(kindSectionView);
995 }
Walter Jangcab3dce2015-02-09 17:48:03 -0800996 }
997 }
998 }
999
Walter Jangf10ca152015-09-22 15:23:55 -07001000 private List<CompactKindSectionView> getKindSectionViews(String mimeType) {
1001 List<CompactKindSectionView> kindSectionViews = mKindSectionViewsMap.get(mimeType);
1002 if (kindSectionViews == null) {
1003 kindSectionViews = new ArrayList<>();
1004 mKindSectionViewsMap.put(mimeType, kindSectionViews);
1005 }
1006 return kindSectionViews;
1007 }
1008
Walter Jangf5dfea42015-09-16 12:30:36 -07001009 private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup,
Walter Jang82acd422015-10-17 14:01:27 -07001010 KindSectionDataList kindSectionDataList, String mimeType,
1011 ValuesDelta primaryValuesDelta) {
Walter Jangf5dfea42015-09-16 12:30:36 -07001012 final CompactKindSectionView kindSectionView = (CompactKindSectionView)
1013 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
1014 /* attachToRoot =*/ false);
Walter Jang45b86d52015-10-15 15:23:16 -07001015 kindSectionView.setIsUserProfile(mIsUserProfile);
Walter Jangf5dfea42015-09-16 12:30:36 -07001016
Walter Jang192a01c2015-09-22 15:23:55 -07001017 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Walter Jangf5dfea42015-09-16 12:30:36 -07001018 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001019 // Phone numbers and email addresses are always displayed,
1020 // even if they are empty
Walter Jangf5dfea42015-09-16 12:30:36 -07001021 kindSectionView.setHideWhenEmpty(false);
1022 }
Walter Jangf5dfea42015-09-16 12:30:36 -07001023
Walter Jang192a01c2015-09-22 15:23:55 -07001024 // Since phone numbers and email addresses displayed even if they are empty,
1025 // they will be the only types you add new values to initially for new contacts
1026 kindSectionView.setShowOneEmptyEditor(true);
1027
Walter Jang82acd422015-10-17 14:01:27 -07001028 // Sort non-name editors so they wind up in the order we want
1029 if (!StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001030 Collections.sort(kindSectionDataList, new EditorComparator(getContext()));
Walter Jang3e5ae0d2015-09-20 12:43:37 -07001031 }
1032
Walter Jang82acd422015-10-17 14:01:27 -07001033 kindSectionView.setState(kindSectionDataList, mViewIdGenerator, mListener,
1034 primaryValuesDelta);
Walter Jangf5dfea42015-09-16 12:30:36 -07001035
1036 return kindSectionView;
Walter Jangcab3dce2015-02-09 17:48:03 -08001037 }
1038
Walter Jange3945952015-10-27 12:44:54 -07001039 void maybeSetReadOnlyDisplayNameAsPrimary(String readOnlyDisplayName) {
1040 if (TextUtils.isEmpty(readOnlyDisplayName)) return;
1041 final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView();
1042 if (primaryNameKindSectionView != null && primaryNameKindSectionView.isEmptyName()) {
1043 vlog("name: using read only display name as primary name");
1044 primaryNameKindSectionView.setName(readOnlyDisplayName);
1045 }
1046 }
1047
1048 private CompactKindSectionView getPrimaryNameKindSectionView() {
Walter Jang4f5594a2015-10-06 18:40:31 -07001049 final List<CompactKindSectionView> kindSectionViews
1050 = mKindSectionViewsMap.get(StructuredName.CONTENT_ITEM_TYPE);
Walter Jange3945952015-10-27 12:44:54 -07001051 return kindSectionViews == null || kindSectionViews.isEmpty()
1052 ? null : kindSectionViews.get(0);
Walter Jang4f5594a2015-10-06 18:40:31 -07001053 }
1054
Walter Jangd6753152015-10-02 09:23:13 -07001055 private void showAllFields() {
Walter Jang79658e12015-09-24 10:36:26 -07001056 // Stop hiding empty editors and allow the user to enter values for all kinds now
1057 for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
1058 final CompactKindSectionView kindSectionView =
1059 (CompactKindSectionView) mKindSectionViews.getChildAt(i);
1060 kindSectionView.setHideWhenEmpty(false);
1061 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
1062 }
1063 mIsExpanded = true;
Walter Jang79658e12015-09-24 10:36:26 -07001064
Walter Jangf5dfea42015-09-16 12:30:36 -07001065 // Hide the more fields button
1066 mMoreFields.setVisibility(View.GONE);
Walter Jangcab3dce2015-02-09 17:48:03 -08001067 }
1068
Walter Jangbf63a6d2015-05-05 09:14:35 -07001069 private static void vlog(String message) {
1070 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1071 Log.v(TAG, message);
Walter Jangcab3dce2015-02-09 17:48:03 -08001072 }
1073 }
Walter Jang363d3fd2015-09-16 10:29:07 -07001074
1075 private static void wlog(String message) {
1076 if (Log.isLoggable(TAG, Log.WARN)) {
1077 Log.w(TAG, message);
1078 }
1079 }
1080
1081 private static void elog(String message) {
1082 Log.e(TAG, message);
1083 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001084}