blob: 4aa7a94e5e0f5f04dbf74fe7092b2d0bbde5595e [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 Jang4f5594a2015-10-06 18:40:31 -0700327 private String mReadOnlyDisplayName;
Walter Jang708ea9e2015-09-10 15:42:05 -0700328 private boolean mHasNewContact;
329 private boolean mIsUserProfile;
330 private AccountWithDataSet mPrimaryAccount;
Walter Jangcbd431d2015-10-06 13:07:30 -0700331 private Map<String,KindSectionDataList> mKindSectionDataMap = new HashMap<>();
Walter Jangcab3dce2015-02-09 17:48:03 -0800332
Walter Jang708ea9e2015-09-10 15:42:05 -0700333 // Account header
334 private View mAccountHeaderContainer;
335 private TextView mAccountHeaderType;
336 private TextView mAccountHeaderName;
Tingting Wang91cee282015-10-07 13:48:17 -0700337 private ImageView mAccountHeaderIcon;
Walter Jang708ea9e2015-09-10 15:42:05 -0700338
339 // Account selector
340 private View mAccountSelectorContainer;
341 private View mAccountSelector;
342 private TextView mAccountSelectorType;
343 private TextView mAccountSelectorName;
344
Tingting Wang655ad1a2015-10-05 17:51:14 -0700345 // Raw contacts selector
346 private View mRawContactContainer;
347 private TextView mRawContactSummary;
Tingting Wang655ad1a2015-10-05 17:51:14 -0700348
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700349 private CompactPhotoEditorView mPhotoView;
Walter Jangf5dfea42015-09-16 12:30:36 -0700350 private ViewGroup mKindSectionViews;
Walter Jangf10ca152015-09-22 15:23:55 -0700351 private Map<String,List<CompactKindSectionView>> mKindSectionViewsMap = new HashMap<>();
Walter Jangb6ca2722015-02-20 11:10:25 -0800352 private View mMoreFields;
Walter Jangcab3dce2015-02-09 17:48:03 -0800353
Walter Jang79658e12015-09-24 10:36:26 -0700354 private boolean mIsExpanded;
Walter Jangcbd431d2015-10-06 13:07:30 -0700355
Walter Jang3efae4a2015-02-18 11:12:00 -0800356 private long mPhotoRawContactId;
Walter Jang31a74ad2015-10-02 19:17:39 -0700357 private ValuesDelta mPhotoValuesDelta;
Walter Jang82acd422015-10-17 14:01:27 -0700358
359 private Pair<KindSectionData, ValuesDelta> mPrimaryNameKindSectionData;
Walter Jang4f5594a2015-10-06 18:40:31 -0700360 private StructuredNameEditorView mPrimaryNameEditorView;
Walter Jang3efae4a2015-02-18 11:12:00 -0800361
Walter Jangcab3dce2015-02-09 17:48:03 -0800362 public CompactRawContactsEditorView(Context context) {
363 super(context);
364 }
365
366 public CompactRawContactsEditorView(Context context, AttributeSet attrs) {
367 super(context, attrs);
368 }
369
Walter Jangb6ca2722015-02-20 11:10:25 -0800370 /**
371 * Sets the receiver for {@link CompactRawContactsEditorView} callbacks.
372 */
373 public void setListener(Listener listener) {
374 mListener = listener;
375 }
376
Walter Jangcab3dce2015-02-09 17:48:03 -0800377 @Override
378 protected void onFinishInflate() {
379 super.onFinishInflate();
380
381 mAccountTypeManager = AccountTypeManager.getInstance(getContext());
382 mLayoutInflater = (LayoutInflater)
383 getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
384
Walter Jang708ea9e2015-09-10 15:42:05 -0700385 // Account header
386 mAccountHeaderContainer = findViewById(R.id.account_container);
387 mAccountHeaderType = (TextView) findViewById(R.id.account_type);
388 mAccountHeaderName = (TextView) findViewById(R.id.account_name);
Tingting Wang91cee282015-10-07 13:48:17 -0700389 mAccountHeaderIcon = (ImageView) findViewById(R.id.account_type_icon);
Walter Jang708ea9e2015-09-10 15:42:05 -0700390
391 // Account selector
392 mAccountSelectorContainer = findViewById(R.id.account_selector_container);
393 mAccountSelector = findViewById(R.id.account);
394 mAccountSelectorType = (TextView) findViewById(R.id.account_type_selector);
395 mAccountSelectorName = (TextView) findViewById(R.id.account_name_selector);
396
Tingting Wang655ad1a2015-10-05 17:51:14 -0700397 // Raw contacts selector
398 mRawContactContainer = findViewById(R.id.all_rawcontacts_accounts_container);
399 mRawContactSummary = (TextView) findViewById(R.id.rawcontacts_accounts_summary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700400
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700401 mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor);
Walter Jangf5dfea42015-09-16 12:30:36 -0700402 mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views);
Walter Jangb6ca2722015-02-20 11:10:25 -0800403 mMoreFields = findViewById(R.id.more_fields);
404 mMoreFields.setOnClickListener(this);
405 }
406
Walter Jangb6ca2722015-02-20 11:10:25 -0800407 @Override
408 public void onClick(View view) {
Walter Jangf5dfea42015-09-16 12:30:36 -0700409 if (view.getId() == R.id.more_fields) {
Walter Jangd6753152015-10-02 09:23:13 -0700410 showAllFields();
Walter Jangb6ca2722015-02-20 11:10:25 -0800411 }
Walter Jangcab3dce2015-02-09 17:48:03 -0800412 }
413
414 @Override
415 public void setEnabled(boolean enabled) {
416 super.setEnabled(enabled);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700417 final int childCount = mKindSectionViews.getChildCount();
418 for (int i = 0; i < childCount; i++) {
419 mKindSectionViews.getChildAt(i).setEnabled(enabled);
Walter Jangcab3dce2015-02-09 17:48:03 -0800420 }
421 }
422
Walter Jang79658e12015-09-24 10:36:26 -0700423 @Override
424 public Parcelable onSaveInstanceState() {
425 final Parcelable superState = super.onSaveInstanceState();
426 final SavedState savedState = new SavedState(superState);
427 savedState.mIsExpanded = mIsExpanded;
428 return savedState;
429 }
430
431 @Override
432 public void onRestoreInstanceState(Parcelable state) {
433 if(!(state instanceof SavedState)) {
434 super.onRestoreInstanceState(state);
435 return;
436 }
437 final SavedState savedState = (SavedState) state;
438 super.onRestoreInstanceState(savedState.getSuperState());
439 mIsExpanded = savedState.mIsExpanded;
Walter Jangd6753152015-10-02 09:23:13 -0700440 if (mIsExpanded) {
441 showAllFields();
Walter Jang79658e12015-09-24 10:36:26 -0700442 }
443 }
444
Walter Jang3efae4a2015-02-18 11:12:00 -0800445 /**
Walter Jang31a74ad2015-10-02 19:17:39 -0700446 * Pass through to {@link CompactPhotoEditorView#setListener}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800447 */
Walter Jang31a74ad2015-10-02 19:17:39 -0700448 public void setPhotoListener(CompactPhotoEditorView.Listener listener) {
449 mPhotoView.setListener(listener);
Walter Jang3efae4a2015-02-18 11:12:00 -0800450 }
451
Walter Jang31a74ad2015-10-02 19:17:39 -0700452 public void removePhoto() {
Walter Jang45b86d52015-10-15 15:23:16 -0700453 mPhotoValuesDelta.setFromTemplate(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700454 mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null);
455
456 mPhotoView.removePhoto();
Walter Jang3efae4a2015-02-18 11:12:00 -0800457 }
458
459 /**
Walter Jang41b3ea12015-03-09 17:30:06 -0700460 * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}.
461 */
462 public void setFullSizePhoto(Uri photoUri) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700463 mPhotoView.setFullSizedPhoto(photoUri);
Walter Jang41b3ea12015-03-09 17:30:06 -0700464 }
465
Walter Jang31a74ad2015-10-02 19:17:39 -0700466 public void updatePhoto(Uri photoUri) {
Walter Jang45b86d52015-10-15 15:23:16 -0700467 mPhotoValuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700468 // Unset primary for all photos
469 unsetSuperPrimary();
470 // Mark the currently displayed photo as primary
471 mPhotoValuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700472
Wenyi Wang4c9cf3a2015-10-15 17:58:23 -0700473 // Even though high-res photos cannot be saved by passing them via
474 // an EntityDeltaList (since they cause the Bundle size limit to be
475 // exceeded), we still pass a low-res thumbnail. This simplifies
476 // code all over the place, because we don't have to test whether
477 // there is a change in EITHER the delta-list OR a changed photo...
478 // this way, there is always a change in the delta-list.
479 try {
480 final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
481 getContext(), photoUri);
482 if (bytes != null) {
483 mPhotoValuesDelta.setPhoto(bytes);
484 }
485 } catch (FileNotFoundException e) {
486 elog("Failed to get bitmap from photo Uri");
487 }
488
Walter Jang31a74ad2015-10-02 19:17:39 -0700489 mPhotoView.setFullSizedPhoto(photoUri);
490 }
491
Walter Jang3f18d612015-10-07 16:01:05 -0700492 private void unsetSuperPrimary() {
493 final List<KindSectionData> kindSectionDataList =
494 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
495 for (KindSectionData kindSectionData : kindSectionDataList) {
496 final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas();
497 for (ValuesDelta valuesDelta : valuesDeltaList) {
498 valuesDelta.setSuperPrimary(false);
499 }
500 }
501 }
502
Walter Jang41b3ea12015-03-09 17:30:06 -0700503 /**
Walter Janga5e4bb22015-02-24 13:08:16 -0800504 * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800505 */
506 public boolean isWritablePhotoSet() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700507 return mPhotoView.isWritablePhotoSet();
Walter Jang3efae4a2015-02-18 11:12:00 -0800508 }
509
510 /**
Walter Jang3efae4a2015-02-18 11:12:00 -0800511 * Get the raw contact ID for the CompactHeaderView photo.
512 */
Walter Jang3efae4a2015-02-18 11:12:00 -0800513 public long getPhotoRawContactId() {
514 return mPhotoRawContactId;
515 }
516
Walter Jang4f5594a2015-10-06 18:40:31 -0700517 public StructuredNameEditorView getPrimaryNameEditorView() {
518 return mPrimaryNameEditorView;
519 }
520
Walter Jang31a74ad2015-10-02 19:17:39 -0700521 /**
522 * Returns a data holder for every non-default/non-empty photo from each raw contact, whether
523 * the raw contact is writable or not.
524 */
525 public ArrayList<CompactPhotoSelectionFragment.Photo> getPhotos() {
526 final ArrayList<CompactPhotoSelectionFragment.Photo> photos = new ArrayList<>();
527
Walter Jang3f18d612015-10-07 16:01:05 -0700528 final Bundle updatedPhotos = mListener == null ? null : mListener.getUpdatedPhotos();
529
Walter Jang31a74ad2015-10-02 19:17:39 -0700530 final List<KindSectionData> kindSectionDataList =
531 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
532 for (int i = 0; i < kindSectionDataList.size(); i++) {
533 final KindSectionData kindSectionData = kindSectionDataList.get(i);
534 final AccountType accountType = kindSectionData.getAccountType();
535 final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas();
536 if (valuesDeltaList == null || valuesDeltaList.isEmpty()) continue;
537 for (int j = 0; j < valuesDeltaList.size(); j++) {
538 final ValuesDelta valuesDelta = valuesDeltaList.get(j);
539 final Bitmap bitmap = EditorUiUtils.getPhotoBitmap(valuesDelta);
540 if (bitmap == null) continue;
541
542 final CompactPhotoSelectionFragment.Photo photo =
543 new CompactPhotoSelectionFragment.Photo();
544 photo.titleRes = accountType.titleRes;
545 photo.iconRes = accountType.iconRes;
546 photo.syncAdapterPackageName = accountType.syncAdapterPackageName;
547 photo.valuesDelta = valuesDelta;
548 photo.primary = valuesDelta.isSuperPrimary();
549 photo.kindSectionDataListIndex = i;
550 photo.valuesDeltaListIndex = j;
Walter Jang3f18d612015-10-07 16:01:05 -0700551
552 if (updatedPhotos != null) {
553 photo.updatedPhotoUri = (Uri) updatedPhotos.get(String.valueOf(
554 kindSectionData.getRawContactDelta().getRawContactId()));
555 }
556
Walter Jang31a74ad2015-10-02 19:17:39 -0700557 photos.add(photo);
558 }
559 }
560
561 return photos;
562 }
563
564 /**
565 * Marks the raw contact photo given as primary for the aggregate contact and updates the
566 * UI.
567 */
568 public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700569 // Find the values delta to mark as primary
Walter Jang3f18d612015-10-07 16:01:05 -0700570 final KindSectionDataList kindSectionDataList =
571 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
Walter Jang31a74ad2015-10-02 19:17:39 -0700572 if (photo.kindSectionDataListIndex < 0
573 || photo.kindSectionDataListIndex >= kindSectionDataList.size()) {
574 wlog("Invalid kind section data list index");
575 return;
576 }
577 final KindSectionData kindSectionData =
578 kindSectionDataList.get(photo.kindSectionDataListIndex);
579 final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas();
580 if (photo.valuesDeltaListIndex >= valuesDeltaList.size()) {
581 wlog("Invalid values delta list index");
582 return;
583 }
584 final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex);
585 valuesDelta.setFromTemplate(false);
Wenyi Wangb8ab8612015-10-27 11:18:49 -0700586 // Unset primary for all photos
587 unsetSuperPrimary();
588 // Mark the currently displayed photo as primary
589 valuesDelta.setSuperPrimary(true);
Walter Jang31a74ad2015-10-02 19:17:39 -0700590 // Update the UI
591 mPhotoView.setPhoto(valuesDelta, mMaterialPalette);
592 }
593
Walter Jangd35e5ef2015-02-24 09:18:16 -0800594 public View getAggregationAnchorView() {
Walter Jangf10ca152015-09-22 15:23:55 -0700595 final List<CompactKindSectionView> kindSectionViews = getKindSectionViews(
Walter Jang192a01c2015-09-22 15:23:55 -0700596 StructuredName.CONTENT_ITEM_TYPE);
Walter Jangf10ca152015-09-22 15:23:55 -0700597 if (!kindSectionViews.isEmpty()) {
Walter Jang192a01c2015-09-22 15:23:55 -0700598 return mKindSectionViews.getChildAt(0).findViewById(R.id.anchor_view);
Walter Jangd35e5ef2015-02-24 09:18:16 -0800599 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700600 return null;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800601 }
602
Walter Jangf10ca152015-09-22 15:23:55 -0700603 public void setGroupMetaData(Cursor groupMetaData) {
604 final List<CompactKindSectionView> kindSectionViews = getKindSectionViews(
605 GroupMembership.CONTENT_ITEM_TYPE);
606 for (CompactKindSectionView kindSectionView : kindSectionViews) {
607 kindSectionView.setGroupMetaData(groupMetaData);
Walter Jangd6753152015-10-02 09:23:13 -0700608 if (mIsExpanded) {
609 kindSectionView.setHideWhenEmpty(false);
610 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
611 }
Walter Jangf10ca152015-09-22 15:23:55 -0700612 }
613 }
614
Walter Jangf46abd82015-02-20 16:52:04 -0800615 public void setState(RawContactDeltaList rawContactDeltas,
Walter Jang06f73a12015-06-17 11:15:48 -0700616 MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
Walter Jang4f5594a2015-10-06 18:40:31 -0700617 long photoId, String readOnlyDisplayName, boolean hasNewContact,
618 boolean isUserProfile, AccountWithDataSet primaryAccount) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700619 mKindSectionDataMap.clear();
Walter Jangf5dfea42015-09-16 12:30:36 -0700620 mKindSectionViews.removeAllViews();
Walter Jang363d3fd2015-09-16 10:29:07 -0700621 mMoreFields.setVisibility(View.VISIBLE);
Walter Jangcab3dce2015-02-09 17:48:03 -0800622
Walter Jangf46abd82015-02-20 16:52:04 -0800623 mMaterialPalette = materialPalette;
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700624 mViewIdGenerator = viewIdGenerator;
Walter Jang708ea9e2015-09-10 15:42:05 -0700625 mPhotoId = photoId;
Walter Jang4f5594a2015-10-06 18:40:31 -0700626 mReadOnlyDisplayName = readOnlyDisplayName;
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 Jang4f5594a2015-10-06 18:40:31 -0700670 if (mHasNewContact) {
671 maybeCopyPrimaryDisplayName();
Walter Jangd6753152015-10-02 09:23:13 -0700672 }
Walter Jang4f5594a2015-10-06 18:40:31 -0700673 if (mIsExpanded) showAllFields();
Walter Jangd6753152015-10-02 09:23:13 -0700674
675 if (mListener != null) mListener.onEditorsBound();
Walter Jangcab3dce2015-02-09 17:48:03 -0800676 }
677
Walter Jangcbd431d2015-10-06 13:07:30 -0700678 private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700679 // Build the kind section data list map
Walter Jang192a01c2015-09-22 15:23:55 -0700680 vlog("parse: " + rawContactDeltas.size() + " rawContactDelta(s)");
681 for (int j = 0; j < rawContactDeltas.size(); j++) {
682 final RawContactDelta rawContactDelta = rawContactDeltas.get(j);
683 vlog("parse: " + j + " rawContactDelta" + rawContactDelta);
Walter Jang363d3fd2015-09-16 10:29:07 -0700684 if (rawContactDelta == null || !rawContactDelta.isVisible()) continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800685 final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
Walter Jang363d3fd2015-09-16 10:29:07 -0700686 if (accountType == null) continue;
687 final List<DataKind> dataKinds = accountType.getSortedDataKinds();
688 final int dataKindSize = dataKinds == null ? 0 : dataKinds.size();
689 vlog("parse: " + dataKindSize + " dataKinds(s)");
690 for (int i = 0; i < dataKindSize; i++) {
691 final DataKind dataKind = dataKinds.get(i);
Walter Jang192a01c2015-09-22 15:23:55 -0700692 if (dataKind == null || !dataKind.editable) {
693 vlog("parse: " + i + " " + dataKind.mimeType + " dropped read-only");
694 continue;
695 }
696 final String mimeType = dataKind.mimeType;
697
698 // Skip psuedo mime types
699 if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
700 || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
701 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
702 continue;
703 }
704
Walter Jang3f18d612015-10-07 16:01:05 -0700705 final KindSectionDataList kindSectionDataList =
Walter Jangcbd431d2015-10-06 13:07:30 -0700706 getOrCreateKindSectionDataList(mimeType);
Walter Jang363d3fd2015-09-16 10:29:07 -0700707 final KindSectionData kindSectionData =
708 new KindSectionData(accountType, dataKind, rawContactDelta);
709 kindSectionDataList.add(kindSectionData);
Walter Jang192a01c2015-09-22 15:23:55 -0700710
Walter Jang363d3fd2015-09-16 10:29:07 -0700711 vlog("parse: " + i + " " + dataKind.mimeType + " " +
Walter Jang192a01c2015-09-22 15:23:55 -0700712 kindSectionData.getValuesDeltas().size() + " value(s)");
Walter Jangac679af2015-06-01 12:17:06 -0700713 }
714 }
Walter Jang3efae4a2015-02-18 11:12:00 -0800715 }
716
Walter Jang3f18d612015-10-07 16:01:05 -0700717 private KindSectionDataList getOrCreateKindSectionDataList(String mimeType) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700718 KindSectionDataList kindSectionDataList = mKindSectionDataMap.get(mimeType);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700719 if (kindSectionDataList == null) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700720 kindSectionDataList = new KindSectionDataList();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700721 mKindSectionDataMap.put(mimeType, kindSectionDataList);
722 }
723 return kindSectionDataList;
724 }
725
Tingting Wang655ad1a2015-10-05 17:51:14 -0700726 private void addAccountInfo(RawContactDeltaList rawContactDeltas) {
Walter Jang82acd422015-10-17 14:01:27 -0700727 mAccountHeaderContainer.setVisibility(View.GONE);
728 mAccountSelectorContainer.setVisibility(View.GONE);
729 mRawContactContainer.setVisibility(View.GONE);
730
731 if (mPrimaryNameKindSectionData == null) return;
732 final RawContactDelta rawContactDelta =
733 mPrimaryNameKindSectionData.first.getRawContactDelta();
Walter Jang708ea9e2015-09-10 15:42:05 -0700734
735 // Get the account information for the primary raw contact delta
Walter Jangbe5e1b12015-10-17 11:38:29 -0700736 final Pair<String,String> accountInfo = mIsUserProfile
737 ? EditorUiUtils.getLocalAccountInfo(getContext(),
Walter Jang82acd422015-10-17 14:01:27 -0700738 rawContactDelta.getAccountName(),
739 rawContactDelta.getAccountType(mAccountTypeManager))
Walter Jangbe5e1b12015-10-17 11:38:29 -0700740 : EditorUiUtils.getAccountInfo(getContext(),
Walter Jang82acd422015-10-17 14:01:27 -0700741 rawContactDelta.getAccountName(),
742 rawContactDelta.getAccountType(mAccountTypeManager));
Walter Jang708ea9e2015-09-10 15:42:05 -0700743
Walter Jang23709542015-10-22 12:50:58 -0700744 // Either the account header or selector should be shown, not both.
Walter Jang708ea9e2015-09-10 15:42:05 -0700745 final List<AccountWithDataSet> accounts =
746 AccountTypeManager.getInstance(getContext()).getAccounts(true);
Walter Jang82acd422015-10-17 14:01:27 -0700747 if (mHasNewContact && !mIsUserProfile) {
748 if (accounts.size() > 1) {
749 addAccountSelector(accountInfo, rawContactDelta);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700750 } else {
Tingting Wang2bb85d22015-10-23 13:01:09 -0700751 addAccountHeader(accountInfo, rawContactDeltas);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700752 }
Walter Jang82acd422015-10-17 14:01:27 -0700753 } else {
Tingting Wang2bb85d22015-10-23 13:01:09 -0700754 addAccountHeader(accountInfo, rawContactDeltas);
Walter Jang708ea9e2015-09-10 15:42:05 -0700755 }
Walter Jang23709542015-10-22 12:50:58 -0700756
757 // The raw contact selector should only display linked raw contacts that can be edited in
758 // the full editor (i.e. they are not newly created raw contacts)
Tingting Wang2bb85d22015-10-23 13:01:09 -0700759 final RawContactAccountListAdapter adapter = new RawContactAccountListAdapter(getContext(),
760 getRawContactDeltaListForSelector(rawContactDeltas));
761 if (adapter.getCount() > 0) {
762 final String accountsSummary = getResources().getQuantityString(
763 R.plurals.compact_editor_linked_contacts_selector_title,
764 adapter.getCount(), adapter.getCount());
Walter Jang23709542015-10-22 12:50:58 -0700765 addRawContactAccountSelector(accountsSummary, adapter);
766 }
Walter Jang708ea9e2015-09-10 15:42:05 -0700767 }
768
Tingting Wang2bb85d22015-10-23 13:01:09 -0700769 private RawContactDeltaList getRawContactDeltaListForSelector(
770 RawContactDeltaList rawContactDeltas) {
771 // Sort raw contacts so google accounts come first
772 Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
773
774 final RawContactDeltaList result = new RawContactDeltaList();
775 for (RawContactDelta rawContactDelta : rawContactDeltas) {
776 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
777 // Only add raw contacts that can be opened in the editor
778 result.add(rawContactDelta);
779 }
780 }
781 // Don't return a list of size 1 that would just open the raw contact being edited
782 // in the compact editor in the full editor
783 if (result.size() == 1 && result.get(0).getRawContactAccountType(
784 getContext()).areContactsWritable()) {
785 result.clear();
786 return result;
787 }
788 return result;
789 }
790
791 // Returns true if there're multiple writable and no read only, or there're both writable and
792 // read only. For ME profile, return false if there's a read only contact and unsaved local one.
793 private boolean shouldHideAccountHeader(RawContactDeltaList rawContactDeltas) {
794 int writable = 0;
795 int readonly = 0;
796 for (RawContactDelta rawContactDelta : rawContactDeltas) {
797 if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
798 if (rawContactDelta.getRawContactAccountType(getContext()).areContactsWritable()) {
799 writable++;
800 } else {
801 readonly++;
802 }
803 }
804 }
805 return (writable > 1 || (writable > 0 && readonly > 0));
806 }
807
808 private void addAccountHeader(Pair<String,String> accountInfo,
809 RawContactDeltaList rawContactDeltas) {
Walter Jang82acd422015-10-17 14:01:27 -0700810 mAccountHeaderContainer.setVisibility(View.VISIBLE);
Tingting Wang2bb85d22015-10-23 13:01:09 -0700811 if (shouldHideAccountHeader(rawContactDeltas)) {
812 mAccountHeaderContainer.setVisibility(View.GONE);
813 }
Walter Jang82acd422015-10-17 14:01:27 -0700814
Walter Jangbe5e1b12015-10-17 11:38:29 -0700815 // Set the account name
816 final String accountName = TextUtils.isEmpty(accountInfo.first)
817 ? accountInfo.second : accountInfo.first;
818 mAccountHeaderName.setVisibility(View.VISIBLE);
819 mAccountHeaderName.setText(accountName);
820
821 // Set the account type
822 final String selectorTitle = getResources().getString(
823 R.string.compact_editor_account_selector_title);
824 mAccountHeaderType.setText(selectorTitle);
825
826 // Set the icon
Walter Jang82acd422015-10-17 14:01:27 -0700827 if (mPrimaryNameKindSectionData != null) {
828 final RawContactDelta rawContactDelta =
829 mPrimaryNameKindSectionData.first.getRawContactDelta();
830 if (rawContactDelta != null) {
831 final AccountType accountType =
832 rawContactDelta.getRawContactAccountType(getContext());
833 mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext()));
834 }
835 }
Walter Jangbe5e1b12015-10-17 11:38:29 -0700836
837 // Set the content description
838 mAccountHeaderContainer.setContentDescription(
839 EditorUiUtils.getAccountInfoContentDescription(accountName, selectorTitle));
840 }
841
Walter Jang82acd422015-10-17 14:01:27 -0700842 private void addAccountSelector(Pair<String,String> accountInfo,
843 final RawContactDelta rawContactDelta) {
Walter Jang708ea9e2015-09-10 15:42:05 -0700844 mAccountSelectorContainer.setVisibility(View.VISIBLE);
845
Walter Jang03cea2e2015-09-18 17:04:21 -0700846 if (TextUtils.isEmpty(accountInfo.first)) {
Walter Jang708ea9e2015-09-10 15:42:05 -0700847 // Hide this view so the other text view will be centered vertically
848 mAccountSelectorName.setVisibility(View.GONE);
849 } else {
850 mAccountSelectorName.setVisibility(View.VISIBLE);
851 mAccountSelectorName.setText(accountInfo.first);
852 }
Walter Jang9488a762015-10-16 13:42:48 -0700853
854 final String selectorTitle = getResources().getString(
855 R.string.compact_editor_account_selector_title);
856 mAccountSelectorType.setText(selectorTitle);
Walter Jang708ea9e2015-09-10 15:42:05 -0700857
858 mAccountSelectorContainer.setContentDescription(
859 EditorUiUtils.getAccountInfoContentDescription(
Walter Jang9488a762015-10-16 13:42:48 -0700860 accountInfo.first, selectorTitle));
Walter Jang708ea9e2015-09-10 15:42:05 -0700861
862 mAccountSelector.setOnClickListener(new View.OnClickListener() {
863 @Override
864 public void onClick(View v) {
865 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
866 final AccountsListAdapter adapter =
867 new AccountsListAdapter(getContext(),
868 AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE,
869 mPrimaryAccount);
870 popup.setWidth(mAccountSelectorContainer.getWidth());
871 popup.setAnchorView(mAccountSelectorContainer);
872 popup.setAdapter(adapter);
873 popup.setModal(true);
874 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
875 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
876 @Override
877 public void onItemClick(AdapterView<?> parent, View view, int position,
878 long id) {
879 UiClosables.closeQuietly(popup);
880 final AccountWithDataSet newAccount = adapter.getItem(position);
881 if (mListener != null && !mPrimaryAccount.equals(newAccount)) {
882 mListener.onRebindEditorsForNewContact(
Walter Jang82acd422015-10-17 14:01:27 -0700883 rawContactDelta,
Walter Jang708ea9e2015-09-10 15:42:05 -0700884 mPrimaryAccount,
885 newAccount);
886 }
887 }
888 });
889 popup.show();
890 }
891 });
892 }
893
Walter Jang23709542015-10-22 12:50:58 -0700894 private void addRawContactAccountSelector(String accountsSummary,
895 final RawContactAccountListAdapter adapter) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700896 mRawContactContainer.setVisibility(View.VISIBLE);
897
Tingting Wang655ad1a2015-10-05 17:51:14 -0700898 mRawContactSummary.setText(accountsSummary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700899
900 mRawContactContainer.setOnClickListener(new View.OnClickListener() {
901 @Override
902 public void onClick(View v) {
903 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700904 popup.setWidth(mRawContactContainer.getWidth());
905 popup.setAnchorView(mRawContactContainer);
906 popup.setAdapter(adapter);
907 popup.setModal(true);
908 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
909 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
910 @Override
911 public void onItemClick(AdapterView<?> parent, View view, int position,
912 long id) {
913 UiClosables.closeQuietly(popup);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700914
915 if (mListener != null) {
Tingting Wang91cee282015-10-07 13:48:17 -0700916 final long rawContactId = adapter.getItemId(position);
917 final Uri rawContactUri = ContentUris.withAppendedId(
918 ContactsContract.RawContacts.CONTENT_URI, rawContactId);
919 final RawContactDelta rawContactDelta = adapter.getItem(position);
920 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
921 getContext());
922 final AccountType accountType = rawContactDelta.getAccountType(
923 accountTypes);
924 final boolean isReadOnly = !accountType.areContactsWritable();
925
926 mListener.onRawContactSelected(rawContactUri, rawContactId, isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700927 }
928 }
929 });
930 popup.show();
931 }
932 });
933 }
934
Walter Jang363d3fd2015-09-16 10:29:07 -0700935 private void addPhotoView() {
Walter Jang31a74ad2015-10-02 19:17:39 -0700936 // Get the kind section data and values delta that we will display in the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700937 final KindSectionDataList kindSectionDataList =
938 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
939 final Pair<KindSectionData,ValuesDelta> photoToDisplay =
940 kindSectionDataList.getEntryToDisplay(mPhotoId);
941 if (photoToDisplay == null) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700942 wlog("photo: no kind section data parsed");
Walter Jangcbd431d2015-10-06 13:07:30 -0700943 mPhotoView.setVisibility(View.GONE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700944 return;
945 }
Walter Jang06f73a12015-06-17 11:15:48 -0700946
Walter Jang31a74ad2015-10-02 19:17:39 -0700947 // Set the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700948 mPhotoView.setPhoto(photoToDisplay.second, mMaterialPalette);
Walter Jang31a74ad2015-10-02 19:17:39 -0700949
950 // Find the raw contact ID and values delta that will be written when the photo is edited
Walter Jang82acd422015-10-17 14:01:27 -0700951 final Pair<KindSectionData, ValuesDelta> pair = kindSectionDataList.getEntryToWrite(
952 mPhotoId, mPrimaryAccount, mIsUserProfile);
953 if (pair == null) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700954 mPhotoView.setReadOnly(true);
955 return;
956 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700957 mPhotoView.setReadOnly(false);
Walter Jang82acd422015-10-17 14:01:27 -0700958 mPhotoRawContactId = pair.first.getRawContactDelta().getRawContactId();
959 mPhotoValuesDelta = pair.second;
Walter Jang31a74ad2015-10-02 19:17:39 -0700960 }
961
Walter Jangf5dfea42015-09-16 12:30:36 -0700962 private void addKindSectionViews() {
963 // Sort the kinds
Walter Jangcbd431d2015-10-06 13:07:30 -0700964 final TreeSet<Map.Entry<String,KindSectionDataList>> entries =
Walter Jang192a01c2015-09-22 15:23:55 -0700965 new TreeSet<>(KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR);
Walter Jangf5dfea42015-09-16 12:30:36 -0700966 entries.addAll(mKindSectionDataMap.entrySet());
Walter Jang3efae4a2015-02-18 11:12:00 -0800967
Walter Jangf5dfea42015-09-16 12:30:36 -0700968 vlog("kind: " + entries.size() + " kindSection(s)");
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700969 int i = -1;
Walter Jangcbd431d2015-10-06 13:07:30 -0700970 for (Map.Entry<String, KindSectionDataList> entry : entries) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700971 i++;
972
Walter Jangf5dfea42015-09-16 12:30:36 -0700973 final String mimeType = entry.getKey();
Walter Jang82acd422015-10-17 14:01:27 -0700974
Tingting Wangf695eb32015-10-15 18:45:15 -0700975 if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang82acd422015-10-17 14:01:27 -0700976 if (mPrimaryNameKindSectionData == null) {
977 vlog("kind: " + i + " " + mimeType + " dropped");
978 continue;
Tingting Wangf695eb32015-10-15 18:45:15 -0700979 }
Walter Jang82acd422015-10-17 14:01:27 -0700980 vlog("kind: " + i + " " + mimeType + " using first entry only");
981 final KindSectionDataList kindSectionDataList = new KindSectionDataList();
982 kindSectionDataList.add(mPrimaryNameKindSectionData.first);
Walter Jangf5dfea42015-09-16 12:30:36 -0700983 final CompactKindSectionView kindSectionView = inflateKindSectionView(
Walter Jang82acd422015-10-17 14:01:27 -0700984 mKindSectionViews, kindSectionDataList, mimeType,
985 mPrimaryNameKindSectionData.second);
Walter Jangf5dfea42015-09-16 12:30:36 -0700986 mKindSectionViews.addView(kindSectionView);
Walter Jangf10ca152015-09-22 15:23:55 -0700987
988 // Keep a pointer to all the KindSectionsViews for each mimeType
989 getKindSectionViews(mimeType).add(kindSectionView);
Walter Jang82acd422015-10-17 14:01:27 -0700990 } else {
991 final KindSectionDataList kindSectionDataList = entry.getValue();
992
993 // Ignore mime types that we've already handled
994 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
995 vlog("kind: " + i + " " + mimeType + " dropped");
996 continue;
997 }
998
999 // Don't show more than one group editor on the compact editor.
1000 // Groups will still be editable for each raw contact individually on the full editor.
1001 if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)
1002 && kindSectionDataList.size() > 1) {
1003 vlog("kind: " + i + " " + mimeType + " dropped");
1004 continue;
1005 }
1006
1007 if (kindSectionDataList != null && !kindSectionDataList.isEmpty()) {
1008 vlog("kind: " + i + " " + mimeType + " " + kindSectionDataList.size() +
1009 " kindSectionData(s)");
1010
1011 final CompactKindSectionView kindSectionView = inflateKindSectionView(
1012 mKindSectionViews, kindSectionDataList, mimeType,
1013 /* primaryValueDelta =*/ null);
1014 mKindSectionViews.addView(kindSectionView);
1015
1016 // Keep a pointer to all the KindSectionsViews for each mimeType
1017 getKindSectionViews(mimeType).add(kindSectionView);
1018 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001019 }
1020 }
1021 }
1022
Walter Jangf10ca152015-09-22 15:23:55 -07001023 private List<CompactKindSectionView> getKindSectionViews(String mimeType) {
1024 List<CompactKindSectionView> kindSectionViews = mKindSectionViewsMap.get(mimeType);
1025 if (kindSectionViews == null) {
1026 kindSectionViews = new ArrayList<>();
1027 mKindSectionViewsMap.put(mimeType, kindSectionViews);
1028 }
1029 return kindSectionViews;
1030 }
1031
Walter Jangf5dfea42015-09-16 12:30:36 -07001032 private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup,
Walter Jang82acd422015-10-17 14:01:27 -07001033 KindSectionDataList kindSectionDataList, String mimeType,
1034 ValuesDelta primaryValuesDelta) {
Walter Jangf5dfea42015-09-16 12:30:36 -07001035 final CompactKindSectionView kindSectionView = (CompactKindSectionView)
1036 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
1037 /* attachToRoot =*/ false);
Walter Jang45b86d52015-10-15 15:23:16 -07001038 kindSectionView.setIsUserProfile(mIsUserProfile);
Walter Jangf5dfea42015-09-16 12:30:36 -07001039
Walter Jang192a01c2015-09-22 15:23:55 -07001040 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Walter Jangf5dfea42015-09-16 12:30:36 -07001041 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001042 // Phone numbers and email addresses are always displayed,
1043 // even if they are empty
Walter Jangf5dfea42015-09-16 12:30:36 -07001044 kindSectionView.setHideWhenEmpty(false);
1045 }
Walter Jangf5dfea42015-09-16 12:30:36 -07001046
Walter Jang192a01c2015-09-22 15:23:55 -07001047 // Since phone numbers and email addresses displayed even if they are empty,
1048 // they will be the only types you add new values to initially for new contacts
1049 kindSectionView.setShowOneEmptyEditor(true);
1050
Walter Jang82acd422015-10-17 14:01:27 -07001051 // Sort non-name editors so they wind up in the order we want
1052 if (!StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001053 Collections.sort(kindSectionDataList, new EditorComparator(getContext()));
Walter Jang3e5ae0d2015-09-20 12:43:37 -07001054 }
1055
Walter Jang82acd422015-10-17 14:01:27 -07001056 kindSectionView.setState(kindSectionDataList, mViewIdGenerator, mListener,
1057 primaryValuesDelta);
Walter Jangf5dfea42015-09-16 12:30:36 -07001058
1059 return kindSectionView;
Walter Jangcab3dce2015-02-09 17:48:03 -08001060 }
1061
Walter Jang4f5594a2015-10-06 18:40:31 -07001062 private void maybeCopyPrimaryDisplayName() {
1063 if (TextUtils.isEmpty(mReadOnlyDisplayName)) return;
1064 final List<CompactKindSectionView> kindSectionViews
1065 = mKindSectionViewsMap.get(StructuredName.CONTENT_ITEM_TYPE);
Tingting Wangac4b12f2015-10-21 11:30:33 -07001066 if (kindSectionViews == null || kindSectionViews.isEmpty()) return;
Walter Jang4f5594a2015-10-06 18:40:31 -07001067 final CompactKindSectionView primaryNameKindSectionView = kindSectionViews.get(0);
1068 if (primaryNameKindSectionView.isEmptyName()) {
1069 vlog("name: using read only display name as primary name");
1070 primaryNameKindSectionView.setName(mReadOnlyDisplayName);
1071 mPrimaryNameEditorView = primaryNameKindSectionView.getPrimaryNameEditorView();
1072 }
1073 }
1074
Walter Jangd6753152015-10-02 09:23:13 -07001075 private void showAllFields() {
Walter Jang79658e12015-09-24 10:36:26 -07001076 // Stop hiding empty editors and allow the user to enter values for all kinds now
1077 for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
1078 final CompactKindSectionView kindSectionView =
1079 (CompactKindSectionView) mKindSectionViews.getChildAt(i);
1080 kindSectionView.setHideWhenEmpty(false);
1081 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
1082 }
1083 mIsExpanded = true;
Walter Jang79658e12015-09-24 10:36:26 -07001084
Walter Jangf5dfea42015-09-16 12:30:36 -07001085 // Hide the more fields button
1086 mMoreFields.setVisibility(View.GONE);
Walter Jangcab3dce2015-02-09 17:48:03 -08001087 }
1088
Walter Jangbf63a6d2015-05-05 09:14:35 -07001089 private static void vlog(String message) {
1090 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1091 Log.v(TAG, message);
Walter Jangcab3dce2015-02-09 17:48:03 -08001092 }
1093 }
Walter Jang363d3fd2015-09-16 10:29:07 -07001094
1095 private static void wlog(String message) {
1096 if (Log.isLoggable(TAG, Log.WARN)) {
1097 Log.w(TAG, message);
1098 }
1099 }
1100
1101 private static void elog(String message) {
1102 Log.e(TAG, message);
1103 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001104}