blob: 8a4d479343db7d072b6c8d9508c1408176719141 [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);
Walter Jang3f18d612015-10-07 16:01:05 -0700468
Walter Jang45b86d52015-10-15 15:23:16 -0700469 if (!mIsUserProfile) {
470 // Unset primary for all photos
471 unsetSuperPrimary();
472
473 // Mark the currently displayed photo as primary
474 mPhotoValuesDelta.setSuperPrimary(true);
475 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700476
Wenyi Wang4c9cf3a2015-10-15 17:58:23 -0700477 // Even though high-res photos cannot be saved by passing them via
478 // an EntityDeltaList (since they cause the Bundle size limit to be
479 // exceeded), we still pass a low-res thumbnail. This simplifies
480 // code all over the place, because we don't have to test whether
481 // there is a change in EITHER the delta-list OR a changed photo...
482 // this way, there is always a change in the delta-list.
483 try {
484 final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes(
485 getContext(), photoUri);
486 if (bytes != null) {
487 mPhotoValuesDelta.setPhoto(bytes);
488 }
489 } catch (FileNotFoundException e) {
490 elog("Failed to get bitmap from photo Uri");
491 }
492
Walter Jang31a74ad2015-10-02 19:17:39 -0700493 mPhotoView.setFullSizedPhoto(photoUri);
494 }
495
Walter Jang3f18d612015-10-07 16:01:05 -0700496 private void unsetSuperPrimary() {
497 final List<KindSectionData> kindSectionDataList =
498 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
499 for (KindSectionData kindSectionData : kindSectionDataList) {
500 final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas();
501 for (ValuesDelta valuesDelta : valuesDeltaList) {
502 valuesDelta.setSuperPrimary(false);
503 }
504 }
505 }
506
Walter Jang41b3ea12015-03-09 17:30:06 -0700507 /**
Walter Janga5e4bb22015-02-24 13:08:16 -0800508 * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}.
Walter Jang3efae4a2015-02-18 11:12:00 -0800509 */
510 public boolean isWritablePhotoSet() {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700511 return mPhotoView.isWritablePhotoSet();
Walter Jang3efae4a2015-02-18 11:12:00 -0800512 }
513
514 /**
Walter Jang3efae4a2015-02-18 11:12:00 -0800515 * Get the raw contact ID for the CompactHeaderView photo.
516 */
Walter Jang3efae4a2015-02-18 11:12:00 -0800517 public long getPhotoRawContactId() {
518 return mPhotoRawContactId;
519 }
520
Walter Jang4f5594a2015-10-06 18:40:31 -0700521 public StructuredNameEditorView getPrimaryNameEditorView() {
522 return mPrimaryNameEditorView;
523 }
524
Walter Jang31a74ad2015-10-02 19:17:39 -0700525 /**
526 * Returns a data holder for every non-default/non-empty photo from each raw contact, whether
527 * the raw contact is writable or not.
528 */
529 public ArrayList<CompactPhotoSelectionFragment.Photo> getPhotos() {
530 final ArrayList<CompactPhotoSelectionFragment.Photo> photos = new ArrayList<>();
531
Walter Jang3f18d612015-10-07 16:01:05 -0700532 final Bundle updatedPhotos = mListener == null ? null : mListener.getUpdatedPhotos();
533
Walter Jang31a74ad2015-10-02 19:17:39 -0700534 final List<KindSectionData> kindSectionDataList =
535 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
536 for (int i = 0; i < kindSectionDataList.size(); i++) {
537 final KindSectionData kindSectionData = kindSectionDataList.get(i);
538 final AccountType accountType = kindSectionData.getAccountType();
539 final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas();
540 if (valuesDeltaList == null || valuesDeltaList.isEmpty()) continue;
541 for (int j = 0; j < valuesDeltaList.size(); j++) {
542 final ValuesDelta valuesDelta = valuesDeltaList.get(j);
543 final Bitmap bitmap = EditorUiUtils.getPhotoBitmap(valuesDelta);
544 if (bitmap == null) continue;
545
546 final CompactPhotoSelectionFragment.Photo photo =
547 new CompactPhotoSelectionFragment.Photo();
548 photo.titleRes = accountType.titleRes;
549 photo.iconRes = accountType.iconRes;
550 photo.syncAdapterPackageName = accountType.syncAdapterPackageName;
551 photo.valuesDelta = valuesDelta;
552 photo.primary = valuesDelta.isSuperPrimary();
553 photo.kindSectionDataListIndex = i;
554 photo.valuesDeltaListIndex = j;
Walter Jang3f18d612015-10-07 16:01:05 -0700555
556 if (updatedPhotos != null) {
557 photo.updatedPhotoUri = (Uri) updatedPhotos.get(String.valueOf(
558 kindSectionData.getRawContactDelta().getRawContactId()));
559 }
560
Walter Jang31a74ad2015-10-02 19:17:39 -0700561 photos.add(photo);
562 }
563 }
564
565 return photos;
566 }
567
568 /**
569 * Marks the raw contact photo given as primary for the aggregate contact and updates the
570 * UI.
571 */
572 public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700573 // Find the values delta to mark as primary
Walter Jang3f18d612015-10-07 16:01:05 -0700574 final KindSectionDataList kindSectionDataList =
575 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
Walter Jang31a74ad2015-10-02 19:17:39 -0700576 if (photo.kindSectionDataListIndex < 0
577 || photo.kindSectionDataListIndex >= kindSectionDataList.size()) {
578 wlog("Invalid kind section data list index");
579 return;
580 }
581 final KindSectionData kindSectionData =
582 kindSectionDataList.get(photo.kindSectionDataListIndex);
583 final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas();
584 if (photo.valuesDeltaListIndex >= valuesDeltaList.size()) {
585 wlog("Invalid values delta list index");
586 return;
587 }
588 final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex);
589 valuesDelta.setFromTemplate(false);
Walter Jang45b86d52015-10-15 15:23:16 -0700590
591 if (!mIsUserProfile) {
592 // Unset primary for all other photos
593 unsetSuperPrimary();
594
595 valuesDelta.setSuperPrimary(true);
596 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700597
598 // Update the UI
599 mPhotoView.setPhoto(valuesDelta, mMaterialPalette);
600 }
601
Walter Jangd35e5ef2015-02-24 09:18:16 -0800602 public View getAggregationAnchorView() {
Walter Jangf10ca152015-09-22 15:23:55 -0700603 final List<CompactKindSectionView> kindSectionViews = getKindSectionViews(
Walter Jang192a01c2015-09-22 15:23:55 -0700604 StructuredName.CONTENT_ITEM_TYPE);
Walter Jangf10ca152015-09-22 15:23:55 -0700605 if (!kindSectionViews.isEmpty()) {
Walter Jang192a01c2015-09-22 15:23:55 -0700606 return mKindSectionViews.getChildAt(0).findViewById(R.id.anchor_view);
Walter Jangd35e5ef2015-02-24 09:18:16 -0800607 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700608 return null;
Walter Jangd35e5ef2015-02-24 09:18:16 -0800609 }
610
Walter Jangf10ca152015-09-22 15:23:55 -0700611 public void setGroupMetaData(Cursor groupMetaData) {
612 final List<CompactKindSectionView> kindSectionViews = getKindSectionViews(
613 GroupMembership.CONTENT_ITEM_TYPE);
614 for (CompactKindSectionView kindSectionView : kindSectionViews) {
615 kindSectionView.setGroupMetaData(groupMetaData);
Walter Jangd6753152015-10-02 09:23:13 -0700616 if (mIsExpanded) {
617 kindSectionView.setHideWhenEmpty(false);
618 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
619 }
Walter Jangf10ca152015-09-22 15:23:55 -0700620 }
621 }
622
Walter Jangf46abd82015-02-20 16:52:04 -0800623 public void setState(RawContactDeltaList rawContactDeltas,
Walter Jang06f73a12015-06-17 11:15:48 -0700624 MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator,
Walter Jang4f5594a2015-10-06 18:40:31 -0700625 long photoId, String readOnlyDisplayName, boolean hasNewContact,
626 boolean isUserProfile, AccountWithDataSet primaryAccount) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700627 mKindSectionDataMap.clear();
Walter Jangf5dfea42015-09-16 12:30:36 -0700628 mKindSectionViews.removeAllViews();
Walter Jang363d3fd2015-09-16 10:29:07 -0700629 mMoreFields.setVisibility(View.VISIBLE);
Walter Jangcab3dce2015-02-09 17:48:03 -0800630
Walter Jangf46abd82015-02-20 16:52:04 -0800631 mMaterialPalette = materialPalette;
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700632 mViewIdGenerator = viewIdGenerator;
Walter Jang708ea9e2015-09-10 15:42:05 -0700633 mPhotoId = photoId;
Walter Jang4f5594a2015-10-06 18:40:31 -0700634 mReadOnlyDisplayName = readOnlyDisplayName;
Tingting Wangf695eb32015-10-15 18:45:15 -0700635
Walter Jang708ea9e2015-09-10 15:42:05 -0700636 mHasNewContact = hasNewContact;
637 mIsUserProfile = isUserProfile;
638 mPrimaryAccount = primaryAccount;
639 if (mPrimaryAccount == null) {
640 mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount();
641 }
642 vlog("state: primary " + mPrimaryAccount);
Walter Jangcab3dce2015-02-09 17:48:03 -0800643
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700644 // Parse the given raw contact deltas
645 if (rawContactDeltas == null || rawContactDeltas.isEmpty()) {
646 elog("No raw contact deltas");
Walter Jang79658e12015-09-24 10:36:26 -0700647 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jangf5dfea42015-09-16 12:30:36 -0700648 return;
649 }
Walter Jangcbd431d2015-10-06 13:07:30 -0700650 parseRawContactDeltas(rawContactDeltas);
Walter Jang79658e12015-09-24 10:36:26 -0700651 if (mKindSectionDataMap.isEmpty()) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700652 elog("No kind section data parsed from RawContactDelta(s)");
Walter Jang79658e12015-09-24 10:36:26 -0700653 if (mListener != null) mListener.onBindEditorsFailed();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700654 return;
655 }
Walter Jang82acd422015-10-17 14:01:27 -0700656
657 // Get the primary name kind section data
658 mPrimaryNameKindSectionData = mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE)
659 .getEntryToWrite(/* id =*/ -1, mPrimaryAccount, mIsUserProfile);
660 if (mPrimaryNameKindSectionData != null) {
661 // Ensure that a structured name and photo exists
662 final RawContactDelta rawContactDelta =
663 mPrimaryNameKindSectionData.first.getRawContactDelta();
664 RawContactModifier.ensureKindExists(
665 rawContactDelta,
666 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700667 StructuredName.CONTENT_ITEM_TYPE);
Walter Jang82acd422015-10-17 14:01:27 -0700668 RawContactModifier.ensureKindExists(
669 rawContactDelta,
670 rawContactDelta.getAccountType(mAccountTypeManager),
Walter Jangcbd431d2015-10-06 13:07:30 -0700671 Photo.CONTENT_ITEM_TYPE);
672 }
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700673
674 // Setup the view
Tingting Wang655ad1a2015-10-05 17:51:14 -0700675 addAccountInfo(rawContactDeltas);
Walter Jang363d3fd2015-09-16 10:29:07 -0700676 addPhotoView();
Walter Jangf5dfea42015-09-16 12:30:36 -0700677 addKindSectionViews();
Walter Jang4f5594a2015-10-06 18:40:31 -0700678 if (mHasNewContact) {
679 maybeCopyPrimaryDisplayName();
Walter Jangd6753152015-10-02 09:23:13 -0700680 }
Walter Jang4f5594a2015-10-06 18:40:31 -0700681 if (mIsExpanded) showAllFields();
Walter Jangd6753152015-10-02 09:23:13 -0700682
683 if (mListener != null) mListener.onEditorsBound();
Walter Jangcab3dce2015-02-09 17:48:03 -0800684 }
685
Walter Jangcbd431d2015-10-06 13:07:30 -0700686 private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas) {
Walter Jang363d3fd2015-09-16 10:29:07 -0700687 // Build the kind section data list map
Walter Jang192a01c2015-09-22 15:23:55 -0700688 vlog("parse: " + rawContactDeltas.size() + " rawContactDelta(s)");
689 for (int j = 0; j < rawContactDeltas.size(); j++) {
690 final RawContactDelta rawContactDelta = rawContactDeltas.get(j);
691 vlog("parse: " + j + " rawContactDelta" + rawContactDelta);
Walter Jang363d3fd2015-09-16 10:29:07 -0700692 if (rawContactDelta == null || !rawContactDelta.isVisible()) continue;
Walter Jangcab3dce2015-02-09 17:48:03 -0800693 final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
Walter Jang363d3fd2015-09-16 10:29:07 -0700694 if (accountType == null) continue;
695 final List<DataKind> dataKinds = accountType.getSortedDataKinds();
696 final int dataKindSize = dataKinds == null ? 0 : dataKinds.size();
697 vlog("parse: " + dataKindSize + " dataKinds(s)");
698 for (int i = 0; i < dataKindSize; i++) {
699 final DataKind dataKind = dataKinds.get(i);
Walter Jang192a01c2015-09-22 15:23:55 -0700700 if (dataKind == null || !dataKind.editable) {
701 vlog("parse: " + i + " " + dataKind.mimeType + " dropped read-only");
702 continue;
703 }
704 final String mimeType = dataKind.mimeType;
705
706 // Skip psuedo mime types
707 if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType)
708 || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
709 vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type");
710 continue;
711 }
712
Walter Jang3f18d612015-10-07 16:01:05 -0700713 final KindSectionDataList kindSectionDataList =
Walter Jangcbd431d2015-10-06 13:07:30 -0700714 getOrCreateKindSectionDataList(mimeType);
Walter Jang363d3fd2015-09-16 10:29:07 -0700715 final KindSectionData kindSectionData =
716 new KindSectionData(accountType, dataKind, rawContactDelta);
717 kindSectionDataList.add(kindSectionData);
Walter Jang192a01c2015-09-22 15:23:55 -0700718
Walter Jang363d3fd2015-09-16 10:29:07 -0700719 vlog("parse: " + i + " " + dataKind.mimeType + " " +
Walter Jang192a01c2015-09-22 15:23:55 -0700720 kindSectionData.getValuesDeltas().size() + " value(s)");
Walter Jangac679af2015-06-01 12:17:06 -0700721 }
722 }
Walter Jang3efae4a2015-02-18 11:12:00 -0800723 }
724
Walter Jang3f18d612015-10-07 16:01:05 -0700725 private KindSectionDataList getOrCreateKindSectionDataList(String mimeType) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700726 KindSectionDataList kindSectionDataList = mKindSectionDataMap.get(mimeType);
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700727 if (kindSectionDataList == null) {
Walter Jangcbd431d2015-10-06 13:07:30 -0700728 kindSectionDataList = new KindSectionDataList();
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700729 mKindSectionDataMap.put(mimeType, kindSectionDataList);
730 }
731 return kindSectionDataList;
732 }
733
Tingting Wang655ad1a2015-10-05 17:51:14 -0700734 private void addAccountInfo(RawContactDeltaList rawContactDeltas) {
Walter Jang82acd422015-10-17 14:01:27 -0700735 mAccountHeaderContainer.setVisibility(View.GONE);
736 mAccountSelectorContainer.setVisibility(View.GONE);
737 mRawContactContainer.setVisibility(View.GONE);
738
739 if (mPrimaryNameKindSectionData == null) return;
740 final RawContactDelta rawContactDelta =
741 mPrimaryNameKindSectionData.first.getRawContactDelta();
Walter Jang708ea9e2015-09-10 15:42:05 -0700742
743 // Get the account information for the primary raw contact delta
Walter Jangbe5e1b12015-10-17 11:38:29 -0700744 final Pair<String,String> accountInfo = mIsUserProfile
745 ? EditorUiUtils.getLocalAccountInfo(getContext(),
Walter Jang82acd422015-10-17 14:01:27 -0700746 rawContactDelta.getAccountName(),
747 rawContactDelta.getAccountType(mAccountTypeManager))
Walter Jangbe5e1b12015-10-17 11:38:29 -0700748 : EditorUiUtils.getAccountInfo(getContext(),
Walter Jang82acd422015-10-17 14:01:27 -0700749 rawContactDelta.getAccountName(),
750 rawContactDelta.getAccountType(mAccountTypeManager));
Walter Jang708ea9e2015-09-10 15:42:05 -0700751
Walter Jang23709542015-10-22 12:50:58 -0700752 // Either the account header or selector should be shown, not both.
Walter Jang708ea9e2015-09-10 15:42:05 -0700753 final List<AccountWithDataSet> accounts =
754 AccountTypeManager.getInstance(getContext()).getAccounts(true);
Walter Jang82acd422015-10-17 14:01:27 -0700755 if (mHasNewContact && !mIsUserProfile) {
756 if (accounts.size() > 1) {
757 addAccountSelector(accountInfo, rawContactDelta);
Walter Jangbe5e1b12015-10-17 11:38:29 -0700758 } else {
759 addAccountHeader(accountInfo);
760 }
Walter Jang82acd422015-10-17 14:01:27 -0700761 } else {
762 addAccountHeader(accountInfo);
Walter Jang708ea9e2015-09-10 15:42:05 -0700763 }
Walter Jang23709542015-10-22 12:50:58 -0700764
765 // The raw contact selector should only display linked raw contacts that can be edited in
766 // the full editor (i.e. they are not newly created raw contacts)
767 Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
768 final RawContactAccountListAdapter adapter =
769 new RawContactAccountListAdapter(getContext(), rawContactDeltas);
770 if (adapter.getCount() > 1) {
771 final String accountsSummary = getResources().getString(
772 R.string.compact_editor_linked_contacts_selector_title,
773 adapter.getCount());
774 addRawContactAccountSelector(accountsSummary, adapter);
775 }
Walter Jang708ea9e2015-09-10 15:42:05 -0700776 }
777
Walter Jang82acd422015-10-17 14:01:27 -0700778 private void addAccountHeader(Pair<String,String> accountInfo) {
779 mAccountHeaderContainer.setVisibility(View.VISIBLE);
780
Walter Jangbe5e1b12015-10-17 11:38:29 -0700781 // Set the account name
782 final String accountName = TextUtils.isEmpty(accountInfo.first)
783 ? accountInfo.second : accountInfo.first;
784 mAccountHeaderName.setVisibility(View.VISIBLE);
785 mAccountHeaderName.setText(accountName);
786
787 // Set the account type
788 final String selectorTitle = getResources().getString(
789 R.string.compact_editor_account_selector_title);
790 mAccountHeaderType.setText(selectorTitle);
791
792 // Set the icon
Walter Jang82acd422015-10-17 14:01:27 -0700793 if (mPrimaryNameKindSectionData != null) {
794 final RawContactDelta rawContactDelta =
795 mPrimaryNameKindSectionData.first.getRawContactDelta();
796 if (rawContactDelta != null) {
797 final AccountType accountType =
798 rawContactDelta.getRawContactAccountType(getContext());
799 mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext()));
800 }
801 }
Walter Jangbe5e1b12015-10-17 11:38:29 -0700802
803 // Set the content description
804 mAccountHeaderContainer.setContentDescription(
805 EditorUiUtils.getAccountInfoContentDescription(accountName, selectorTitle));
806 }
807
Walter Jang82acd422015-10-17 14:01:27 -0700808 private void addAccountSelector(Pair<String,String> accountInfo,
809 final RawContactDelta rawContactDelta) {
Walter Jang708ea9e2015-09-10 15:42:05 -0700810 mAccountSelectorContainer.setVisibility(View.VISIBLE);
811
Walter Jang03cea2e2015-09-18 17:04:21 -0700812 if (TextUtils.isEmpty(accountInfo.first)) {
Walter Jang708ea9e2015-09-10 15:42:05 -0700813 // Hide this view so the other text view will be centered vertically
814 mAccountSelectorName.setVisibility(View.GONE);
815 } else {
816 mAccountSelectorName.setVisibility(View.VISIBLE);
817 mAccountSelectorName.setText(accountInfo.first);
818 }
Walter Jang9488a762015-10-16 13:42:48 -0700819
820 final String selectorTitle = getResources().getString(
821 R.string.compact_editor_account_selector_title);
822 mAccountSelectorType.setText(selectorTitle);
Walter Jang708ea9e2015-09-10 15:42:05 -0700823
824 mAccountSelectorContainer.setContentDescription(
825 EditorUiUtils.getAccountInfoContentDescription(
Walter Jang9488a762015-10-16 13:42:48 -0700826 accountInfo.first, selectorTitle));
Walter Jang708ea9e2015-09-10 15:42:05 -0700827
828 mAccountSelector.setOnClickListener(new View.OnClickListener() {
829 @Override
830 public void onClick(View v) {
831 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
832 final AccountsListAdapter adapter =
833 new AccountsListAdapter(getContext(),
834 AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE,
835 mPrimaryAccount);
836 popup.setWidth(mAccountSelectorContainer.getWidth());
837 popup.setAnchorView(mAccountSelectorContainer);
838 popup.setAdapter(adapter);
839 popup.setModal(true);
840 popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
841 popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
842 @Override
843 public void onItemClick(AdapterView<?> parent, View view, int position,
844 long id) {
845 UiClosables.closeQuietly(popup);
846 final AccountWithDataSet newAccount = adapter.getItem(position);
847 if (mListener != null && !mPrimaryAccount.equals(newAccount)) {
848 mListener.onRebindEditorsForNewContact(
Walter Jang82acd422015-10-17 14:01:27 -0700849 rawContactDelta,
Walter Jang708ea9e2015-09-10 15:42:05 -0700850 mPrimaryAccount,
851 newAccount);
852 }
853 }
854 });
855 popup.show();
856 }
857 });
858 }
859
Walter Jang23709542015-10-22 12:50:58 -0700860 private void addRawContactAccountSelector(String accountsSummary,
861 final RawContactAccountListAdapter adapter) {
Tingting Wang655ad1a2015-10-05 17:51:14 -0700862 mRawContactContainer.setVisibility(View.VISIBLE);
863
Tingting Wang655ad1a2015-10-05 17:51:14 -0700864 mRawContactSummary.setText(accountsSummary);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700865
866 mRawContactContainer.setOnClickListener(new View.OnClickListener() {
867 @Override
868 public void onClick(View v) {
869 final ListPopupWindow popup = new ListPopupWindow(getContext(), null);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700870 popup.setWidth(mRawContactContainer.getWidth());
871 popup.setAnchorView(mRawContactContainer);
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);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700880
881 if (mListener != null) {
Tingting Wang91cee282015-10-07 13:48:17 -0700882 final long rawContactId = adapter.getItemId(position);
883 final Uri rawContactUri = ContentUris.withAppendedId(
884 ContactsContract.RawContacts.CONTENT_URI, rawContactId);
885 final RawContactDelta rawContactDelta = adapter.getItem(position);
886 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(
887 getContext());
888 final AccountType accountType = rawContactDelta.getAccountType(
889 accountTypes);
890 final boolean isReadOnly = !accountType.areContactsWritable();
891
892 mListener.onRawContactSelected(rawContactUri, rawContactId, isReadOnly);
Tingting Wang655ad1a2015-10-05 17:51:14 -0700893 }
894 }
895 });
896 popup.show();
897 }
898 });
899 }
900
Walter Jang363d3fd2015-09-16 10:29:07 -0700901 private void addPhotoView() {
Walter Jang31a74ad2015-10-02 19:17:39 -0700902 // Get the kind section data and values delta that we will display in the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700903 final KindSectionDataList kindSectionDataList =
904 mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE);
905 final Pair<KindSectionData,ValuesDelta> photoToDisplay =
906 kindSectionDataList.getEntryToDisplay(mPhotoId);
907 if (photoToDisplay == null) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700908 wlog("photo: no kind section data parsed");
Walter Jangcbd431d2015-10-06 13:07:30 -0700909 mPhotoView.setVisibility(View.GONE);
Walter Jang363d3fd2015-09-16 10:29:07 -0700910 return;
911 }
Walter Jang06f73a12015-06-17 11:15:48 -0700912
Walter Jang31a74ad2015-10-02 19:17:39 -0700913 // Set the photo view
Walter Jangcbd431d2015-10-06 13:07:30 -0700914 mPhotoView.setPhoto(photoToDisplay.second, mMaterialPalette);
Walter Jang31a74ad2015-10-02 19:17:39 -0700915
916 // Find the raw contact ID and values delta that will be written when the photo is edited
Walter Jang82acd422015-10-17 14:01:27 -0700917 final Pair<KindSectionData, ValuesDelta> pair = kindSectionDataList.getEntryToWrite(
918 mPhotoId, mPrimaryAccount, mIsUserProfile);
919 if (pair == null) {
Walter Jang31a74ad2015-10-02 19:17:39 -0700920 mPhotoView.setReadOnly(true);
921 return;
922 }
Walter Jang31a74ad2015-10-02 19:17:39 -0700923 mPhotoView.setReadOnly(false);
Walter Jang82acd422015-10-17 14:01:27 -0700924 mPhotoRawContactId = pair.first.getRawContactDelta().getRawContactId();
925 mPhotoValuesDelta = pair.second;
Walter Jang31a74ad2015-10-02 19:17:39 -0700926 }
927
Walter Jangf5dfea42015-09-16 12:30:36 -0700928 private void addKindSectionViews() {
929 // Sort the kinds
Walter Jangcbd431d2015-10-06 13:07:30 -0700930 final TreeSet<Map.Entry<String,KindSectionDataList>> entries =
Walter Jang192a01c2015-09-22 15:23:55 -0700931 new TreeSet<>(KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR);
Walter Jangf5dfea42015-09-16 12:30:36 -0700932 entries.addAll(mKindSectionDataMap.entrySet());
Walter Jang3efae4a2015-02-18 11:12:00 -0800933
Walter Jangf5dfea42015-09-16 12:30:36 -0700934 vlog("kind: " + entries.size() + " kindSection(s)");
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700935 int i = -1;
Walter Jangcbd431d2015-10-06 13:07:30 -0700936 for (Map.Entry<String, KindSectionDataList> entry : entries) {
Walter Jang3e5ae0d2015-09-20 12:43:37 -0700937 i++;
938
Walter Jangf5dfea42015-09-16 12:30:36 -0700939 final String mimeType = entry.getKey();
Walter Jang82acd422015-10-17 14:01:27 -0700940
Tingting Wangf695eb32015-10-15 18:45:15 -0700941 if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang82acd422015-10-17 14:01:27 -0700942 if (mPrimaryNameKindSectionData == null) {
943 vlog("kind: " + i + " " + mimeType + " dropped");
944 continue;
Tingting Wangf695eb32015-10-15 18:45:15 -0700945 }
Walter Jang82acd422015-10-17 14:01:27 -0700946 vlog("kind: " + i + " " + mimeType + " using first entry only");
947 final KindSectionDataList kindSectionDataList = new KindSectionDataList();
948 kindSectionDataList.add(mPrimaryNameKindSectionData.first);
Walter Jangf5dfea42015-09-16 12:30:36 -0700949 final CompactKindSectionView kindSectionView = inflateKindSectionView(
Walter Jang82acd422015-10-17 14:01:27 -0700950 mKindSectionViews, kindSectionDataList, mimeType,
951 mPrimaryNameKindSectionData.second);
Walter Jangf5dfea42015-09-16 12:30:36 -0700952 mKindSectionViews.addView(kindSectionView);
Walter Jangf10ca152015-09-22 15:23:55 -0700953
954 // Keep a pointer to all the KindSectionsViews for each mimeType
955 getKindSectionViews(mimeType).add(kindSectionView);
Walter Jang82acd422015-10-17 14:01:27 -0700956 } else {
957 final KindSectionDataList kindSectionDataList = entry.getValue();
958
959 // Ignore mime types that we've already handled
960 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
961 vlog("kind: " + i + " " + mimeType + " dropped");
962 continue;
963 }
964
965 // Don't show more than one group editor on the compact editor.
966 // Groups will still be editable for each raw contact individually on the full editor.
967 if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)
968 && kindSectionDataList.size() > 1) {
969 vlog("kind: " + i + " " + mimeType + " dropped");
970 continue;
971 }
972
973 if (kindSectionDataList != null && !kindSectionDataList.isEmpty()) {
974 vlog("kind: " + i + " " + mimeType + " " + kindSectionDataList.size() +
975 " kindSectionData(s)");
976
977 final CompactKindSectionView kindSectionView = inflateKindSectionView(
978 mKindSectionViews, kindSectionDataList, mimeType,
979 /* primaryValueDelta =*/ null);
980 mKindSectionViews.addView(kindSectionView);
981
982 // Keep a pointer to all the KindSectionsViews for each mimeType
983 getKindSectionViews(mimeType).add(kindSectionView);
984 }
Walter Jangcab3dce2015-02-09 17:48:03 -0800985 }
986 }
987 }
988
Walter Jangf10ca152015-09-22 15:23:55 -0700989 private List<CompactKindSectionView> getKindSectionViews(String mimeType) {
990 List<CompactKindSectionView> kindSectionViews = mKindSectionViewsMap.get(mimeType);
991 if (kindSectionViews == null) {
992 kindSectionViews = new ArrayList<>();
993 mKindSectionViewsMap.put(mimeType, kindSectionViews);
994 }
995 return kindSectionViews;
996 }
997
Walter Jangf5dfea42015-09-16 12:30:36 -0700998 private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup,
Walter Jang82acd422015-10-17 14:01:27 -0700999 KindSectionDataList kindSectionDataList, String mimeType,
1000 ValuesDelta primaryValuesDelta) {
Walter Jangf5dfea42015-09-16 12:30:36 -07001001 final CompactKindSectionView kindSectionView = (CompactKindSectionView)
1002 mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup,
1003 /* attachToRoot =*/ false);
Walter Jang45b86d52015-10-15 15:23:16 -07001004 kindSectionView.setIsUserProfile(mIsUserProfile);
Walter Jangf5dfea42015-09-16 12:30:36 -07001005
Walter Jang192a01c2015-09-22 15:23:55 -07001006 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)
Walter Jangf5dfea42015-09-16 12:30:36 -07001007 || Email.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001008 // Phone numbers and email addresses are always displayed,
1009 // even if they are empty
Walter Jangf5dfea42015-09-16 12:30:36 -07001010 kindSectionView.setHideWhenEmpty(false);
1011 }
Walter Jangf5dfea42015-09-16 12:30:36 -07001012
Walter Jang192a01c2015-09-22 15:23:55 -07001013 // Since phone numbers and email addresses displayed even if they are empty,
1014 // they will be the only types you add new values to initially for new contacts
1015 kindSectionView.setShowOneEmptyEditor(true);
1016
Walter Jang82acd422015-10-17 14:01:27 -07001017 // Sort non-name editors so they wind up in the order we want
1018 if (!StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) {
Walter Jang192a01c2015-09-22 15:23:55 -07001019 Collections.sort(kindSectionDataList, new EditorComparator(getContext()));
Walter Jang3e5ae0d2015-09-20 12:43:37 -07001020 }
1021
Walter Jang82acd422015-10-17 14:01:27 -07001022 kindSectionView.setState(kindSectionDataList, mViewIdGenerator, mListener,
1023 primaryValuesDelta);
Walter Jangf5dfea42015-09-16 12:30:36 -07001024
1025 return kindSectionView;
Walter Jangcab3dce2015-02-09 17:48:03 -08001026 }
1027
Walter Jang4f5594a2015-10-06 18:40:31 -07001028 private void maybeCopyPrimaryDisplayName() {
1029 if (TextUtils.isEmpty(mReadOnlyDisplayName)) return;
1030 final List<CompactKindSectionView> kindSectionViews
1031 = mKindSectionViewsMap.get(StructuredName.CONTENT_ITEM_TYPE);
Tingting Wangac4b12f2015-10-21 11:30:33 -07001032 if (kindSectionViews == null || kindSectionViews.isEmpty()) return;
Walter Jang4f5594a2015-10-06 18:40:31 -07001033 final CompactKindSectionView primaryNameKindSectionView = kindSectionViews.get(0);
1034 if (primaryNameKindSectionView.isEmptyName()) {
1035 vlog("name: using read only display name as primary name");
1036 primaryNameKindSectionView.setName(mReadOnlyDisplayName);
1037 mPrimaryNameEditorView = primaryNameKindSectionView.getPrimaryNameEditorView();
1038 }
1039 }
1040
Walter Jangd6753152015-10-02 09:23:13 -07001041 private void showAllFields() {
Walter Jang79658e12015-09-24 10:36:26 -07001042 // Stop hiding empty editors and allow the user to enter values for all kinds now
1043 for (int i = 0; i < mKindSectionViews.getChildCount(); i++) {
1044 final CompactKindSectionView kindSectionView =
1045 (CompactKindSectionView) mKindSectionViews.getChildAt(i);
1046 kindSectionView.setHideWhenEmpty(false);
1047 kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true);
1048 }
1049 mIsExpanded = true;
Walter Jang79658e12015-09-24 10:36:26 -07001050
Walter Jangf5dfea42015-09-16 12:30:36 -07001051 // Hide the more fields button
1052 mMoreFields.setVisibility(View.GONE);
Walter Jangcab3dce2015-02-09 17:48:03 -08001053 }
1054
Walter Jangbf63a6d2015-05-05 09:14:35 -07001055 private static void vlog(String message) {
1056 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1057 Log.v(TAG, message);
Walter Jangcab3dce2015-02-09 17:48:03 -08001058 }
1059 }
Walter Jang363d3fd2015-09-16 10:29:07 -07001060
1061 private static void wlog(String message) {
1062 if (Log.isLoggable(TAG, Log.WARN)) {
1063 Log.w(TAG, message);
1064 }
1065 }
1066
1067 private static void elog(String message) {
1068 Log.e(TAG, message);
1069 }
Walter Jangcab3dce2015-02-09 17:48:03 -08001070}