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 | |
| 19 | import com.android.contacts.R; |
| 20 | import com.android.contacts.common.model.AccountTypeManager; |
| 21 | import com.android.contacts.common.model.RawContactDelta; |
| 22 | import com.android.contacts.common.model.RawContactDeltaList; |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 23 | import com.android.contacts.common.model.RawContactModifier; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 24 | import com.android.contacts.common.model.ValuesDelta; |
| 25 | import com.android.contacts.common.model.account.AccountType; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 26 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 27 | import com.android.contacts.common.model.dataitem.DataKind; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 28 | import com.android.contacts.common.util.AccountsListAdapter; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 29 | import com.android.contacts.common.util.MaterialColorMapUtils; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 30 | import com.android.contacts.editor.CompactContactEditorFragment.PhotoHandler; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 31 | import com.android.contacts.util.UiClosables; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 32 | |
| 33 | import android.content.Context; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 34 | import android.graphics.Bitmap; |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 36 | import android.provider.ContactsContract.CommonDataKinds.Email; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 37 | import android.provider.ContactsContract.CommonDataKinds.Event; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 38 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 39 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 40 | import android.provider.ContactsContract.CommonDataKinds.Nickname; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 41 | import android.provider.ContactsContract.CommonDataKinds.Note; |
| 42 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 43 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 44 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 45 | import android.provider.ContactsContract.CommonDataKinds.Relation; |
| 46 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 47 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 48 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
| 49 | import android.provider.ContactsContract.CommonDataKinds.Website; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 50 | import android.text.TextUtils; |
| 51 | import android.util.AttributeSet; |
| 52 | import android.util.Log; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 53 | import android.util.Pair; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 54 | import android.view.LayoutInflater; |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 55 | import android.view.View; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 56 | import android.view.ViewGroup; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 57 | import android.widget.AdapterView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 58 | import android.widget.LinearLayout; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 59 | import android.widget.ListPopupWindow; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 60 | import android.widget.TextView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 61 | |
| 62 | import java.util.ArrayList; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 63 | import java.util.Arrays; |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 64 | import java.util.Collections; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 65 | import java.util.Comparator; |
Walter Jang | 19d985f | 2015-07-01 13:36:27 -0700 | [diff] [blame] | 66 | import java.util.HashMap; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 67 | import java.util.List; |
Walter Jang | 19d985f | 2015-07-01 13:36:27 -0700 | [diff] [blame] | 68 | import java.util.Map; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 69 | import java.util.Objects; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 70 | import java.util.TreeSet; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 73 | * View to display information from multiple {@link RawContactDelta}s grouped together. |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 74 | */ |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 75 | public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener { |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 76 | |
| 77 | private static final String TAG = "CompactEditorView"; |
| 78 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 79 | private static final KindSectionDataMapEntryComparator |
| 80 | KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR = new KindSectionDataMapEntryComparator(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 81 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Callbacks for hosts of {@link CompactRawContactsEditorView}s. |
| 84 | */ |
| 85 | public interface Listener { |
| 86 | |
| 87 | /** |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 88 | * Invoked when the structured name editor field has changed. |
| 89 | * |
| 90 | * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}. |
| 91 | * @param valuesDelta The values from the underlying {@link RawContactDelta}. |
| 92 | */ |
| 93 | public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Invoked when the compact editor should rebind editors for a new account. |
| 97 | * |
| 98 | * @param oldState Old data being edited. |
| 99 | * @param oldAccount Old account associated with oldState. |
| 100 | * @param newAccount New account to be used. |
| 101 | */ |
| 102 | public void onRebindEditorsForNewContact(RawContactDelta oldState, |
| 103 | AccountWithDataSet oldAccount, AccountWithDataSet newAccount); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 106 | /** Used to sort entire kind sections. */ |
| 107 | private static final class KindSectionDataMapEntryComparator implements |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 108 | Comparator<Map.Entry<String,List<KindSectionData>>> { |
| 109 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 110 | final MimeTypeComparator mMimeTypeComparator = new MimeTypeComparator(); |
| 111 | |
| 112 | @Override |
| 113 | public int compare(Map.Entry<String, List<KindSectionData>> entry1, |
| 114 | Map.Entry<String, List<KindSectionData>> entry2) { |
| 115 | if (entry1 == entry2) return 0; |
| 116 | if (entry1 == null) return -1; |
| 117 | if (entry2 == null) return 1; |
| 118 | |
| 119 | final String mimeType1 = entry1.getKey(); |
| 120 | final String mimeType2 = entry2.getKey(); |
| 121 | |
| 122 | return mMimeTypeComparator.compare(mimeType1, mimeType2); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Sorts kinds roughly the same as quick contacts; we diverge in the following ways: |
| 128 | * <ol> |
| 129 | * <li>All names are together at the top.</li> |
| 130 | * <li>IM is moved up after addresses</li> |
| 131 | * <li>SIP addresses are moved to below phone numbers</li> |
| 132 | * </ol> |
| 133 | */ |
| 134 | private static final class MimeTypeComparator implements Comparator<String> { |
| 135 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 136 | private static final List<String> MIME_TYPE_ORDER = Arrays.asList(new String[] { |
| 137 | StructuredName.CONTENT_ITEM_TYPE, |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 138 | Nickname.CONTENT_ITEM_TYPE, |
| 139 | Phone.CONTENT_ITEM_TYPE, |
| 140 | SipAddress.CONTENT_ITEM_TYPE, |
| 141 | Email.CONTENT_ITEM_TYPE, |
| 142 | StructuredPostal.CONTENT_ITEM_TYPE, |
| 143 | Im.CONTENT_ITEM_TYPE, |
| 144 | Website.CONTENT_ITEM_TYPE, |
| 145 | Organization.CONTENT_ITEM_TYPE, |
| 146 | Event.CONTENT_ITEM_TYPE, |
| 147 | Relation.CONTENT_ITEM_TYPE, |
| 148 | Note.CONTENT_ITEM_TYPE |
| 149 | }); |
| 150 | |
| 151 | @Override |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 152 | public int compare(String mimeType1, String mimeType2) { |
| 153 | if (mimeType1 == mimeType2) return 0; |
| 154 | if (mimeType1 == null) return -1; |
| 155 | if (mimeType2 == null) return 1; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 156 | |
| 157 | int index1 = MIME_TYPE_ORDER.indexOf(mimeType1); |
| 158 | int index2 = MIME_TYPE_ORDER.indexOf(mimeType2); |
| 159 | |
| 160 | // Fallback to alphabetical ordering of the mime type if both are not found |
| 161 | if (index1 < 0 && index2 < 0) return mimeType1.compareTo(mimeType2); |
| 162 | if (index1 < 0) return 1; |
| 163 | if (index2 < 0) return -1; |
| 164 | |
| 165 | return index1 < index2 ? -1 : 1; |
| 166 | } |
| 167 | } |
| 168 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 169 | /** |
| 170 | * Sorts primary accounts and google account types before others. |
| 171 | */ |
| 172 | private static final class EditorComparator implements Comparator<KindSectionData> { |
| 173 | |
| 174 | private RawContactDeltaComparator mRawContactDeltaComparator; |
| 175 | |
| 176 | private EditorComparator(Context context) { |
| 177 | mRawContactDeltaComparator = new RawContactDeltaComparator(context); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public int compare(KindSectionData kindSectionData1, KindSectionData kindSectionData2) { |
| 182 | if (kindSectionData1 == kindSectionData2) return 0; |
| 183 | if (kindSectionData1 == null) return -1; |
| 184 | if (kindSectionData2 == null) return 1; |
| 185 | |
| 186 | final RawContactDelta rawContactDelta1 = kindSectionData1.getRawContactDelta(); |
| 187 | final RawContactDelta rawContactDelta2 = kindSectionData2.getRawContactDelta(); |
| 188 | |
| 189 | if (rawContactDelta1 == rawContactDelta2) return 0; |
| 190 | if (rawContactDelta1 == null) return -1; |
| 191 | if (rawContactDelta2 == null) return 1; |
| 192 | |
| 193 | return mRawContactDeltaComparator.compare(rawContactDelta1, rawContactDelta2); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Sorts primary account names first, followed by google account types, and other account |
| 199 | * types last. For names from the same account we order structured names before nicknames, |
| 200 | * but still keep names from the same account together. |
| 201 | */ |
| 202 | private static final class NameEditorComparator implements Comparator<KindSectionData> { |
| 203 | |
| 204 | private RawContactDeltaComparator mRawContactDeltaComparator; |
| 205 | private MimeTypeComparator mMimeTypeComparator; |
| 206 | private RawContactDelta mPrimaryRawContactDelta; |
| 207 | |
| 208 | private NameEditorComparator(Context context, RawContactDelta primaryRawContactDelta) { |
| 209 | mRawContactDeltaComparator = new RawContactDeltaComparator(context); |
| 210 | mMimeTypeComparator = new MimeTypeComparator(); |
| 211 | mPrimaryRawContactDelta = primaryRawContactDelta; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public int compare(KindSectionData kindSectionData1, KindSectionData kindSectionData2) { |
| 216 | if (kindSectionData1 == kindSectionData2) return 0; |
| 217 | if (kindSectionData1 == null) return -1; |
| 218 | if (kindSectionData2 == null) return 1; |
| 219 | |
| 220 | final RawContactDelta rawContactDelta1 = kindSectionData1.getRawContactDelta(); |
| 221 | final RawContactDelta rawContactDelta2 = kindSectionData2.getRawContactDelta(); |
| 222 | |
| 223 | if (rawContactDelta1 == rawContactDelta2) return 0; |
| 224 | if (rawContactDelta1 == null) return -1; |
| 225 | if (rawContactDelta2 == null) return 1; |
| 226 | |
| 227 | final boolean isRawContactDelta1Primary = |
| 228 | mPrimaryRawContactDelta.equals(rawContactDelta1); |
| 229 | final boolean isRawContactDelta2Primary = |
| 230 | mPrimaryRawContactDelta.equals(rawContactDelta2); |
| 231 | |
| 232 | // If both names are from the primary account, sort my by mime type |
| 233 | if (isRawContactDelta1Primary && isRawContactDelta2Primary) { |
| 234 | final String mimeType1 = kindSectionData1.getDataKind().mimeType; |
| 235 | final String mimeType2 = kindSectionData2.getDataKind().mimeType; |
| 236 | return mMimeTypeComparator.compare(mimeType1, mimeType2); |
| 237 | } |
| 238 | |
| 239 | // The primary account name should be before all others |
| 240 | if (isRawContactDelta1Primary) return 1; |
| 241 | if (isRawContactDelta2Primary) return -1; |
| 242 | |
| 243 | return mRawContactDeltaComparator.compare(rawContactDelta1, rawContactDelta2); |
| 244 | } |
| 245 | } |
| 246 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 247 | private CompactRawContactsEditorView.Listener mListener; |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 248 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 249 | private AccountTypeManager mAccountTypeManager; |
| 250 | private LayoutInflater mLayoutInflater; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 251 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 252 | private ViewIdGenerator mViewIdGenerator; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 253 | private MaterialColorMapUtils.MaterialPalette mMaterialPalette; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 254 | private long mPhotoId; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 255 | private boolean mHasNewContact; |
| 256 | private boolean mIsUserProfile; |
| 257 | private AccountWithDataSet mPrimaryAccount; |
| 258 | private RawContactDelta mPrimaryRawContactDelta; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 259 | private Map<String,List<KindSectionData>> mKindSectionDataMap = new HashMap<>(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 260 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 261 | // Account header |
| 262 | private View mAccountHeaderContainer; |
| 263 | private TextView mAccountHeaderType; |
| 264 | private TextView mAccountHeaderName; |
| 265 | |
| 266 | // Account selector |
| 267 | private View mAccountSelectorContainer; |
| 268 | private View mAccountSelector; |
| 269 | private TextView mAccountSelectorType; |
| 270 | private TextView mAccountSelectorName; |
| 271 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 272 | private CompactPhotoEditorView mPhotoView; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 273 | private ViewGroup mKindSectionViews; |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 274 | private View mMoreFields; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 275 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 276 | private long mPhotoRawContactId; |
| 277 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 278 | public CompactRawContactsEditorView(Context context) { |
| 279 | super(context); |
| 280 | } |
| 281 | |
| 282 | public CompactRawContactsEditorView(Context context, AttributeSet attrs) { |
| 283 | super(context, attrs); |
| 284 | } |
| 285 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 286 | /** |
| 287 | * Sets the receiver for {@link CompactRawContactsEditorView} callbacks. |
| 288 | */ |
| 289 | public void setListener(Listener listener) { |
| 290 | mListener = listener; |
| 291 | } |
| 292 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 293 | @Override |
| 294 | protected void onFinishInflate() { |
| 295 | super.onFinishInflate(); |
| 296 | |
| 297 | mAccountTypeManager = AccountTypeManager.getInstance(getContext()); |
| 298 | mLayoutInflater = (LayoutInflater) |
| 299 | getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 300 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 301 | // Account header |
| 302 | mAccountHeaderContainer = findViewById(R.id.account_container); |
| 303 | mAccountHeaderType = (TextView) findViewById(R.id.account_type); |
| 304 | mAccountHeaderName = (TextView) findViewById(R.id.account_name); |
| 305 | |
| 306 | // Account selector |
| 307 | mAccountSelectorContainer = findViewById(R.id.account_selector_container); |
| 308 | mAccountSelector = findViewById(R.id.account); |
| 309 | mAccountSelectorType = (TextView) findViewById(R.id.account_type_selector); |
| 310 | mAccountSelectorName = (TextView) findViewById(R.id.account_name_selector); |
| 311 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 312 | mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 313 | mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 314 | mMoreFields = findViewById(R.id.more_fields); |
| 315 | mMoreFields.setOnClickListener(this); |
| 316 | } |
| 317 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 318 | @Override |
| 319 | public void onClick(View view) { |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 320 | if (view.getId() == R.id.more_fields) { |
| 321 | // Stop hiding empty editors and allow the user to enter values for all kinds now |
| 322 | for (int i = 0; i < mKindSectionViews.getChildCount(); i++) { |
| 323 | final CompactKindSectionView kindSectionView = |
| 324 | (CompactKindSectionView) mKindSectionViews.getChildAt(i); |
| 325 | kindSectionView.setHideWhenEmpty(false); |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 326 | // Except the user is never allowed to add new names |
| 327 | final String mimeType = kindSectionView.getMimeType(); |
| 328 | if (!StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 329 | kindSectionView.setShowOneEmptyEditor(true); |
| 330 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 331 | kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ false); |
| 332 | } |
| 333 | |
| 334 | updateMoreFieldsButton(); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 335 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void setEnabled(boolean enabled) { |
| 340 | super.setEnabled(enabled); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 341 | final int childCount = mKindSectionViews.getChildCount(); |
| 342 | for (int i = 0; i < childCount; i++) { |
| 343 | mKindSectionViews.getChildAt(i).setEnabled(enabled); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 347 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 348 | * Pass through to {@link CompactPhotoEditorView#setPhotoHandler}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 349 | */ |
| 350 | public void setPhotoHandler(PhotoHandler photoHandler) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 351 | mPhotoView.setPhotoHandler(photoHandler); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 355 | * Pass through to {@link CompactPhotoEditorView#setPhoto}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 356 | */ |
| 357 | public void setPhoto(Bitmap bitmap) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 358 | mPhotoView.setPhoto(bitmap); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 362 | * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}. |
| 363 | */ |
| 364 | public void setFullSizePhoto(Uri photoUri) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 365 | mPhotoView.setFullSizedPhoto(photoUri); |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 369 | * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 370 | */ |
| 371 | public boolean isWritablePhotoSet() { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 372 | return mPhotoView.isWritablePhotoSet(); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 376 | * Get the raw contact ID for the CompactHeaderView photo. |
| 377 | */ |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 378 | public long getPhotoRawContactId() { |
| 379 | return mPhotoRawContactId; |
| 380 | } |
| 381 | |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 382 | public View getAggregationAnchorView() { |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 383 | // The kind section for the primary account is sorted to the front |
| 384 | final List<KindSectionData> kindSectionDataList = getKindSectionDataList( |
| 385 | StructuredName.CONTENT_ITEM_TYPE); |
| 386 | if (kindSectionDataList != null && !kindSectionDataList.isEmpty()) { |
| 387 | return mKindSectionViews.getChildAt(0).findViewById(R.id.anchor_view); |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 388 | } |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 389 | return null; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 392 | public void setState(RawContactDeltaList rawContactDeltas, |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 393 | MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator, |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 394 | long photoId, boolean hasNewContact, boolean isUserProfile, |
| 395 | AccountWithDataSet primaryAccount) { |
| 396 | // Clear previous state and reset views |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 397 | mKindSectionDataMap.clear(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 398 | mKindSectionViews.removeAllViews(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 399 | mMoreFields.setVisibility(View.VISIBLE); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 400 | |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 401 | mMaterialPalette = materialPalette; |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 402 | mViewIdGenerator = viewIdGenerator; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 403 | mPhotoId = photoId; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 404 | mHasNewContact = hasNewContact; |
| 405 | mIsUserProfile = isUserProfile; |
| 406 | mPrimaryAccount = primaryAccount; |
| 407 | if (mPrimaryAccount == null) { |
| 408 | mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount(); |
| 409 | } |
| 410 | vlog("state: primary " + mPrimaryAccount); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 411 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 412 | // Parse the given raw contact deltas |
| 413 | if (rawContactDeltas == null || rawContactDeltas.isEmpty()) { |
| 414 | elog("No raw contact deltas"); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 415 | return; |
| 416 | } |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 417 | parseRawContactDeltas(rawContactDeltas, mPrimaryAccount); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 418 | if (mKindSectionDataMap == null || mKindSectionDataMap.isEmpty()) { |
| 419 | elog("No kind section data parsed from RawContactDelta(s)"); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | // Setup the view |
| 424 | setId(mViewIdGenerator.getId(rawContactDeltas.get(0), /* dataKind =*/ null, |
| 425 | /* valuesDelta =*/ null, ViewIdGenerator.NO_VIEW_INDEX)); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 426 | addAccountInfo(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 427 | addPhotoView(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 428 | addKindSectionViews(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 429 | updateMoreFieldsButton(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 432 | private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas, |
| 433 | AccountWithDataSet primaryAccount) { |
| 434 | if (primaryAccount != null) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 435 | // Use the first writable contact that matches the primary account |
| 436 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 437 | if (!rawContactDelta.isVisible()) continue; |
| 438 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 439 | if (accountType == null || !accountType.areContactsWritable()) continue; |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 440 | if (matchesAccount(primaryAccount, rawContactDelta)) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 441 | vlog("parse: matched primary account raw contact"); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 442 | mPrimaryRawContactDelta = rawContactDelta; |
| 443 | break; |
| 444 | } |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 447 | if (mPrimaryRawContactDelta == null) { |
| 448 | // Fall back to the first writable raw contact |
| 449 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 450 | if (!rawContactDelta.isVisible()) continue; |
| 451 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 452 | if (accountType != null && accountType.areContactsWritable()) { |
| 453 | vlog("parse: falling back to the first writable raw contact as primary"); |
| 454 | mPrimaryRawContactDelta = rawContactDelta; |
| 455 | break; |
| 456 | } |
| 457 | } |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 458 | } |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 459 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 460 | if (mPrimaryRawContactDelta != null) { |
| 461 | RawContactModifier.ensureKindExists(mPrimaryRawContactDelta, |
| 462 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager), |
| 463 | StructuredName.CONTENT_ITEM_TYPE); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 464 | RawContactModifier.ensureKindExists(mPrimaryRawContactDelta, |
| 465 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager), |
| 466 | Photo.CONTENT_ITEM_TYPE); |
| 467 | } |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 468 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 469 | // Build the kind section data list map |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 470 | vlog("parse: " + rawContactDeltas.size() + " rawContactDelta(s)"); |
| 471 | for (int j = 0; j < rawContactDeltas.size(); j++) { |
| 472 | final RawContactDelta rawContactDelta = rawContactDeltas.get(j); |
| 473 | vlog("parse: " + j + " rawContactDelta" + rawContactDelta); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 474 | if (rawContactDelta == null || !rawContactDelta.isVisible()) continue; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 475 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 476 | if (accountType == null) continue; |
| 477 | final List<DataKind> dataKinds = accountType.getSortedDataKinds(); |
| 478 | final int dataKindSize = dataKinds == null ? 0 : dataKinds.size(); |
| 479 | vlog("parse: " + dataKindSize + " dataKinds(s)"); |
| 480 | for (int i = 0; i < dataKindSize; i++) { |
| 481 | final DataKind dataKind = dataKinds.get(i); |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 482 | if (dataKind == null || !dataKind.editable) { |
| 483 | vlog("parse: " + i + " " + dataKind.mimeType + " dropped read-only"); |
| 484 | continue; |
| 485 | } |
| 486 | final String mimeType = dataKind.mimeType; |
| 487 | |
| 488 | // Skip psuedo mime types |
| 489 | if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType) |
| 490 | || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) { |
| 491 | vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type"); |
| 492 | continue; |
| 493 | } |
| 494 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 495 | final List<KindSectionData> kindSectionDataList = |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 496 | getKindSectionDataList(mimeType); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 497 | final KindSectionData kindSectionData = |
| 498 | new KindSectionData(accountType, dataKind, rawContactDelta); |
| 499 | kindSectionDataList.add(kindSectionData); |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 500 | |
| 501 | // Note we must create a nickname entry on inserts |
| 502 | if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType) |
| 503 | && kindSectionData.getValuesDeltas().isEmpty() |
| 504 | && mHasNewContact) { |
| 505 | RawContactModifier.insertChild(rawContactDelta, dataKind); |
| 506 | } |
| 507 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 508 | vlog("parse: " + i + " " + dataKind.mimeType + " " + |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 509 | kindSectionData.getValuesDeltas().size() + " value(s)"); |
Walter Jang | ac679af | 2015-06-01 12:17:06 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 514 | private List<KindSectionData> getKindSectionDataList(String mimeType) { |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 515 | // Put structured names and nicknames together |
| 516 | mimeType = Nickname.CONTENT_ITEM_TYPE.equals(mimeType) |
| 517 | ? StructuredName.CONTENT_ITEM_TYPE : mimeType; |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 518 | List<KindSectionData> kindSectionDataList = mKindSectionDataMap.get(mimeType); |
| 519 | if (kindSectionDataList == null) { |
| 520 | kindSectionDataList = new ArrayList<>(); |
| 521 | mKindSectionDataMap.put(mimeType, kindSectionDataList); |
| 522 | } |
| 523 | return kindSectionDataList; |
| 524 | } |
| 525 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 526 | /** Whether the given RawContactDelta belong to the given account. */ |
| 527 | private boolean matchesAccount(AccountWithDataSet accountWithDataSet, |
| 528 | RawContactDelta rawContactDelta) { |
| 529 | if (accountWithDataSet == null) return false; |
| 530 | return Objects.equals(accountWithDataSet.name, rawContactDelta.getAccountName()) |
| 531 | && Objects.equals(accountWithDataSet.type, rawContactDelta.getAccountType()) |
| 532 | && Objects.equals(accountWithDataSet.dataSet, rawContactDelta.getDataSet()); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 535 | private void addAccountInfo() { |
| 536 | if (mPrimaryRawContactDelta == null) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 537 | mAccountHeaderContainer.setVisibility(View.GONE); |
| 538 | mAccountSelectorContainer.setVisibility(View.GONE); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | // Get the account information for the primary raw contact delta |
| 543 | final Pair<String,String> accountInfo = EditorUiUtils.getAccountInfo(getContext(), |
| 544 | mIsUserProfile, mPrimaryRawContactDelta.getAccountName(), |
| 545 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager)); |
| 546 | |
| 547 | // The account header and selector show the same information so both shouldn't be visible |
| 548 | // at the same time |
| 549 | final List<AccountWithDataSet> accounts = |
| 550 | AccountTypeManager.getInstance(getContext()).getAccounts(true); |
| 551 | if (mHasNewContact && !mIsUserProfile && accounts.size() > 1) { |
| 552 | mAccountHeaderContainer.setVisibility(View.GONE); |
| 553 | addAccountSelector(accountInfo); |
| 554 | } else { |
| 555 | addAccountHeader(accountInfo); |
| 556 | mAccountSelectorContainer.setVisibility(View.GONE); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | private void addAccountHeader(Pair<String,String> accountInfo) { |
Walter Jang | 03cea2e | 2015-09-18 17:04:21 -0700 | [diff] [blame] | 561 | if (TextUtils.isEmpty(accountInfo.first)) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 562 | // Hide this view so the other text view will be centered vertically |
| 563 | mAccountHeaderName.setVisibility(View.GONE); |
| 564 | } else { |
| 565 | mAccountHeaderName.setVisibility(View.VISIBLE); |
| 566 | mAccountHeaderName.setText(accountInfo.first); |
| 567 | } |
| 568 | mAccountHeaderType.setText(accountInfo.second); |
| 569 | |
| 570 | mAccountHeaderContainer.setContentDescription( |
| 571 | EditorUiUtils.getAccountInfoContentDescription( |
| 572 | accountInfo.first, accountInfo.second)); |
| 573 | } |
| 574 | |
| 575 | private void addAccountSelector(Pair<String,String> accountInfo) { |
| 576 | mAccountSelectorContainer.setVisibility(View.VISIBLE); |
| 577 | |
Walter Jang | 03cea2e | 2015-09-18 17:04:21 -0700 | [diff] [blame] | 578 | if (TextUtils.isEmpty(accountInfo.first)) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 579 | // Hide this view so the other text view will be centered vertically |
| 580 | mAccountSelectorName.setVisibility(View.GONE); |
| 581 | } else { |
| 582 | mAccountSelectorName.setVisibility(View.VISIBLE); |
| 583 | mAccountSelectorName.setText(accountInfo.first); |
| 584 | } |
| 585 | mAccountSelectorType.setText(accountInfo.second); |
| 586 | |
| 587 | mAccountSelectorContainer.setContentDescription( |
| 588 | EditorUiUtils.getAccountInfoContentDescription( |
| 589 | accountInfo.first, accountInfo.second)); |
| 590 | |
| 591 | mAccountSelector.setOnClickListener(new View.OnClickListener() { |
| 592 | @Override |
| 593 | public void onClick(View v) { |
| 594 | final ListPopupWindow popup = new ListPopupWindow(getContext(), null); |
| 595 | final AccountsListAdapter adapter = |
| 596 | new AccountsListAdapter(getContext(), |
| 597 | AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE, |
| 598 | mPrimaryAccount); |
| 599 | popup.setWidth(mAccountSelectorContainer.getWidth()); |
| 600 | popup.setAnchorView(mAccountSelectorContainer); |
| 601 | popup.setAdapter(adapter); |
| 602 | popup.setModal(true); |
| 603 | popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 604 | popup.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 605 | @Override |
| 606 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 607 | long id) { |
| 608 | UiClosables.closeQuietly(popup); |
| 609 | final AccountWithDataSet newAccount = adapter.getItem(position); |
| 610 | if (mListener != null && !mPrimaryAccount.equals(newAccount)) { |
| 611 | mListener.onRebindEditorsForNewContact( |
| 612 | mPrimaryRawContactDelta, |
| 613 | mPrimaryAccount, |
| 614 | newAccount); |
| 615 | } |
| 616 | } |
| 617 | }); |
| 618 | popup.show(); |
| 619 | } |
| 620 | }); |
| 621 | } |
| 622 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 623 | private void addPhotoView() { |
| 624 | // Get the kind section data and values delta that will back the photo view |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 625 | Pair<KindSectionData,ValuesDelta> pair = getPrimaryKindSectionData(mPhotoId); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 626 | if (pair == null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 627 | wlog("photo: no kind section data parsed"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | final KindSectionData kindSectionData = pair.first; |
| 631 | final ValuesDelta valuesDelta = pair.second; |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 632 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 633 | // If we're editing a read-only contact we want to display the photo from the |
| 634 | // read-only contact in a photo editor backed by the new raw contact |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 635 | // that was created. |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 636 | if (mHasNewContact) { |
| 637 | mPhotoRawContactId = mPrimaryRawContactDelta == null |
| 638 | ? null : mPrimaryRawContactDelta.getRawContactId(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 639 | } |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 640 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 641 | mPhotoRawContactId = kindSectionData.getRawContactDelta().getRawContactId(); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 642 | mPhotoView.setValues(kindSectionData.getDataKind(), valuesDelta, |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 643 | kindSectionData.getRawContactDelta(), |
| 644 | !kindSectionData.getAccountType().areContactsWritable(), mMaterialPalette, |
| 645 | mViewIdGenerator); |
| 646 | } |
| 647 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 648 | private Pair<KindSectionData,ValuesDelta> getPrimaryKindSectionData(long id) { |
| 649 | final String mimeType = Photo.CONTENT_ITEM_TYPE; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 650 | final List<KindSectionData> kindSectionDataList = mKindSectionDataMap.get(mimeType); |
| 651 | if (kindSectionDataList == null || kindSectionDataList.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 652 | wlog("photo: no kind section data parsed"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 653 | return null; |
| 654 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 655 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 656 | KindSectionData resultKindSectionData = null; |
| 657 | ValuesDelta resultValuesDelta = null; |
| 658 | if (id > 0) { |
| 659 | // Look for a match for the ID that was passed in |
| 660 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 661 | resultValuesDelta = kindSectionData.getValuesDeltaById(id); |
| 662 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 663 | vlog("photo: matched kind section data by ID"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 664 | resultKindSectionData = kindSectionData; |
| 665 | break; |
Walter Jang | 398cd4b | 2015-06-16 11:17:53 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 669 | if (resultKindSectionData == null) { |
| 670 | // Look for a super primary photo |
| 671 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 672 | resultValuesDelta = kindSectionData.getSuperPrimaryValuesDelta(); |
| 673 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 674 | wlog("photo: matched super primary kind section data"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 675 | resultKindSectionData = kindSectionData; |
| 676 | break; |
| 677 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 678 | } |
| 679 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 680 | if (resultKindSectionData == null) { |
| 681 | // Fall back to the first non-empty value |
| 682 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 683 | resultValuesDelta = kindSectionData.getFirstNonEmptyValuesDelta(); |
| 684 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 685 | vlog("photo: using first non empty value"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 686 | resultKindSectionData = kindSectionData; |
| 687 | break; |
| 688 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 691 | if (resultKindSectionData == null || resultValuesDelta == null) { |
| 692 | final List<ValuesDelta> valuesDeltaList = kindSectionDataList.get(0).getValuesDeltas(); |
| 693 | if (valuesDeltaList != null && !valuesDeltaList.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 694 | vlog("photo: falling back to first empty entry"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 695 | resultValuesDelta = valuesDeltaList.get(0); |
| 696 | resultKindSectionData = kindSectionDataList.get(0); |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 699 | return resultKindSectionData != null && resultValuesDelta != null |
| 700 | ? new Pair<>(resultKindSectionData, resultValuesDelta) : null; |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 703 | private void addKindSectionViews() { |
| 704 | // Sort the kinds |
| 705 | final TreeSet<Map.Entry<String,List<KindSectionData>>> entries = |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 706 | new TreeSet<>(KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 707 | entries.addAll(mKindSectionDataMap.entrySet()); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 708 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 709 | vlog("kind: " + entries.size() + " kindSection(s)"); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 710 | int i = -1; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 711 | for (Map.Entry<String, List<KindSectionData>> entry : entries) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 712 | i++; |
| 713 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 714 | final String mimeType = entry.getKey(); |
| 715 | final List<KindSectionData> kindSectionDataList = entry.getValue(); |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 716 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 717 | // Ignore mime types that we've already handled |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 718 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 719 | vlog("kind: " + i + " " + mimeType + " dropped"); |
| 720 | continue; |
| 721 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 722 | |
| 723 | // Ignore mime types that we don't handle |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 724 | if (GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 725 | vlog("kind: " + i + " " + mimeType + " dropped"); |
| 726 | continue; |
| 727 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 728 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 729 | if (kindSectionDataList != null && !kindSectionDataList.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 730 | vlog("kind: " + i + " " + mimeType + ": " + kindSectionDataList.size() + |
| 731 | " kindSectionData(s)"); |
| 732 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 733 | final CompactKindSectionView kindSectionView = inflateKindSectionView( |
| 734 | mKindSectionViews, kindSectionDataList, mimeType); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 735 | mKindSectionViews.addView(kindSectionView); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 740 | private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup, |
| 741 | List<KindSectionData> kindSectionDataList, String mimeType) { |
| 742 | final CompactKindSectionView kindSectionView = (CompactKindSectionView) |
| 743 | mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup, |
| 744 | /* attachToRoot =*/ false); |
| 745 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 746 | if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 747 | || Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 748 | // Phone numbers and email addresses are always displayed, |
| 749 | // even if they are empty |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 750 | kindSectionView.setHideWhenEmpty(false); |
| 751 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 752 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame^] | 753 | // Since phone numbers and email addresses displayed even if they are empty, |
| 754 | // they will be the only types you add new values to initially for new contacts |
| 755 | kindSectionView.setShowOneEmptyEditor(true); |
| 756 | |
| 757 | // Sort so the editors wind up in the order we want |
| 758 | if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 759 | Collections.sort(kindSectionDataList, new NameEditorComparator(getContext(), |
| 760 | mPrimaryRawContactDelta)); |
| 761 | } else { |
| 762 | Collections.sort(kindSectionDataList, new EditorComparator(getContext())); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | kindSectionView.setState(kindSectionDataList, /* readOnly =*/ false, mViewIdGenerator, |
| 766 | mListener); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 767 | |
| 768 | return kindSectionView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 771 | private void updateMoreFieldsButton() { |
| 772 | // If any kind section views are hidden then show the link |
| 773 | for (int i = 0; i < mKindSectionViews.getChildCount(); i++) { |
| 774 | final CompactKindSectionView kindSectionView = |
| 775 | (CompactKindSectionView) mKindSectionViews.getChildAt(i); |
| 776 | if (kindSectionView.getVisibility() == View.GONE) { |
| 777 | // Show the more fields button |
| 778 | mMoreFields.setVisibility(View.VISIBLE); |
| 779 | return; |
| 780 | } |
| 781 | } |
| 782 | // Hide the more fields button |
| 783 | mMoreFields.setVisibility(View.GONE); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 786 | private static void vlog(String message) { |
| 787 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 788 | Log.v(TAG, message); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 789 | } |
| 790 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 791 | |
| 792 | private static void wlog(String message) { |
| 793 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 794 | Log.w(TAG, message); |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | private static void elog(String message) { |
| 799 | Log.e(TAG, message); |
| 800 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 801 | } |