Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts.editor; |
| 18 | |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 19 | import android.content.ContentUris; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Gary Mai | 65971d0 | 2016-09-15 15:00:16 -0700 | [diff] [blame^] | 21 | import android.content.res.Resources; |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 22 | import android.database.Cursor; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
Gary Mai | 65971d0 | 2016-09-15 15:00:16 -0700 | [diff] [blame^] | 24 | import android.graphics.drawable.Drawable; |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Walter Jang | 3f18d61 | 2015-10-07 16:01:05 -0700 | [diff] [blame] | 26 | import android.os.Bundle; |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 27 | import android.os.Parcel; |
| 28 | import android.os.Parcelable; |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 29 | import android.provider.ContactsContract; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 30 | import android.provider.ContactsContract.CommonDataKinds.Email; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 31 | import android.provider.ContactsContract.CommonDataKinds.Event; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 32 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 33 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 34 | import android.provider.ContactsContract.CommonDataKinds.Nickname; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract.CommonDataKinds.Note; |
| 36 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 37 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 38 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 39 | import android.provider.ContactsContract.CommonDataKinds.Relation; |
| 40 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 41 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 42 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
| 43 | import android.provider.ContactsContract.CommonDataKinds.Website; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 44 | import android.text.TextUtils; |
| 45 | import android.util.AttributeSet; |
| 46 | import android.util.Log; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 47 | import android.util.Pair; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 49 | import android.view.View; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 50 | import android.view.ViewGroup; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 51 | import android.widget.AdapterView; |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 52 | import android.widget.BaseAdapter; |
| 53 | import android.widget.ImageView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 54 | import android.widget.LinearLayout; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 55 | import android.widget.ListPopupWindow; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 56 | import android.widget.TextView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 57 | |
Gary Mai | b6e28d2 | 2016-09-12 14:04:53 -0700 | [diff] [blame] | 58 | import com.android.contacts.R; |
Gary Mai | 65971d0 | 2016-09-15 15:00:16 -0700 | [diff] [blame^] | 59 | import com.android.contacts.common.GeoUtil; |
| 60 | import com.android.contacts.common.compat.PhoneNumberUtilsCompat; |
Gary Mai | b6e28d2 | 2016-09-12 14:04:53 -0700 | [diff] [blame] | 61 | import com.android.contacts.common.model.AccountTypeManager; |
| 62 | import com.android.contacts.common.model.RawContactDelta; |
| 63 | import com.android.contacts.common.model.RawContactDeltaList; |
| 64 | import com.android.contacts.common.model.RawContactModifier; |
| 65 | import com.android.contacts.common.model.ValuesDelta; |
| 66 | import com.android.contacts.common.model.account.AccountDisplayInfo; |
| 67 | import com.android.contacts.common.model.account.AccountDisplayInfoFactory; |
| 68 | import com.android.contacts.common.model.account.AccountType; |
| 69 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 70 | import com.android.contacts.common.model.dataitem.CustomDataItem; |
Gary Mai | b6e28d2 | 2016-09-12 14:04:53 -0700 | [diff] [blame] | 71 | import com.android.contacts.common.model.dataitem.DataKind; |
| 72 | import com.android.contacts.common.util.AccountsListAdapter; |
| 73 | import com.android.contacts.common.util.MaterialColorMapUtils; |
| 74 | import com.android.contacts.util.UiClosables; |
| 75 | |
Walter Jang | 708774a | 2015-10-16 09:32:06 -0700 | [diff] [blame] | 76 | import java.io.FileNotFoundException; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 77 | import java.util.ArrayList; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 78 | import java.util.Arrays; |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 79 | import java.util.Collections; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 80 | import java.util.Comparator; |
Walter Jang | 19d985f | 2015-07-01 13:36:27 -0700 | [diff] [blame] | 81 | import java.util.HashMap; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 82 | import java.util.List; |
Walter Jang | 19d985f | 2015-07-01 13:36:27 -0700 | [diff] [blame] | 83 | import java.util.Map; |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 84 | import java.util.Set; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 85 | import java.util.TreeSet; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 88 | * View to display information from multiple {@link RawContactDelta}s grouped together. |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 89 | */ |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 90 | public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener { |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 91 | |
Walter Jang | 4f5594a | 2015-10-06 18:40:31 -0700 | [diff] [blame] | 92 | static final String TAG = "CompactEditorView"; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 93 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Callbacks for hosts of {@link CompactRawContactsEditorView}s. |
| 96 | */ |
| 97 | public interface Listener { |
| 98 | |
| 99 | /** |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 100 | * Invoked when the structured name editor field has changed. |
| 101 | * |
| 102 | * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}. |
| 103 | * @param valuesDelta The values from the underlying {@link RawContactDelta}. |
| 104 | */ |
| 105 | public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Invoked when the compact editor should rebind editors for a new account. |
| 109 | * |
| 110 | * @param oldState Old data being edited. |
| 111 | * @param oldAccount Old account associated with oldState. |
| 112 | * @param newAccount New account to be used. |
| 113 | */ |
| 114 | public void onRebindEditorsForNewContact(RawContactDelta oldState, |
| 115 | AccountWithDataSet oldAccount, AccountWithDataSet newAccount); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Invoked when no editors could be bound for the contact. |
| 119 | */ |
| 120 | public void onBindEditorsFailed(); |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Invoked after editors have been bound for the contact. |
| 124 | */ |
| 125 | public void onEditorsBound(); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
Tingting Wang | 5585c6e | 2015-10-14 11:19:58 -0700 | [diff] [blame] | 128 | * Invoked when a rawcontact from linked contacts is selected in editor. |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 129 | */ |
Tingting Wang | 91cee28 | 2015-10-07 13:48:17 -0700 | [diff] [blame] | 130 | public void onRawContactSelected(Uri uri, long rawContactId, boolean isReadOnly); |
Walter Jang | 3f18d61 | 2015-10-07 16:01:05 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Returns the map of raw contact IDs to newly taken or selected photos that have not |
| 134 | * yet been saved to CP2. |
| 135 | */ |
| 136 | public Bundle getUpdatedPhotos(); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Used to list the account info for the given raw contacts list. |
| 141 | */ |
| 142 | private static final class RawContactAccountListAdapter extends BaseAdapter { |
| 143 | private final LayoutInflater mInflater; |
| 144 | private final Context mContext; |
| 145 | private final RawContactDeltaList mRawContactDeltas; |
| 146 | |
| 147 | public RawContactAccountListAdapter(Context context, RawContactDeltaList rawContactDeltas) { |
| 148 | mContext = context; |
| 149 | mRawContactDeltas = new RawContactDeltaList(); |
| 150 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
Walter Jang | 2370954 | 2015-10-22 12:50:58 -0700 | [diff] [blame] | 151 | if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) { |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 152 | mRawContactDeltas.add(rawContactDelta); |
| 153 | } |
| 154 | } |
| 155 | mInflater = LayoutInflater.from(context); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public View getView(int position, View convertView, ViewGroup parent) { |
| 160 | final View resultView = convertView != null ? convertView |
| 161 | : mInflater.inflate(R.layout.account_selector_list_item, parent, false); |
| 162 | |
| 163 | final RawContactDelta rawContactDelta = mRawContactDeltas.get(position); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 164 | |
| 165 | final TextView text1 = (TextView) resultView.findViewById(android.R.id.text1); |
Walter Jang | 9488a76 | 2015-10-16 13:42:48 -0700 | [diff] [blame] | 166 | final AccountType accountType = rawContactDelta.getRawContactAccountType(mContext); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 167 | text1.setText(accountType.getDisplayLabel(mContext)); |
| 168 | |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 169 | final TextView text2 = (TextView) resultView.findViewById(android.R.id.text2); |
Walter Jang | 9488a76 | 2015-10-16 13:42:48 -0700 | [diff] [blame] | 170 | final String accountName = rawContactDelta.getAccountName(); |
Tingting Wang | a0b6940 | 2015-10-13 14:27:58 -0700 | [diff] [blame] | 171 | if (TextUtils.isEmpty(accountName)) { |
| 172 | text2.setVisibility(View.GONE); |
| 173 | } else { |
Walter Jang | 9488a76 | 2015-10-16 13:42:48 -0700 | [diff] [blame] | 174 | // Truncate email addresses in the middle so we don't lose the domain |
Tingting Wang | a0b6940 | 2015-10-13 14:27:58 -0700 | [diff] [blame] | 175 | text2.setText(accountName); |
| 176 | text2.setEllipsize(TextUtils.TruncateAt.MIDDLE); |
| 177 | } |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 178 | |
| 179 | final ImageView icon = (ImageView) resultView.findViewById(android.R.id.icon); |
| 180 | icon.setImageDrawable(accountType.getDisplayIcon(mContext)); |
| 181 | |
| 182 | return resultView; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public int getCount() { |
| 187 | return mRawContactDeltas.size(); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public RawContactDelta getItem(int position) { |
| 192 | return mRawContactDeltas.get(position); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public long getItemId(int position) { |
| 197 | return getItem(position).getRawContactId(); |
| 198 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Sorts kinds roughly the same as quick contacts; we diverge in the following ways: |
| 203 | * <ol> |
| 204 | * <li>All names are together at the top.</li> |
| 205 | * <li>IM is moved up after addresses</li> |
| 206 | * <li>SIP addresses are moved to below phone numbers</li> |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 207 | * <li>Group membership is placed at the end</li> |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 208 | * </ol> |
| 209 | */ |
| 210 | private static final class MimeTypeComparator implements Comparator<String> { |
| 211 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 212 | private static final List<String> MIME_TYPE_ORDER = Arrays.asList(new String[] { |
| 213 | StructuredName.CONTENT_ITEM_TYPE, |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 214 | Nickname.CONTENT_ITEM_TYPE, |
Walter Jang | 2ae2175 | 2015-09-25 11:25:38 -0700 | [diff] [blame] | 215 | Organization.CONTENT_ITEM_TYPE, |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 216 | Phone.CONTENT_ITEM_TYPE, |
| 217 | SipAddress.CONTENT_ITEM_TYPE, |
| 218 | Email.CONTENT_ITEM_TYPE, |
| 219 | StructuredPostal.CONTENT_ITEM_TYPE, |
| 220 | Im.CONTENT_ITEM_TYPE, |
| 221 | Website.CONTENT_ITEM_TYPE, |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 222 | Event.CONTENT_ITEM_TYPE, |
| 223 | Relation.CONTENT_ITEM_TYPE, |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 224 | Note.CONTENT_ITEM_TYPE, |
| 225 | GroupMembership.CONTENT_ITEM_TYPE |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 226 | }); |
| 227 | |
| 228 | @Override |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 229 | public int compare(String mimeType1, String mimeType2) { |
| 230 | if (mimeType1 == mimeType2) return 0; |
| 231 | if (mimeType1 == null) return -1; |
| 232 | if (mimeType2 == null) return 1; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 233 | |
| 234 | int index1 = MIME_TYPE_ORDER.indexOf(mimeType1); |
| 235 | int index2 = MIME_TYPE_ORDER.indexOf(mimeType2); |
| 236 | |
| 237 | // Fallback to alphabetical ordering of the mime type if both are not found |
| 238 | if (index1 < 0 && index2 < 0) return mimeType1.compareTo(mimeType2); |
| 239 | if (index1 < 0) return 1; |
| 240 | if (index2 < 0) return -1; |
| 241 | |
| 242 | return index1 < index2 ? -1 : 1; |
| 243 | } |
| 244 | } |
| 245 | |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 246 | public static class SavedState extends BaseSavedState { |
| 247 | |
| 248 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 249 | new Parcelable.Creator<SavedState>() { |
| 250 | public SavedState createFromParcel(Parcel in) { |
| 251 | return new SavedState(in); |
| 252 | } |
| 253 | public SavedState[] newArray(int size) { |
| 254 | return new SavedState[size]; |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | private boolean mIsExpanded; |
| 259 | |
| 260 | public SavedState(Parcelable superState) { |
| 261 | super(superState); |
| 262 | } |
| 263 | |
| 264 | private SavedState(Parcel in) { |
| 265 | super(in); |
| 266 | mIsExpanded = in.readInt() != 0; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void writeToParcel(Parcel out, int flags) { |
| 271 | super.writeToParcel(out, flags); |
| 272 | out.writeInt(mIsExpanded ? 1 : 0); |
| 273 | } |
| 274 | } |
| 275 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 276 | private CompactRawContactsEditorView.Listener mListener; |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 277 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 278 | private AccountTypeManager mAccountTypeManager; |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 279 | private AccountDisplayInfoFactory mAccountDisplayInfoFactory; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 280 | private LayoutInflater mLayoutInflater; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 281 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 282 | private ViewIdGenerator mViewIdGenerator; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 283 | private MaterialColorMapUtils.MaterialPalette mMaterialPalette; |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 284 | private long mAggregatePhotoId = -1; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 285 | private boolean mHasNewContact; |
| 286 | private boolean mIsUserProfile; |
| 287 | private AccountWithDataSet mPrimaryAccount; |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 288 | private RawContactDeltaList mRawContactDeltas; |
| 289 | private long mRawContactIdToDisplayAlone = -1; |
| 290 | private boolean mRawContactDisplayAloneIsReadOnly; |
| 291 | private boolean mIsEditingReadOnlyRawContactWithNewContact; |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 292 | private KindSectionDataList mPhotoKindSectionDataList = new KindSectionDataList(); |
| 293 | private Map<String, KindSectionData> mKindSectionDataMap = new HashMap<>(); |
| 294 | private Set<String> mSortedMimetypes = new TreeSet<>(new MimeTypeComparator()); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 295 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 296 | // Account header |
| 297 | private View mAccountHeaderContainer; |
| 298 | private TextView mAccountHeaderType; |
| 299 | private TextView mAccountHeaderName; |
Tingting Wang | 91cee28 | 2015-10-07 13:48:17 -0700 | [diff] [blame] | 300 | private ImageView mAccountHeaderIcon; |
Gary Mai | 46cb310 | 2016-08-10 18:14:09 -0700 | [diff] [blame] | 301 | private ImageView mAccountHeaderExpanderIcon; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 302 | |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 303 | // Raw contacts selector |
| 304 | private View mRawContactContainer; |
| 305 | private TextView mRawContactSummary; |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 306 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 307 | private CompactPhotoEditorView mPhotoView; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 308 | private ViewGroup mKindSectionViews; |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 309 | private Map<String, CompactKindSectionView> mKindSectionViewMap = new HashMap<>(); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 310 | private View mMoreFields; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 311 | |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 312 | private boolean mIsExpanded; |
Walter Jang | cbd431d | 2015-10-06 13:07:30 -0700 | [diff] [blame] | 313 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 314 | private long mPhotoRawContactId; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 315 | private ValuesDelta mPhotoValuesDelta; |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 316 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 317 | public CompactRawContactsEditorView(Context context) { |
| 318 | super(context); |
| 319 | } |
| 320 | |
| 321 | public CompactRawContactsEditorView(Context context, AttributeSet attrs) { |
| 322 | super(context, attrs); |
| 323 | } |
| 324 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 325 | /** |
| 326 | * Sets the receiver for {@link CompactRawContactsEditorView} callbacks. |
| 327 | */ |
| 328 | public void setListener(Listener listener) { |
| 329 | mListener = listener; |
| 330 | } |
| 331 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 332 | @Override |
| 333 | protected void onFinishInflate() { |
| 334 | super.onFinishInflate(); |
| 335 | |
| 336 | mAccountTypeManager = AccountTypeManager.getInstance(getContext()); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 337 | mAccountDisplayInfoFactory = AccountDisplayInfoFactory.forWritableAccounts(getContext()); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 338 | mLayoutInflater = (LayoutInflater) |
| 339 | getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 340 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 341 | // Account header |
Gary Mai | 46cb310 | 2016-08-10 18:14:09 -0700 | [diff] [blame] | 342 | mAccountHeaderContainer = findViewById(R.id.account_header_container); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 343 | mAccountHeaderType = (TextView) findViewById(R.id.account_type); |
| 344 | mAccountHeaderName = (TextView) findViewById(R.id.account_name); |
Tingting Wang | 91cee28 | 2015-10-07 13:48:17 -0700 | [diff] [blame] | 345 | mAccountHeaderIcon = (ImageView) findViewById(R.id.account_type_icon); |
Gary Mai | 46cb310 | 2016-08-10 18:14:09 -0700 | [diff] [blame] | 346 | mAccountHeaderExpanderIcon = (ImageView) findViewById(R.id.account_expander_icon); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 347 | |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 348 | // Raw contacts selector |
| 349 | mRawContactContainer = findViewById(R.id.all_rawcontacts_accounts_container); |
| 350 | mRawContactSummary = (TextView) findViewById(R.id.rawcontacts_accounts_summary); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 351 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 352 | mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 353 | mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 354 | mMoreFields = findViewById(R.id.more_fields); |
| 355 | mMoreFields.setOnClickListener(this); |
| 356 | } |
| 357 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 358 | @Override |
| 359 | public void onClick(View view) { |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 360 | if (view.getId() == R.id.more_fields) { |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 361 | showAllFields(); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 362 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void setEnabled(boolean enabled) { |
| 367 | super.setEnabled(enabled); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 368 | final int childCount = mKindSectionViews.getChildCount(); |
| 369 | for (int i = 0; i < childCount; i++) { |
| 370 | mKindSectionViews.getChildAt(i).setEnabled(enabled); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 374 | @Override |
| 375 | public Parcelable onSaveInstanceState() { |
| 376 | final Parcelable superState = super.onSaveInstanceState(); |
| 377 | final SavedState savedState = new SavedState(superState); |
| 378 | savedState.mIsExpanded = mIsExpanded; |
| 379 | return savedState; |
| 380 | } |
| 381 | |
| 382 | @Override |
| 383 | public void onRestoreInstanceState(Parcelable state) { |
| 384 | if(!(state instanceof SavedState)) { |
| 385 | super.onRestoreInstanceState(state); |
| 386 | return; |
| 387 | } |
| 388 | final SavedState savedState = (SavedState) state; |
| 389 | super.onRestoreInstanceState(savedState.getSuperState()); |
| 390 | mIsExpanded = savedState.mIsExpanded; |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 391 | if (mIsExpanded) { |
| 392 | showAllFields(); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 396 | /** |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 397 | * Pass through to {@link CompactPhotoEditorView#setListener}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 398 | */ |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 399 | public void setPhotoListener(CompactPhotoEditorView.Listener listener) { |
| 400 | mPhotoView.setListener(listener); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 403 | public void removePhoto() { |
Walter Jang | 45b86d5 | 2015-10-15 15:23:16 -0700 | [diff] [blame] | 404 | mPhotoValuesDelta.setFromTemplate(true); |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 405 | mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null); |
| 406 | |
| 407 | mPhotoView.removePhoto(); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 411 | * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}. |
| 412 | */ |
| 413 | public void setFullSizePhoto(Uri photoUri) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 414 | mPhotoView.setFullSizedPhoto(photoUri); |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 417 | public void updatePhoto(Uri photoUri) { |
Walter Jang | 45b86d5 | 2015-10-15 15:23:16 -0700 | [diff] [blame] | 418 | mPhotoValuesDelta.setFromTemplate(false); |
Wenyi Wang | b8ab861 | 2015-10-27 11:18:49 -0700 | [diff] [blame] | 419 | // Unset primary for all photos |
Wenyi Wang | 9086fb9 | 2015-11-16 09:58:20 -0800 | [diff] [blame] | 420 | unsetSuperPrimaryFromAllPhotos(); |
Wenyi Wang | b8ab861 | 2015-10-27 11:18:49 -0700 | [diff] [blame] | 421 | // Mark the currently displayed photo as primary |
| 422 | mPhotoValuesDelta.setSuperPrimary(true); |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 423 | |
Wenyi Wang | 4c9cf3a | 2015-10-15 17:58:23 -0700 | [diff] [blame] | 424 | // Even though high-res photos cannot be saved by passing them via |
| 425 | // an EntityDeltaList (since they cause the Bundle size limit to be |
| 426 | // exceeded), we still pass a low-res thumbnail. This simplifies |
| 427 | // code all over the place, because we don't have to test whether |
| 428 | // there is a change in EITHER the delta-list OR a changed photo... |
| 429 | // this way, there is always a change in the delta-list. |
| 430 | try { |
| 431 | final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes( |
| 432 | getContext(), photoUri); |
| 433 | if (bytes != null) { |
| 434 | mPhotoValuesDelta.setPhoto(bytes); |
| 435 | } |
| 436 | } catch (FileNotFoundException e) { |
| 437 | elog("Failed to get bitmap from photo Uri"); |
| 438 | } |
| 439 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 440 | mPhotoView.setFullSizedPhoto(photoUri); |
| 441 | } |
| 442 | |
Wenyi Wang | 9086fb9 | 2015-11-16 09:58:20 -0800 | [diff] [blame] | 443 | private void unsetSuperPrimaryFromAllPhotos() { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 444 | for (int i = 0; i < mPhotoKindSectionDataList.size(); i++) { |
| 445 | final KindSectionData kindSectionData = mPhotoKindSectionDataList.get(0); |
| 446 | final List<ValuesDelta> valuesDeltas = kindSectionData.getNonEmptyValuesDeltas(); |
| 447 | for (int j = 0; j < valuesDeltas.size(); j++) { |
| 448 | valuesDeltas.get(j).setSuperPrimary(false); |
Walter Jang | 3f18d61 | 2015-10-07 16:01:05 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 453 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 454 | * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 455 | */ |
| 456 | public boolean isWritablePhotoSet() { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 457 | return mPhotoView.isWritablePhotoSet(); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /** |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 461 | * Get the raw contact ID for the CompactHeaderView photo. |
| 462 | */ |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 463 | public long getPhotoRawContactId() { |
| 464 | return mPhotoRawContactId; |
| 465 | } |
| 466 | |
Walter Jang | 4f5594a | 2015-10-06 18:40:31 -0700 | [diff] [blame] | 467 | public StructuredNameEditorView getPrimaryNameEditorView() { |
Walter Jang | e394595 | 2015-10-27 12:44:54 -0700 | [diff] [blame] | 468 | final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView(); |
| 469 | return primaryNameKindSectionView == null |
| 470 | ? null : primaryNameKindSectionView.getPrimaryNameEditorView(); |
Walter Jang | 4f5594a | 2015-10-06 18:40:31 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 473 | /** |
| 474 | * Returns a data holder for every non-default/non-empty photo from each raw contact, whether |
| 475 | * the raw contact is writable or not. |
| 476 | */ |
| 477 | public ArrayList<CompactPhotoSelectionFragment.Photo> getPhotos() { |
| 478 | final ArrayList<CompactPhotoSelectionFragment.Photo> photos = new ArrayList<>(); |
| 479 | |
Walter Jang | 3f18d61 | 2015-10-07 16:01:05 -0700 | [diff] [blame] | 480 | final Bundle updatedPhotos = mListener == null ? null : mListener.getUpdatedPhotos(); |
| 481 | |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 482 | for (int i = 0; i < mPhotoKindSectionDataList.size(); i++) { |
| 483 | final KindSectionData kindSectionData = mPhotoKindSectionDataList.get(i); |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 484 | final AccountType accountType = kindSectionData.getAccountType(); |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 485 | final List<ValuesDelta> valuesDeltas = kindSectionData.getNonEmptyValuesDeltas(); |
| 486 | if (valuesDeltas.isEmpty()) continue; |
| 487 | for (int j = 0; j < valuesDeltas.size(); j++) { |
| 488 | final ValuesDelta valuesDelta = valuesDeltas.get(j); |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 489 | final Bitmap bitmap = EditorUiUtils.getPhotoBitmap(valuesDelta); |
| 490 | if (bitmap == null) continue; |
| 491 | |
| 492 | final CompactPhotoSelectionFragment.Photo photo = |
| 493 | new CompactPhotoSelectionFragment.Photo(); |
| 494 | photo.titleRes = accountType.titleRes; |
| 495 | photo.iconRes = accountType.iconRes; |
| 496 | photo.syncAdapterPackageName = accountType.syncAdapterPackageName; |
| 497 | photo.valuesDelta = valuesDelta; |
| 498 | photo.primary = valuesDelta.isSuperPrimary(); |
| 499 | photo.kindSectionDataListIndex = i; |
| 500 | photo.valuesDeltaListIndex = j; |
Walter Jang | da258ff | 2015-11-17 11:06:02 -0800 | [diff] [blame] | 501 | photo.photoId = valuesDelta.getId(); |
Walter Jang | 3f18d61 | 2015-10-07 16:01:05 -0700 | [diff] [blame] | 502 | |
| 503 | if (updatedPhotos != null) { |
| 504 | photo.updatedPhotoUri = (Uri) updatedPhotos.get(String.valueOf( |
| 505 | kindSectionData.getRawContactDelta().getRawContactId())); |
| 506 | } |
| 507 | |
Wenyi Wang | d375c35 | 2016-03-28 15:12:45 -0700 | [diff] [blame] | 508 | final CharSequence accountTypeLabel = accountType.getDisplayLabel(getContext()); |
| 509 | photo.accountType = accountTypeLabel == null ? "" : accountTypeLabel.toString(); |
| 510 | final String accountName = kindSectionData.getRawContactDelta().getAccountName(); |
| 511 | photo.accountName = accountName == null ? "" : accountName; |
Wenyi Wang | ab36017 | 2015-12-15 10:35:58 -0800 | [diff] [blame] | 512 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 513 | photos.add(photo); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return photos; |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Marks the raw contact photo given as primary for the aggregate contact and updates the |
| 522 | * UI. |
| 523 | */ |
| 524 | public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) { |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 525 | // Find the values delta to mark as primary |
| 526 | if (photo.kindSectionDataListIndex < 0 |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 527 | || photo.kindSectionDataListIndex >= mPhotoKindSectionDataList.size()) { |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 528 | wlog("Invalid kind section data list index"); |
| 529 | return; |
| 530 | } |
| 531 | final KindSectionData kindSectionData = |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 532 | mPhotoKindSectionDataList.get(photo.kindSectionDataListIndex); |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 533 | final List<ValuesDelta> valuesDeltaList = kindSectionData.getNonEmptyValuesDeltas(); |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 534 | if (photo.valuesDeltaListIndex >= valuesDeltaList.size()) { |
| 535 | wlog("Invalid values delta list index"); |
| 536 | return; |
| 537 | } |
Walter Jang | da258ff | 2015-11-17 11:06:02 -0800 | [diff] [blame] | 538 | |
| 539 | // Update values delta |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 540 | final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex); |
| 541 | valuesDelta.setFromTemplate(false); |
Wenyi Wang | 9086fb9 | 2015-11-16 09:58:20 -0800 | [diff] [blame] | 542 | unsetSuperPrimaryFromAllPhotos(); |
Wenyi Wang | b8ab861 | 2015-10-27 11:18:49 -0700 | [diff] [blame] | 543 | valuesDelta.setSuperPrimary(true); |
Walter Jang | da258ff | 2015-11-17 11:06:02 -0800 | [diff] [blame] | 544 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 545 | // Update the UI |
| 546 | mPhotoView.setPhoto(valuesDelta, mMaterialPalette); |
| 547 | } |
| 548 | |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 549 | public View getAggregationAnchorView() { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 550 | CompactKindSectionView nameView = mKindSectionViewMap.get(StructuredName.CONTENT_ITEM_TYPE); |
| 551 | return nameView != null ? nameView.getChildAt(0).findViewById(R.id.anchor_view) : null; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 554 | public void setGroupMetaData(Cursor groupMetaData) { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 555 | final CompactKindSectionView groupKindSectionView = |
| 556 | mKindSectionViewMap.get(GroupMembership.CONTENT_ITEM_TYPE); |
| 557 | if (groupKindSectionView == null) { |
| 558 | return; |
| 559 | } |
| 560 | groupKindSectionView.setGroupMetaData(groupMetaData); |
| 561 | if (mIsExpanded) { |
| 562 | groupKindSectionView.setHideWhenEmpty(false); |
| 563 | groupKindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true); |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 567 | public void setState(RawContactDeltaList rawContactDeltas, |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 568 | MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator, |
Walter Jang | e394595 | 2015-10-27 12:44:54 -0700 | [diff] [blame] | 569 | long photoId, boolean hasNewContact, boolean isUserProfile, |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 570 | AccountWithDataSet primaryAccount, long rawContactIdToDisplayAlone, |
| 571 | boolean rawContactDisplayAloneIsReadOnly, |
| 572 | boolean isEditingReadOnlyRawContactWithNewContact) { |
Marcus Hagerott | f04c975 | 2016-09-08 15:22:57 -0700 | [diff] [blame] | 573 | |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 574 | mRawContactDeltas = rawContactDeltas; |
| 575 | mRawContactIdToDisplayAlone = rawContactIdToDisplayAlone; |
| 576 | mRawContactDisplayAloneIsReadOnly = rawContactDisplayAloneIsReadOnly; |
| 577 | mIsEditingReadOnlyRawContactWithNewContact = isEditingReadOnlyRawContactWithNewContact; |
| 578 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 579 | mKindSectionDataMap.clear(); |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 580 | mKindSectionViewMap.clear(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 581 | mKindSectionViews.removeAllViews(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 582 | mMoreFields.setVisibility(View.VISIBLE); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 583 | |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 584 | mMaterialPalette = materialPalette; |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 585 | mViewIdGenerator = viewIdGenerator; |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 586 | mAggregatePhotoId = photoId; |
Tingting Wang | f695eb3 | 2015-10-15 18:45:15 -0700 | [diff] [blame] | 587 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 588 | mHasNewContact = hasNewContact; |
| 589 | mIsUserProfile = isUserProfile; |
| 590 | mPrimaryAccount = primaryAccount; |
| 591 | if (mPrimaryAccount == null) { |
| 592 | mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount(); |
| 593 | } |
| 594 | vlog("state: primary " + mPrimaryAccount); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 595 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 596 | // Parse the given raw contact deltas |
| 597 | if (rawContactDeltas == null || rawContactDeltas.isEmpty()) { |
| 598 | elog("No raw contact deltas"); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 599 | if (mListener != null) mListener.onBindEditorsFailed(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 600 | return; |
| 601 | } |
Walter Jang | cbd431d | 2015-10-06 13:07:30 -0700 | [diff] [blame] | 602 | parseRawContactDeltas(rawContactDeltas); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 603 | if (mKindSectionDataMap.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 604 | elog("No kind section data parsed from RawContactDelta(s)"); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 605 | if (mListener != null) mListener.onBindEditorsFailed(); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 606 | return; |
| 607 | } |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 608 | |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 609 | final KindSectionData nameSectionData = |
| 610 | mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE); |
| 611 | // Ensure that a structured name and photo exists |
| 612 | if (nameSectionData != null) { |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 613 | final RawContactDelta rawContactDelta = |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 614 | nameSectionData.getRawContactDelta(); |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 615 | RawContactModifier.ensureKindExists( |
| 616 | rawContactDelta, |
| 617 | rawContactDelta.getAccountType(mAccountTypeManager), |
Walter Jang | cbd431d | 2015-10-06 13:07:30 -0700 | [diff] [blame] | 618 | StructuredName.CONTENT_ITEM_TYPE); |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 619 | RawContactModifier.ensureKindExists( |
| 620 | rawContactDelta, |
| 621 | rawContactDelta.getAccountType(mAccountTypeManager), |
Walter Jang | cbd431d | 2015-10-06 13:07:30 -0700 | [diff] [blame] | 622 | Photo.CONTENT_ITEM_TYPE); |
| 623 | } |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 624 | |
| 625 | // Setup the view |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 626 | addPhotoView(); |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 627 | if (isSingleReadOnlyRawContact()) { |
| 628 | // We're want to display the inputs fields for a single read only raw contact |
| 629 | addReadOnlyRawContactEditorViews(); |
| 630 | // Hide the "More fields" link |
| 631 | mMoreFields.setVisibility(View.GONE); |
| 632 | } else if (mIsEditingReadOnlyRawContactWithNewContact) { |
| 633 | // A new writable raw contact was created and joined with the read only contact |
| 634 | // that the user is trying to edit. |
| 635 | setupCompactEditorNormally(); |
Walter Jang | e394595 | 2015-10-27 12:44:54 -0700 | [diff] [blame] | 636 | |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 637 | // TODO: Hide the raw contact selector since it will just contain the read-only raw |
| 638 | // contact and clicking that will just open the exact same editor. When we clean up |
| 639 | // the whole account header, selector, and raw contact selector mess, we can prevent |
| 640 | // the selector from being displayed in a less hacky way. |
| 641 | mRawContactContainer.setVisibility(View.GONE); |
| 642 | } else if (mRawContactDeltas.size() > 1) { |
| 643 | // We're editing an aggregate composed of more than one writable raw contacts |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 644 | |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 645 | // TODO: Don't render any input fields. Eventually we will show a list of account |
| 646 | // types and names but for now just show the account selector and hide the "More fields" |
| 647 | // link. |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 648 | addAccountInfo(); |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 649 | mMoreFields.setVisibility(View.GONE); |
| 650 | } else { |
| 651 | setupCompactEditorNormally(); |
| 652 | } |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 653 | if (mListener != null) mListener.onEditorsBound(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 656 | private void setupCompactEditorNormally() { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 657 | addAccountInfo(); |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 658 | addKindSectionViews(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 659 | |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 660 | mMoreFields.setVisibility(hasMoreFields() ? View.VISIBLE : View.GONE); |
| 661 | |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 662 | if (mIsExpanded) showAllFields(); |
| 663 | } |
| 664 | |
| 665 | private boolean isSingleReadOnlyRawContact() { |
| 666 | return mRawContactDeltas.size() == 1 |
| 667 | && mRawContactDeltas.get(0).getRawContactId() == mRawContactIdToDisplayAlone |
| 668 | && mRawContactDisplayAloneIsReadOnly; |
| 669 | } |
| 670 | |
Walter Jang | cbd431d | 2015-10-06 13:07:30 -0700 | [diff] [blame] | 671 | private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas) { |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 672 | // Build the kind section data list map |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 673 | vlog("parse: " + rawContactDeltas.size() + " rawContactDelta(s)"); |
| 674 | for (int j = 0; j < rawContactDeltas.size(); j++) { |
| 675 | final RawContactDelta rawContactDelta = rawContactDeltas.get(j); |
| 676 | vlog("parse: " + j + " rawContactDelta" + rawContactDelta); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 677 | if (rawContactDelta == null || !rawContactDelta.isVisible()) continue; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 678 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 679 | if (accountType == null) continue; |
| 680 | final List<DataKind> dataKinds = accountType.getSortedDataKinds(); |
| 681 | final int dataKindSize = dataKinds == null ? 0 : dataKinds.size(); |
| 682 | vlog("parse: " + dataKindSize + " dataKinds(s)"); |
| 683 | for (int i = 0; i < dataKindSize; i++) { |
| 684 | final DataKind dataKind = dataKinds.get(i); |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 685 | if (dataKind == null) { |
| 686 | vlog("parse: " + i + " " + dataKind.mimeType + " dropped null data kind"); |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 687 | continue; |
| 688 | } |
| 689 | final String mimeType = dataKind.mimeType; |
| 690 | |
| 691 | // Skip psuedo mime types |
| 692 | if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType) |
| 693 | || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) { |
| 694 | vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type"); |
| 695 | continue; |
| 696 | } |
| 697 | |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 698 | // Skip custom fields |
| 699 | // TODO: Handle them when we implement editing custom fields. |
| 700 | if (CustomDataItem.MIMETYPE_CUSTOM_FIELD.equals(mimeType)) { |
| 701 | vlog("parse: " + i + " " + dataKind.mimeType + " dropped custom field"); |
| 702 | continue; |
| 703 | } |
| 704 | |
| 705 | // Add all photo data. |
| 706 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 707 | final KindSectionData photoKindSectionData = |
| 708 | new KindSectionData(accountType, dataKind, rawContactDelta); |
| 709 | mPhotoKindSectionDataList.add(photoKindSectionData); |
| 710 | vlog("parse: " + i + " " + dataKind.mimeType + " " + |
| 711 | photoKindSectionData.getValuesDeltas().size() + " value(s) " + |
| 712 | photoKindSectionData.getNonEmptyValuesDeltas().size() + |
| 713 | " non-empty value(s) " + |
| 714 | photoKindSectionData.getVisibleValuesDeltas().size() + |
| 715 | " visible value(s)"); |
| 716 | continue; |
| 717 | } |
| 718 | |
| 719 | // Skip the non-writable names when we're auto creating a new writable contact. |
| 720 | if (mIsEditingReadOnlyRawContactWithNewContact |
| 721 | && !accountType.areContactsWritable() |
| 722 | && StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 723 | vlog("parse: " + i + " " + dataKind.mimeType + " dropped non-writable name"); |
| 724 | continue; |
| 725 | } |
| 726 | |
| 727 | // Skip non-photo data that doesn't belong to the single raw contact we're editing. |
| 728 | if (mRawContactIdToDisplayAlone > 0 && |
| 729 | !rawContactDelta.getRawContactId().equals(mRawContactIdToDisplayAlone)) { |
| 730 | continue; |
| 731 | } |
| 732 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 733 | final KindSectionData kindSectionData = |
| 734 | new KindSectionData(accountType, dataKind, rawContactDelta); |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 735 | mKindSectionDataMap.put(mimeType, kindSectionData); |
| 736 | mSortedMimetypes.add(mimeType); |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 737 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 738 | vlog("parse: " + i + " " + dataKind.mimeType + " " + |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 739 | kindSectionData.getValuesDeltas().size() + " value(s) " + |
Tingting Wang | d482e0c | 2015-11-16 10:13:29 -0800 | [diff] [blame] | 740 | kindSectionData.getNonEmptyValuesDeltas().size() + " non-empty value(s) " + |
Wenyi Wang | 42d740c | 2015-11-13 14:23:54 -0800 | [diff] [blame] | 741 | kindSectionData.getVisibleValuesDeltas().size() + |
Walter Jang | 228e02f | 2015-11-13 09:11:01 -0800 | [diff] [blame] | 742 | " visible value(s)"); |
Walter Jang | ac679af | 2015-06-01 12:17:06 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 747 | private void addReadOnlyRawContactEditorViews() { |
Gary Mai | 65971d0 | 2016-09-15 15:00:16 -0700 | [diff] [blame^] | 748 | mKindSectionViews.removeAllViews(); |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 749 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance( |
| 750 | getContext()); |
Gary Mai | 65971d0 | 2016-09-15 15:00:16 -0700 | [diff] [blame^] | 751 | final RawContactDelta state = mRawContactDeltas |
| 752 | .getByRawContactId(mRawContactIdToDisplayAlone); |
| 753 | final AccountType type = state.getAccountType(accountTypes); |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 754 | |
Gary Mai | 65971d0 | 2016-09-15 15:00:16 -0700 | [diff] [blame^] | 755 | // Bail if invalid state or source |
| 756 | if (state == null || type == null) return; |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 757 | |
Gary Mai | 65971d0 | 2016-09-15 15:00:16 -0700 | [diff] [blame^] | 758 | // Make sure we have StructuredName |
| 759 | RawContactModifier.ensureKindExists(state, type, StructuredName.CONTENT_ITEM_TYPE); |
| 760 | |
| 761 | final AccountDisplayInfo account = mAccountDisplayInfoFactory |
| 762 | .getAccountDisplayInfoFor(state); |
| 763 | |
| 764 | final String accountTypeLabel; |
| 765 | final String accountNameLabel; |
| 766 | if (mIsUserProfile) { |
| 767 | accountTypeLabel = EditorUiUtils.getAccountHeaderLabelForMyProfile( |
| 768 | getContext(), account); |
| 769 | accountNameLabel = account.getNameLabel().toString(); |
| 770 | } else { |
| 771 | accountTypeLabel = account.getTypeLabel().toString(); |
| 772 | accountNameLabel = account.getNameLabel().toString(); |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 773 | } |
Gary Mai | 65971d0 | 2016-09-15 15:00:16 -0700 | [diff] [blame^] | 774 | |
| 775 | if (!account.hasDistinctName()) { |
| 776 | // Hide this view so the other view will be centered vertically |
| 777 | mAccountHeaderName.setVisibility(View.GONE); |
| 778 | } else { |
| 779 | mAccountHeaderName.setVisibility(View.VISIBLE); |
| 780 | mAccountHeaderName.setText(accountNameLabel); |
| 781 | } |
| 782 | mAccountHeaderType.setText(accountTypeLabel); |
| 783 | updateAccountHeaderContentDescription(); |
| 784 | |
| 785 | mAccountHeaderIcon.setImageDrawable(state.getRawContactAccountType(getContext()) |
| 786 | .getDisplayIcon(getContext())); |
| 787 | |
| 788 | ValuesDelta primary; |
| 789 | |
| 790 | // Name |
| 791 | final Context context = getContext(); |
| 792 | final Resources res = context.getResources(); |
| 793 | primary = state.getPrimaryEntry(StructuredName.CONTENT_ITEM_TYPE); |
| 794 | final String name = primary != null ? primary.getAsString(StructuredName.DISPLAY_NAME) : |
| 795 | getContext().getString(R.string.missing_name); |
| 796 | final Drawable nameDrawable = context.getDrawable(R.drawable.ic_person_24dp); |
| 797 | final String nameContentDescription = res.getString(R.string.header_name_entry); |
| 798 | bindData(nameDrawable, nameContentDescription, name, /* type */ null, |
| 799 | /* isFirstEntry */ true); |
| 800 | |
| 801 | // Phones |
| 802 | final ArrayList<ValuesDelta> phones = state.getMimeEntries(Phone.CONTENT_ITEM_TYPE); |
| 803 | final Drawable phoneDrawable = context.getDrawable(R.drawable.ic_phone_24dp); |
| 804 | final String phoneContentDescription = res.getString(R.string.header_phone_entry); |
| 805 | if (phones != null) { |
| 806 | boolean isFirstPhoneBound = true; |
| 807 | for (ValuesDelta phone : phones) { |
| 808 | final String phoneNumber = phone.getPhoneNumber(); |
| 809 | if (TextUtils.isEmpty(phoneNumber)) { |
| 810 | continue; |
| 811 | } |
| 812 | final String formattedNumber = PhoneNumberUtilsCompat.formatNumber( |
| 813 | phoneNumber, phone.getPhoneNormalizedNumber(), |
| 814 | GeoUtil.getCurrentCountryIso(getContext())); |
| 815 | CharSequence phoneType = null; |
| 816 | if (phone.hasPhoneType()) { |
| 817 | phoneType = Phone.getTypeLabel( |
| 818 | res, phone.getPhoneType(), phone.getPhoneLabel()); |
| 819 | } |
| 820 | bindData(phoneDrawable, phoneContentDescription, formattedNumber, phoneType, |
| 821 | isFirstPhoneBound, true); |
| 822 | isFirstPhoneBound = false; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | // Emails |
| 827 | final ArrayList<ValuesDelta> emails = state.getMimeEntries(Email.CONTENT_ITEM_TYPE); |
| 828 | final Drawable emailDrawable = context.getDrawable(R.drawable.ic_email_24dp); |
| 829 | final String emailContentDescription = res.getString(R.string.header_email_entry); |
| 830 | if (emails != null) { |
| 831 | boolean isFirstEmailBound = true; |
| 832 | for (ValuesDelta email : emails) { |
| 833 | final String emailAddress = email.getEmailData(); |
| 834 | if (TextUtils.isEmpty(emailAddress)) { |
| 835 | continue; |
| 836 | } |
| 837 | CharSequence emailType = null; |
| 838 | if (email.hasEmailType()) { |
| 839 | emailType = Email.getTypeLabel( |
| 840 | res, email.getEmailType(), email.getEmailLabel()); |
| 841 | } |
| 842 | bindData(emailDrawable, emailContentDescription, emailAddress, emailType, |
| 843 | isFirstEmailBound); |
| 844 | isFirstEmailBound = false; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | mKindSectionViews.setVisibility(mKindSectionViews.getChildCount() > 0 ? VISIBLE : GONE); |
| 849 | // Hide the "More fields" link |
| 850 | mMoreFields.setVisibility(GONE); |
| 851 | } |
| 852 | |
| 853 | protected void updateAccountHeaderContentDescription() { |
| 854 | final StringBuilder builder = new StringBuilder(); |
| 855 | builder.append(EditorUiUtils.getAccountInfoContentDescription( |
| 856 | mAccountHeaderName.getText(), mAccountHeaderType.getText())); |
| 857 | mAccountHeaderContainer.setContentDescription(builder); |
| 858 | } |
| 859 | |
| 860 | private void bindData(Drawable icon, String iconContentDescription, CharSequence data, |
| 861 | CharSequence type, boolean isFirstEntry) { |
| 862 | bindData(icon, iconContentDescription, data, type, isFirstEntry, false); |
| 863 | } |
| 864 | |
| 865 | private void bindData(Drawable icon, String iconContentDescription, CharSequence data, |
| 866 | CharSequence type, boolean isFirstEntry, boolean forceLTR) { |
| 867 | final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( |
| 868 | Context.LAYOUT_INFLATER_SERVICE); |
| 869 | final View field = inflater.inflate(R.layout.item_read_only_field, mKindSectionViews, |
| 870 | false); |
| 871 | if (isFirstEntry) { |
| 872 | final ImageView imageView = (ImageView) field.findViewById(R.id.kind_icon); |
| 873 | imageView.setImageDrawable(icon); |
| 874 | imageView.setContentDescription(iconContentDescription); |
| 875 | } else { |
| 876 | final ImageView imageView = (ImageView) field.findViewById(R.id.kind_icon); |
| 877 | imageView.setVisibility(View.INVISIBLE); |
| 878 | imageView.setContentDescription(null); |
| 879 | } |
| 880 | final TextView dataView = (TextView) field.findViewById(R.id.data); |
| 881 | dataView.setText(data); |
| 882 | if (forceLTR) { |
| 883 | dataView.setTextDirection(View.TEXT_DIRECTION_LTR); |
| 884 | } |
| 885 | final TextView typeView = (TextView) field.findViewById(R.id.type); |
| 886 | if (!TextUtils.isEmpty(type)) { |
| 887 | typeView.setText(type); |
| 888 | } else { |
| 889 | typeView.setVisibility(View.GONE); |
| 890 | } |
| 891 | mKindSectionViews.addView(field); |
Walter Jang | 9a55237 | 2016-08-24 11:51:05 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 894 | private void addAccountInfo() { |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 895 | mAccountHeaderContainer.setVisibility(View.GONE); |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 896 | mRawContactContainer.setVisibility(View.GONE); |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 897 | final KindSectionData nameSectionData = |
| 898 | mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE); |
| 899 | if (nameSectionData == null) return; |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 900 | final RawContactDelta rawContactDelta = |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 901 | nameSectionData.getRawContactDelta(); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 902 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 903 | final AccountDisplayInfo account = |
| 904 | mAccountDisplayInfoFactory.getAccountDisplayInfoFor(rawContactDelta); |
| 905 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 906 | // Get the account information for the primary raw contact delta |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 907 | final String accountLabel = mIsUserProfile |
| 908 | ? EditorUiUtils.getAccountHeaderLabelForMyProfile(getContext(), account) |
| 909 | : account.getNameLabel().toString(); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 910 | |
Walter Jang | 2370954 | 2015-10-22 12:50:58 -0700 | [diff] [blame] | 911 | // Either the account header or selector should be shown, not both. |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 912 | final List<AccountWithDataSet> accounts = |
| 913 | AccountTypeManager.getInstance(getContext()).getAccounts(true); |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 914 | if (mHasNewContact && !mIsUserProfile) { |
| 915 | if (accounts.size() > 1) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 916 | addAccountSelector(rawContactDelta, accountLabel); |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 917 | } else { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 918 | addAccountHeader(accountLabel); |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 919 | } |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 920 | } else if (mIsUserProfile || !shouldHideAccountContainer(mRawContactDeltas)) { |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 921 | addAccountHeader(accountLabel); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 922 | } |
Walter Jang | 2370954 | 2015-10-22 12:50:58 -0700 | [diff] [blame] | 923 | |
| 924 | // The raw contact selector should only display linked raw contacts that can be edited in |
| 925 | // the full editor (i.e. they are not newly created raw contacts) |
Tingting Wang | 2bb85d2 | 2015-10-23 13:01:09 -0700 | [diff] [blame] | 926 | final RawContactAccountListAdapter adapter = new RawContactAccountListAdapter(getContext(), |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 927 | getRawContactDeltaListForSelector(mRawContactDeltas)); |
Tingting Wang | 2bb85d2 | 2015-10-23 13:01:09 -0700 | [diff] [blame] | 928 | if (adapter.getCount() > 0) { |
| 929 | final String accountsSummary = getResources().getQuantityString( |
| 930 | R.plurals.compact_editor_linked_contacts_selector_title, |
| 931 | adapter.getCount(), adapter.getCount()); |
Walter Jang | 2370954 | 2015-10-22 12:50:58 -0700 | [diff] [blame] | 932 | addRawContactAccountSelector(accountsSummary, adapter); |
| 933 | } |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Tingting Wang | 2bb85d2 | 2015-10-23 13:01:09 -0700 | [diff] [blame] | 936 | private RawContactDeltaList getRawContactDeltaListForSelector( |
| 937 | RawContactDeltaList rawContactDeltas) { |
| 938 | // Sort raw contacts so google accounts come first |
| 939 | Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext())); |
| 940 | |
| 941 | final RawContactDeltaList result = new RawContactDeltaList(); |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 942 | for (int i = 0; i < rawContactDeltas.size(); i++) { |
| 943 | final RawContactDelta rawContactDelta = rawContactDeltas.get(i); |
Tingting Wang | 2bb85d2 | 2015-10-23 13:01:09 -0700 | [diff] [blame] | 944 | if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) { |
| 945 | // Only add raw contacts that can be opened in the editor |
| 946 | result.add(rawContactDelta); |
| 947 | } |
| 948 | } |
| 949 | // Don't return a list of size 1 that would just open the raw contact being edited |
| 950 | // in the compact editor in the full editor |
| 951 | if (result.size() == 1 && result.get(0).getRawContactAccountType( |
| 952 | getContext()).areContactsWritable()) { |
| 953 | result.clear(); |
| 954 | return result; |
| 955 | } |
| 956 | return result; |
| 957 | } |
| 958 | |
Tingting Wang | 1594c6f | 2015-10-26 17:39:16 -0700 | [diff] [blame] | 959 | // Returns true if there are multiple writable rawcontacts and no read-only ones, |
| 960 | // or there are both writable and read-only rawcontacts. |
| 961 | private boolean shouldHideAccountContainer(RawContactDeltaList rawContactDeltas) { |
Tingting Wang | 2bb85d2 | 2015-10-23 13:01:09 -0700 | [diff] [blame] | 962 | int writable = 0; |
| 963 | int readonly = 0; |
| 964 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 965 | if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) { |
| 966 | if (rawContactDelta.getRawContactAccountType(getContext()).areContactsWritable()) { |
| 967 | writable++; |
| 968 | } else { |
| 969 | readonly++; |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | return (writable > 1 || (writable > 0 && readonly > 0)); |
| 974 | } |
| 975 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 976 | private void addAccountHeader(String accountLabel) { |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 977 | mAccountHeaderContainer.setVisibility(View.VISIBLE); |
| 978 | |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 979 | // Set the account name |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 980 | mAccountHeaderName.setVisibility(View.VISIBLE); |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 981 | mAccountHeaderName.setText(accountLabel); |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 982 | |
| 983 | // Set the account type |
| 984 | final String selectorTitle = getResources().getString( |
| 985 | R.string.compact_editor_account_selector_title); |
| 986 | mAccountHeaderType.setText(selectorTitle); |
| 987 | |
| 988 | // Set the icon |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 989 | final KindSectionData nameSectionData = |
| 990 | mKindSectionDataMap.get(StructuredName.CONTENT_ITEM_TYPE); |
| 991 | if (nameSectionData != null) { |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 992 | final RawContactDelta rawContactDelta = |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 993 | nameSectionData.getRawContactDelta(); |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 994 | if (rawContactDelta != null) { |
| 995 | final AccountType accountType = |
| 996 | rawContactDelta.getRawContactAccountType(getContext()); |
| 997 | mAccountHeaderIcon.setImageDrawable(accountType.getDisplayIcon(getContext())); |
| 998 | } |
| 999 | } |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 1000 | |
| 1001 | // Set the content description |
| 1002 | mAccountHeaderContainer.setContentDescription( |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 1003 | EditorUiUtils.getAccountInfoContentDescription(accountLabel, |
| 1004 | selectorTitle)); |
Walter Jang | be5e1b1 | 2015-10-17 11:38:29 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 1007 | private void addAccountSelector(final RawContactDelta rawContactDelta, CharSequence nameLabel) { |
Gary Mai | 46cb310 | 2016-08-10 18:14:09 -0700 | [diff] [blame] | 1008 | // Show save to default account. |
Marcus Hagerott | fac695a | 2016-08-24 17:02:40 -0700 | [diff] [blame] | 1009 | addAccountHeader(nameLabel.toString()); |
Gary Mai | 46cb310 | 2016-08-10 18:14:09 -0700 | [diff] [blame] | 1010 | // Add handlers for choosing another account to save to. |
| 1011 | mAccountHeaderExpanderIcon.setVisibility(View.VISIBLE); |
| 1012 | mAccountHeaderContainer.setOnClickListener(new View.OnClickListener() { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 1013 | @Override |
| 1014 | public void onClick(View v) { |
| 1015 | final ListPopupWindow popup = new ListPopupWindow(getContext(), null); |
| 1016 | final AccountsListAdapter adapter = |
| 1017 | new AccountsListAdapter(getContext(), |
| 1018 | AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE, |
| 1019 | mPrimaryAccount); |
Gary Mai | 46cb310 | 2016-08-10 18:14:09 -0700 | [diff] [blame] | 1020 | popup.setWidth(mAccountHeaderContainer.getWidth()); |
| 1021 | popup.setAnchorView(mAccountHeaderContainer); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 1022 | popup.setAdapter(adapter); |
| 1023 | popup.setModal(true); |
| 1024 | popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 1025 | popup.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 1026 | @Override |
| 1027 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 1028 | long id) { |
| 1029 | UiClosables.closeQuietly(popup); |
| 1030 | final AccountWithDataSet newAccount = adapter.getItem(position); |
| 1031 | if (mListener != null && !mPrimaryAccount.equals(newAccount)) { |
| 1032 | mListener.onRebindEditorsForNewContact( |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 1033 | rawContactDelta, |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 1034 | mPrimaryAccount, |
| 1035 | newAccount); |
| 1036 | } |
| 1037 | } |
| 1038 | }); |
| 1039 | popup.show(); |
| 1040 | } |
| 1041 | }); |
| 1042 | } |
| 1043 | |
Walter Jang | 2370954 | 2015-10-22 12:50:58 -0700 | [diff] [blame] | 1044 | private void addRawContactAccountSelector(String accountsSummary, |
| 1045 | final RawContactAccountListAdapter adapter) { |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 1046 | mRawContactContainer.setVisibility(View.VISIBLE); |
| 1047 | |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 1048 | mRawContactSummary.setText(accountsSummary); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 1049 | |
| 1050 | mRawContactContainer.setOnClickListener(new View.OnClickListener() { |
| 1051 | @Override |
| 1052 | public void onClick(View v) { |
| 1053 | final ListPopupWindow popup = new ListPopupWindow(getContext(), null); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 1054 | popup.setWidth(mRawContactContainer.getWidth()); |
| 1055 | popup.setAnchorView(mRawContactContainer); |
| 1056 | popup.setAdapter(adapter); |
| 1057 | popup.setModal(true); |
| 1058 | popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 1059 | popup.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 1060 | @Override |
| 1061 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 1062 | long id) { |
| 1063 | UiClosables.closeQuietly(popup); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 1064 | |
| 1065 | if (mListener != null) { |
Tingting Wang | 91cee28 | 2015-10-07 13:48:17 -0700 | [diff] [blame] | 1066 | final long rawContactId = adapter.getItemId(position); |
| 1067 | final Uri rawContactUri = ContentUris.withAppendedId( |
| 1068 | ContactsContract.RawContacts.CONTENT_URI, rawContactId); |
| 1069 | final RawContactDelta rawContactDelta = adapter.getItem(position); |
| 1070 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance( |
| 1071 | getContext()); |
| 1072 | final AccountType accountType = rawContactDelta.getAccountType( |
| 1073 | accountTypes); |
| 1074 | final boolean isReadOnly = !accountType.areContactsWritable(); |
| 1075 | |
| 1076 | mListener.onRawContactSelected(rawContactUri, rawContactId, isReadOnly); |
Tingting Wang | 655ad1a | 2015-10-05 17:51:14 -0700 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | }); |
| 1080 | popup.show(); |
| 1081 | } |
| 1082 | }); |
| 1083 | } |
| 1084 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 1085 | private void addPhotoView() { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1086 | // Get the kind section data and values delta that we will display in the photo view. Either |
| 1087 | // the aggregate photo or the photo from the raw contact that is being edited. |
| 1088 | final Pair<KindSectionData, ValuesDelta> photo = |
| 1089 | mPhotoKindSectionDataList.getEntryToDisplay( |
| 1090 | mRawContactIdToDisplayAlone > 0 |
| 1091 | ? mRawContactIdToDisplayAlone |
| 1092 | : mAggregatePhotoId); |
| 1093 | if (photo == null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 1094 | wlog("photo: no kind section data parsed"); |
Walter Jang | cbd431d | 2015-10-06 13:07:30 -0700 | [diff] [blame] | 1095 | mPhotoView.setVisibility(View.GONE); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 1096 | return; |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 1097 | } else { |
| 1098 | mPhotoView.setVisibility(View.VISIBLE); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 1099 | } |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 1100 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 1101 | // Set the photo view |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1102 | mPhotoView.setPhoto(photo.second, mMaterialPalette); |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 1103 | |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1104 | // If we're showing an aggregate photo, set it to read only. |
| 1105 | if (mRawContactIdToDisplayAlone < 1) { |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 1106 | mPhotoView.setReadOnly(true); |
| 1107 | return; |
| 1108 | } |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 1109 | mPhotoView.setReadOnly(false); |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1110 | mPhotoRawContactId = photo.first.getRawContactDelta().getRawContactId(); |
| 1111 | mPhotoValuesDelta = photo.second; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1114 | private void addKindSectionViews() { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 1115 | int i = -1; |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1116 | |
| 1117 | for (String mimeType : mSortedMimetypes) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 1118 | i++; |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1119 | final CompactKindSectionView kindSectionView; |
| 1120 | // TODO: Since we don't have a primary name kind anymore, refactor and collapse |
| 1121 | // these two branches and the following code paths. |
Tingting Wang | f695eb3 | 2015-10-15 18:45:15 -0700 | [diff] [blame] | 1122 | if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1123 | final KindSectionData nameSectionData = mKindSectionDataMap.get(mimeType); |
| 1124 | if (nameSectionData == null) { |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 1125 | vlog("kind: " + i + " " + mimeType + " dropped"); |
| 1126 | continue; |
Tingting Wang | f695eb3 | 2015-10-15 18:45:15 -0700 | [diff] [blame] | 1127 | } |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 1128 | vlog("kind: " + i + " " + mimeType + " using first entry only"); |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1129 | kindSectionView = inflateKindSectionView( |
| 1130 | mKindSectionViews, nameSectionData, mimeType, |
| 1131 | nameSectionData.getValuesDeltas().get(0)); |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 1132 | } else { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1133 | final KindSectionData kindSectionData = mKindSectionDataMap.get(mimeType); |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 1134 | |
| 1135 | // Ignore mime types that we've already handled |
| 1136 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 1137 | vlog("kind: " + i + " " + mimeType + " dropped"); |
| 1138 | continue; |
| 1139 | } |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1140 | kindSectionView = inflateKindSectionView( |
| 1141 | mKindSectionViews, kindSectionData, mimeType, |
| 1142 | /* primaryValueDelta =*/ null); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 1143 | } |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1144 | mKindSectionViews.addView(kindSectionView); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 1145 | |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1146 | // Keep a pointer to the KindSectionView for each mimeType |
| 1147 | mKindSectionViewMap.put(mimeType, kindSectionView); |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 1148 | } |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1151 | private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup, |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1152 | KindSectionData kindSectionData, String mimeType, |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 1153 | ValuesDelta primaryValuesDelta) { |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1154 | final CompactKindSectionView kindSectionView = (CompactKindSectionView) |
| 1155 | mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup, |
| 1156 | /* attachToRoot =*/ false); |
Walter Jang | 45b86d5 | 2015-10-15 15:23:16 -0700 | [diff] [blame] | 1157 | kindSectionView.setIsUserProfile(mIsUserProfile); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1158 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 1159 | if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1160 | || Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 1161 | // Phone numbers and email addresses are always displayed, |
| 1162 | // even if they are empty |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1163 | kindSectionView.setHideWhenEmpty(false); |
| 1164 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1165 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 1166 | // Since phone numbers and email addresses displayed even if they are empty, |
| 1167 | // they will be the only types you add new values to initially for new contacts |
| 1168 | kindSectionView.setShowOneEmptyEditor(true); |
| 1169 | |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1170 | kindSectionView.setState(kindSectionData, mViewIdGenerator, mListener, |
Walter Jang | 82acd42 | 2015-10-17 14:01:27 -0700 | [diff] [blame] | 1171 | primaryValuesDelta); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1172 | |
| 1173 | return kindSectionView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
Walter Jang | e394595 | 2015-10-27 12:44:54 -0700 | [diff] [blame] | 1176 | void maybeSetReadOnlyDisplayNameAsPrimary(String readOnlyDisplayName) { |
| 1177 | if (TextUtils.isEmpty(readOnlyDisplayName)) return; |
| 1178 | final CompactKindSectionView primaryNameKindSectionView = getPrimaryNameKindSectionView(); |
| 1179 | if (primaryNameKindSectionView != null && primaryNameKindSectionView.isEmptyName()) { |
| 1180 | vlog("name: using read only display name as primary name"); |
| 1181 | primaryNameKindSectionView.setName(readOnlyDisplayName); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | private CompactKindSectionView getPrimaryNameKindSectionView() { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1186 | return mKindSectionViewMap.get(StructuredName.CONTENT_ITEM_TYPE); |
Walter Jang | 4f5594a | 2015-10-06 18:40:31 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 1189 | private void showAllFields() { |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 1190 | // Stop hiding empty editors and allow the user to enter values for all kinds now |
| 1191 | for (int i = 0; i < mKindSectionViews.getChildCount(); i++) { |
| 1192 | final CompactKindSectionView kindSectionView = |
| 1193 | (CompactKindSectionView) mKindSectionViews.getChildAt(i); |
| 1194 | kindSectionView.setHideWhenEmpty(false); |
| 1195 | kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true); |
| 1196 | } |
| 1197 | mIsExpanded = true; |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 1198 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 1199 | // Hide the more fields button |
| 1200 | mMoreFields.setVisibility(View.GONE); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 1203 | private boolean hasMoreFields() { |
Gary Mai | 98868d3 | 2016-09-14 11:55:04 -0700 | [diff] [blame] | 1204 | for (CompactKindSectionView section : mKindSectionViewMap.values()) { |
| 1205 | if (section.getVisibility() != View.VISIBLE) { |
| 1206 | return true; |
Marcus Hagerott | cf7f295 | 2016-09-06 13:49:17 -0700 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 1212 | private static void vlog(String message) { |
| 1213 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1214 | Log.v(TAG, message); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 1215 | } |
| 1216 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 1217 | |
| 1218 | private static void wlog(String message) { |
| 1219 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 1220 | Log.w(TAG, message); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | private static void elog(String message) { |
| 1225 | Log.e(TAG, message); |
| 1226 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 1227 | } |