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; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 20 | import com.android.contacts.common.ContactsUtils; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 21 | import com.android.contacts.common.model.AccountTypeManager; |
| 22 | import com.android.contacts.common.model.RawContactDelta; |
| 23 | import com.android.contacts.common.model.RawContactDeltaList; |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 24 | import com.android.contacts.common.model.RawContactModifier; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 25 | import com.android.contacts.common.model.ValuesDelta; |
| 26 | import com.android.contacts.common.model.account.AccountType; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 27 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 28 | import com.android.contacts.common.model.dataitem.DataKind; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 29 | import com.android.contacts.common.util.AccountsListAdapter; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 30 | import com.android.contacts.common.util.MaterialColorMapUtils; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 31 | import com.android.contacts.util.ContactPhotoUtils; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 32 | import com.android.contacts.util.UiClosables; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 33 | |
| 34 | import android.content.Context; |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 35 | import android.database.Cursor; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 36 | import android.graphics.Bitmap; |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 37 | import android.net.Uri; |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 38 | import android.os.Parcel; |
| 39 | import android.os.Parcelable; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 40 | import android.provider.ContactsContract.CommonDataKinds.Email; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 41 | import android.provider.ContactsContract.CommonDataKinds.Event; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 42 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 43 | import android.provider.ContactsContract.CommonDataKinds.Im; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 44 | import android.provider.ContactsContract.CommonDataKinds.Nickname; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 45 | import android.provider.ContactsContract.CommonDataKinds.Note; |
| 46 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 47 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 48 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 49 | import android.provider.ContactsContract.CommonDataKinds.Relation; |
| 50 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 51 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 52 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
| 53 | import android.provider.ContactsContract.CommonDataKinds.Website; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 54 | import android.text.TextUtils; |
| 55 | import android.util.AttributeSet; |
| 56 | import android.util.Log; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 57 | import android.util.Pair; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 58 | import android.view.LayoutInflater; |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 59 | import android.view.View; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 60 | import android.view.ViewGroup; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 61 | import android.widget.AdapterView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 62 | import android.widget.LinearLayout; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 63 | import android.widget.ListPopupWindow; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 64 | import android.widget.TextView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 65 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 66 | import java.io.File; |
| 67 | import java.io.FileNotFoundException; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 68 | import java.util.ArrayList; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 69 | import java.util.Arrays; |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 70 | import java.util.Collections; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 71 | import java.util.Comparator; |
Walter Jang | 19d985f | 2015-07-01 13:36:27 -0700 | [diff] [blame] | 72 | import java.util.HashMap; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 73 | import java.util.List; |
Walter Jang | 19d985f | 2015-07-01 13:36:27 -0700 | [diff] [blame] | 74 | import java.util.Map; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 75 | import java.util.Objects; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 76 | import java.util.TreeSet; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 79 | * View to display information from multiple {@link RawContactDelta}s grouped together. |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 80 | */ |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 81 | public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener { |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 82 | |
| 83 | private static final String TAG = "CompactEditorView"; |
| 84 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 85 | private static final KindSectionDataMapEntryComparator |
| 86 | KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR = new KindSectionDataMapEntryComparator(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 87 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Callbacks for hosts of {@link CompactRawContactsEditorView}s. |
| 90 | */ |
| 91 | public interface Listener { |
| 92 | |
| 93 | /** |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 94 | * Invoked when the structured name editor field has changed. |
| 95 | * |
| 96 | * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}. |
| 97 | * @param valuesDelta The values from the underlying {@link RawContactDelta}. |
| 98 | */ |
| 99 | public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * Invoked when the compact editor should rebind editors for a new account. |
| 103 | * |
| 104 | * @param oldState Old data being edited. |
| 105 | * @param oldAccount Old account associated with oldState. |
| 106 | * @param newAccount New account to be used. |
| 107 | */ |
| 108 | public void onRebindEditorsForNewContact(RawContactDelta oldState, |
| 109 | AccountWithDataSet oldAccount, AccountWithDataSet newAccount); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Invoked when no editors could be bound for the contact. |
| 113 | */ |
| 114 | public void onBindEditorsFailed(); |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Invoked after editors have been bound for the contact. |
| 118 | */ |
| 119 | public void onEditorsBound(); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 122 | /** Used to sort entire kind sections. */ |
| 123 | private static final class KindSectionDataMapEntryComparator implements |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 124 | Comparator<Map.Entry<String,List<KindSectionData>>> { |
| 125 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 126 | final MimeTypeComparator mMimeTypeComparator = new MimeTypeComparator(); |
| 127 | |
| 128 | @Override |
| 129 | public int compare(Map.Entry<String, List<KindSectionData>> entry1, |
| 130 | Map.Entry<String, List<KindSectionData>> entry2) { |
| 131 | if (entry1 == entry2) return 0; |
| 132 | if (entry1 == null) return -1; |
| 133 | if (entry2 == null) return 1; |
| 134 | |
| 135 | final String mimeType1 = entry1.getKey(); |
| 136 | final String mimeType2 = entry2.getKey(); |
| 137 | |
| 138 | return mMimeTypeComparator.compare(mimeType1, mimeType2); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Sorts kinds roughly the same as quick contacts; we diverge in the following ways: |
| 144 | * <ol> |
| 145 | * <li>All names are together at the top.</li> |
| 146 | * <li>IM is moved up after addresses</li> |
| 147 | * <li>SIP addresses are moved to below phone numbers</li> |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 148 | * <li>Group membership is placed at the end</li> |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 149 | * </ol> |
| 150 | */ |
| 151 | private static final class MimeTypeComparator implements Comparator<String> { |
| 152 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 153 | private static final List<String> MIME_TYPE_ORDER = Arrays.asList(new String[] { |
| 154 | StructuredName.CONTENT_ITEM_TYPE, |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 155 | Nickname.CONTENT_ITEM_TYPE, |
Walter Jang | 2ae2175 | 2015-09-25 11:25:38 -0700 | [diff] [blame] | 156 | Organization.CONTENT_ITEM_TYPE, |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 157 | Phone.CONTENT_ITEM_TYPE, |
| 158 | SipAddress.CONTENT_ITEM_TYPE, |
| 159 | Email.CONTENT_ITEM_TYPE, |
| 160 | StructuredPostal.CONTENT_ITEM_TYPE, |
| 161 | Im.CONTENT_ITEM_TYPE, |
| 162 | Website.CONTENT_ITEM_TYPE, |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 163 | Event.CONTENT_ITEM_TYPE, |
| 164 | Relation.CONTENT_ITEM_TYPE, |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 165 | Note.CONTENT_ITEM_TYPE, |
| 166 | GroupMembership.CONTENT_ITEM_TYPE |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 167 | }); |
| 168 | |
| 169 | @Override |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 170 | public int compare(String mimeType1, String mimeType2) { |
| 171 | if (mimeType1 == mimeType2) return 0; |
| 172 | if (mimeType1 == null) return -1; |
| 173 | if (mimeType2 == null) return 1; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 174 | |
| 175 | int index1 = MIME_TYPE_ORDER.indexOf(mimeType1); |
| 176 | int index2 = MIME_TYPE_ORDER.indexOf(mimeType2); |
| 177 | |
| 178 | // Fallback to alphabetical ordering of the mime type if both are not found |
| 179 | if (index1 < 0 && index2 < 0) return mimeType1.compareTo(mimeType2); |
| 180 | if (index1 < 0) return 1; |
| 181 | if (index2 < 0) return -1; |
| 182 | |
| 183 | return index1 < index2 ? -1 : 1; |
| 184 | } |
| 185 | } |
| 186 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 187 | /** |
| 188 | * Sorts primary accounts and google account types before others. |
| 189 | */ |
| 190 | private static final class EditorComparator implements Comparator<KindSectionData> { |
| 191 | |
| 192 | private RawContactDeltaComparator mRawContactDeltaComparator; |
| 193 | |
| 194 | private EditorComparator(Context context) { |
| 195 | mRawContactDeltaComparator = new RawContactDeltaComparator(context); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public int compare(KindSectionData kindSectionData1, KindSectionData kindSectionData2) { |
| 200 | if (kindSectionData1 == kindSectionData2) return 0; |
| 201 | if (kindSectionData1 == null) return -1; |
| 202 | if (kindSectionData2 == null) return 1; |
| 203 | |
| 204 | final RawContactDelta rawContactDelta1 = kindSectionData1.getRawContactDelta(); |
| 205 | final RawContactDelta rawContactDelta2 = kindSectionData2.getRawContactDelta(); |
| 206 | |
| 207 | if (rawContactDelta1 == rawContactDelta2) return 0; |
| 208 | if (rawContactDelta1 == null) return -1; |
| 209 | if (rawContactDelta2 == null) return 1; |
| 210 | |
| 211 | return mRawContactDeltaComparator.compare(rawContactDelta1, rawContactDelta2); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Sorts primary account names first, followed by google account types, and other account |
| 217 | * types last. For names from the same account we order structured names before nicknames, |
| 218 | * but still keep names from the same account together. |
| 219 | */ |
| 220 | private static final class NameEditorComparator implements Comparator<KindSectionData> { |
| 221 | |
| 222 | private RawContactDeltaComparator mRawContactDeltaComparator; |
| 223 | private MimeTypeComparator mMimeTypeComparator; |
| 224 | private RawContactDelta mPrimaryRawContactDelta; |
| 225 | |
| 226 | private NameEditorComparator(Context context, RawContactDelta primaryRawContactDelta) { |
| 227 | mRawContactDeltaComparator = new RawContactDeltaComparator(context); |
| 228 | mMimeTypeComparator = new MimeTypeComparator(); |
| 229 | mPrimaryRawContactDelta = primaryRawContactDelta; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public int compare(KindSectionData kindSectionData1, KindSectionData kindSectionData2) { |
| 234 | if (kindSectionData1 == kindSectionData2) return 0; |
| 235 | if (kindSectionData1 == null) return -1; |
| 236 | if (kindSectionData2 == null) return 1; |
| 237 | |
| 238 | final RawContactDelta rawContactDelta1 = kindSectionData1.getRawContactDelta(); |
| 239 | final RawContactDelta rawContactDelta2 = kindSectionData2.getRawContactDelta(); |
| 240 | |
| 241 | if (rawContactDelta1 == rawContactDelta2) return 0; |
| 242 | if (rawContactDelta1 == null) return -1; |
| 243 | if (rawContactDelta2 == null) return 1; |
| 244 | |
| 245 | final boolean isRawContactDelta1Primary = |
| 246 | mPrimaryRawContactDelta.equals(rawContactDelta1); |
| 247 | final boolean isRawContactDelta2Primary = |
| 248 | mPrimaryRawContactDelta.equals(rawContactDelta2); |
| 249 | |
| 250 | // If both names are from the primary account, sort my by mime type |
| 251 | if (isRawContactDelta1Primary && isRawContactDelta2Primary) { |
| 252 | final String mimeType1 = kindSectionData1.getDataKind().mimeType; |
| 253 | final String mimeType2 = kindSectionData2.getDataKind().mimeType; |
| 254 | return mMimeTypeComparator.compare(mimeType1, mimeType2); |
| 255 | } |
| 256 | |
| 257 | // The primary account name should be before all others |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 258 | if (isRawContactDelta1Primary) return -1; |
| 259 | if (isRawContactDelta2Primary) return 1; |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 260 | |
| 261 | return mRawContactDeltaComparator.compare(rawContactDelta1, rawContactDelta2); |
| 262 | } |
| 263 | } |
| 264 | |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 265 | public static class SavedState extends BaseSavedState { |
| 266 | |
| 267 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 268 | new Parcelable.Creator<SavedState>() { |
| 269 | public SavedState createFromParcel(Parcel in) { |
| 270 | return new SavedState(in); |
| 271 | } |
| 272 | public SavedState[] newArray(int size) { |
| 273 | return new SavedState[size]; |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | private boolean mIsExpanded; |
| 278 | |
| 279 | public SavedState(Parcelable superState) { |
| 280 | super(superState); |
| 281 | } |
| 282 | |
| 283 | private SavedState(Parcel in) { |
| 284 | super(in); |
| 285 | mIsExpanded = in.readInt() != 0; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void writeToParcel(Parcel out, int flags) { |
| 290 | super.writeToParcel(out, flags); |
| 291 | out.writeInt(mIsExpanded ? 1 : 0); |
| 292 | } |
| 293 | } |
| 294 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 295 | private CompactRawContactsEditorView.Listener mListener; |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 296 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 297 | private AccountTypeManager mAccountTypeManager; |
| 298 | private LayoutInflater mLayoutInflater; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 299 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 300 | private ViewIdGenerator mViewIdGenerator; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 301 | private MaterialColorMapUtils.MaterialPalette mMaterialPalette; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 302 | private long mPhotoId; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 303 | private boolean mHasNewContact; |
| 304 | private boolean mIsUserProfile; |
| 305 | private AccountWithDataSet mPrimaryAccount; |
| 306 | private RawContactDelta mPrimaryRawContactDelta; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 307 | private Map<String,List<KindSectionData>> mKindSectionDataMap = new HashMap<>(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 308 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 309 | // Account header |
| 310 | private View mAccountHeaderContainer; |
| 311 | private TextView mAccountHeaderType; |
| 312 | private TextView mAccountHeaderName; |
| 313 | |
| 314 | // Account selector |
| 315 | private View mAccountSelectorContainer; |
| 316 | private View mAccountSelector; |
| 317 | private TextView mAccountSelectorType; |
| 318 | private TextView mAccountSelectorName; |
| 319 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 320 | private CompactPhotoEditorView mPhotoView; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 321 | private ViewGroup mKindSectionViews; |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 322 | private Map<String,List<CompactKindSectionView>> mKindSectionViewsMap = new HashMap<>(); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 323 | private View mMoreFields; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 324 | |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 325 | private boolean mIsExpanded; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 326 | private long mPhotoRawContactId; |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 327 | private ValuesDelta mPhotoValuesDelta; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 328 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 329 | public CompactRawContactsEditorView(Context context) { |
| 330 | super(context); |
| 331 | } |
| 332 | |
| 333 | public CompactRawContactsEditorView(Context context, AttributeSet attrs) { |
| 334 | super(context, attrs); |
| 335 | } |
| 336 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 337 | /** |
| 338 | * Sets the receiver for {@link CompactRawContactsEditorView} callbacks. |
| 339 | */ |
| 340 | public void setListener(Listener listener) { |
| 341 | mListener = listener; |
| 342 | } |
| 343 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 344 | @Override |
| 345 | protected void onFinishInflate() { |
| 346 | super.onFinishInflate(); |
| 347 | |
| 348 | mAccountTypeManager = AccountTypeManager.getInstance(getContext()); |
| 349 | mLayoutInflater = (LayoutInflater) |
| 350 | getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 351 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 352 | // Account header |
| 353 | mAccountHeaderContainer = findViewById(R.id.account_container); |
| 354 | mAccountHeaderType = (TextView) findViewById(R.id.account_type); |
| 355 | mAccountHeaderName = (TextView) findViewById(R.id.account_name); |
| 356 | |
| 357 | // Account selector |
| 358 | mAccountSelectorContainer = findViewById(R.id.account_selector_container); |
| 359 | mAccountSelector = findViewById(R.id.account); |
| 360 | mAccountSelectorType = (TextView) findViewById(R.id.account_type_selector); |
| 361 | mAccountSelectorName = (TextView) findViewById(R.id.account_name_selector); |
| 362 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 363 | mPhotoView = (CompactPhotoEditorView) findViewById(R.id.photo_editor); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 364 | mKindSectionViews = (LinearLayout) findViewById(R.id.kind_section_views); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 365 | mMoreFields = findViewById(R.id.more_fields); |
| 366 | mMoreFields.setOnClickListener(this); |
| 367 | } |
| 368 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 369 | @Override |
| 370 | public void onClick(View view) { |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 371 | if (view.getId() == R.id.more_fields) { |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 372 | showAllFields(); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 373 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public void setEnabled(boolean enabled) { |
| 378 | super.setEnabled(enabled); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 379 | final int childCount = mKindSectionViews.getChildCount(); |
| 380 | for (int i = 0; i < childCount; i++) { |
| 381 | mKindSectionViews.getChildAt(i).setEnabled(enabled); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 385 | @Override |
| 386 | public Parcelable onSaveInstanceState() { |
| 387 | final Parcelable superState = super.onSaveInstanceState(); |
| 388 | final SavedState savedState = new SavedState(superState); |
| 389 | savedState.mIsExpanded = mIsExpanded; |
| 390 | return savedState; |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public void onRestoreInstanceState(Parcelable state) { |
| 395 | if(!(state instanceof SavedState)) { |
| 396 | super.onRestoreInstanceState(state); |
| 397 | return; |
| 398 | } |
| 399 | final SavedState savedState = (SavedState) state; |
| 400 | super.onRestoreInstanceState(savedState.getSuperState()); |
| 401 | mIsExpanded = savedState.mIsExpanded; |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 402 | if (mIsExpanded) { |
| 403 | showAllFields(); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 407 | /** |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 408 | * Pass through to {@link CompactPhotoEditorView#setListener}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 409 | */ |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 410 | public void setPhotoListener(CompactPhotoEditorView.Listener listener) { |
| 411 | mPhotoView.setListener(listener); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 414 | public void removePhoto() { |
| 415 | mPhotoValuesDelta.setFromTemplate(false); |
| 416 | mPhotoValuesDelta.put(Photo.PHOTO, (byte[]) null); |
| 417 | |
| 418 | mPhotoView.removePhoto(); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 422 | * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}. |
| 423 | */ |
| 424 | public void setFullSizePhoto(Uri photoUri) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 425 | mPhotoView.setFullSizedPhoto(photoUri); |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 428 | public void updatePhoto(Uri photoUri) { |
| 429 | // Even though high-res photos cannot be saved by passing them via |
| 430 | // an EntityDeltaList (since they cause the Bundle size limit to be |
| 431 | // exceeded), we still pass a low-res thumbnail. This simplifies |
| 432 | // code all over the place, because we don't have to test whether |
| 433 | // there is a change in EITHER the delta-list OR a changed photo... |
| 434 | // this way, there is always a change in the delta-list. |
| 435 | mPhotoValuesDelta.setFromTemplate(false); |
| 436 | mPhotoValuesDelta.setSuperPrimary(true); |
| 437 | try { |
| 438 | final byte[] bytes = EditorUiUtils.getCompressedThumbnailBitmapBytes( |
| 439 | getContext(), photoUri); |
| 440 | if (bytes != null) { |
| 441 | mPhotoValuesDelta.setPhoto(bytes); |
| 442 | } |
| 443 | } catch (FileNotFoundException e) { |
| 444 | elog("Failed to get bitmap from photo Uri"); |
| 445 | } |
| 446 | |
| 447 | mPhotoView.setFullSizedPhoto(photoUri); |
| 448 | } |
| 449 | |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 450 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 451 | * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 452 | */ |
| 453 | public boolean isWritablePhotoSet() { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 454 | return mPhotoView.isWritablePhotoSet(); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 458 | * Get the raw contact ID for the CompactHeaderView photo. |
| 459 | */ |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 460 | public long getPhotoRawContactId() { |
| 461 | return mPhotoRawContactId; |
| 462 | } |
| 463 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 464 | /** |
| 465 | * Returns a data holder for every non-default/non-empty photo from each raw contact, whether |
| 466 | * the raw contact is writable or not. |
| 467 | */ |
| 468 | public ArrayList<CompactPhotoSelectionFragment.Photo> getPhotos() { |
| 469 | final ArrayList<CompactPhotoSelectionFragment.Photo> photos = new ArrayList<>(); |
| 470 | |
| 471 | final List<KindSectionData> kindSectionDataList = |
| 472 | mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE); |
| 473 | for (int i = 0; i < kindSectionDataList.size(); i++) { |
| 474 | final KindSectionData kindSectionData = kindSectionDataList.get(i); |
| 475 | final AccountType accountType = kindSectionData.getAccountType(); |
| 476 | final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas(); |
| 477 | if (valuesDeltaList == null || valuesDeltaList.isEmpty()) continue; |
| 478 | for (int j = 0; j < valuesDeltaList.size(); j++) { |
| 479 | final ValuesDelta valuesDelta = valuesDeltaList.get(j); |
| 480 | final Bitmap bitmap = EditorUiUtils.getPhotoBitmap(valuesDelta); |
| 481 | if (bitmap == null) continue; |
| 482 | |
| 483 | final CompactPhotoSelectionFragment.Photo photo = |
| 484 | new CompactPhotoSelectionFragment.Photo(); |
| 485 | photo.titleRes = accountType.titleRes; |
| 486 | photo.iconRes = accountType.iconRes; |
| 487 | photo.syncAdapterPackageName = accountType.syncAdapterPackageName; |
| 488 | photo.valuesDelta = valuesDelta; |
| 489 | photo.primary = valuesDelta.isSuperPrimary(); |
| 490 | photo.kindSectionDataListIndex = i; |
| 491 | photo.valuesDeltaListIndex = j; |
| 492 | photos.add(photo); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | return photos; |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Marks the raw contact photo given as primary for the aggregate contact and updates the |
| 501 | * UI. |
| 502 | */ |
| 503 | public void setPrimaryPhoto(CompactPhotoSelectionFragment.Photo photo) { |
| 504 | // Unset primary for all other photos |
| 505 | final List<KindSectionData> kindSectionDataList = |
| 506 | mKindSectionDataMap.get(Photo.CONTENT_ITEM_TYPE); |
| 507 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 508 | final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas(); |
| 509 | for (ValuesDelta valuesDelta : valuesDeltaList) { |
| 510 | valuesDelta.setSuperPrimary(false); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // Find the values delta to mark as primary |
| 515 | if (photo.kindSectionDataListIndex < 0 |
| 516 | || photo.kindSectionDataListIndex >= kindSectionDataList.size()) { |
| 517 | wlog("Invalid kind section data list index"); |
| 518 | return; |
| 519 | } |
| 520 | final KindSectionData kindSectionData = |
| 521 | kindSectionDataList.get(photo.kindSectionDataListIndex); |
| 522 | final List<ValuesDelta> valuesDeltaList = kindSectionData.getValuesDeltas(); |
| 523 | if (photo.valuesDeltaListIndex >= valuesDeltaList.size()) { |
| 524 | wlog("Invalid values delta list index"); |
| 525 | return; |
| 526 | } |
| 527 | final ValuesDelta valuesDelta = valuesDeltaList.get(photo.valuesDeltaListIndex); |
| 528 | valuesDelta.setFromTemplate(false); |
| 529 | valuesDelta.setSuperPrimary(true); |
| 530 | |
| 531 | // Update the UI |
| 532 | mPhotoView.setPhoto(valuesDelta, mMaterialPalette); |
| 533 | } |
| 534 | |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 535 | public View getAggregationAnchorView() { |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 536 | final List<CompactKindSectionView> kindSectionViews = getKindSectionViews( |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 537 | StructuredName.CONTENT_ITEM_TYPE); |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 538 | if (!kindSectionViews.isEmpty()) { |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 539 | return mKindSectionViews.getChildAt(0).findViewById(R.id.anchor_view); |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 540 | } |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 541 | return null; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 544 | public void setGroupMetaData(Cursor groupMetaData) { |
| 545 | final List<CompactKindSectionView> kindSectionViews = getKindSectionViews( |
| 546 | GroupMembership.CONTENT_ITEM_TYPE); |
| 547 | for (CompactKindSectionView kindSectionView : kindSectionViews) { |
| 548 | kindSectionView.setGroupMetaData(groupMetaData); |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 549 | if (mIsExpanded) { |
| 550 | kindSectionView.setHideWhenEmpty(false); |
| 551 | kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true); |
| 552 | } |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 556 | public void setState(RawContactDeltaList rawContactDeltas, |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 557 | MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator, |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 558 | long photoId, boolean hasNewContact, boolean isUserProfile, |
| 559 | AccountWithDataSet primaryAccount) { |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 560 | mKindSectionDataMap.clear(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 561 | mKindSectionViews.removeAllViews(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 562 | mMoreFields.setVisibility(View.VISIBLE); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 563 | |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 564 | mMaterialPalette = materialPalette; |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 565 | mViewIdGenerator = viewIdGenerator; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 566 | mPhotoId = photoId; |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 567 | mHasNewContact = hasNewContact; |
| 568 | mIsUserProfile = isUserProfile; |
| 569 | mPrimaryAccount = primaryAccount; |
| 570 | if (mPrimaryAccount == null) { |
| 571 | mPrimaryAccount = ContactEditorUtils.getInstance(getContext()).getDefaultAccount(); |
| 572 | } |
| 573 | vlog("state: primary " + mPrimaryAccount); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 574 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 575 | // Parse the given raw contact deltas |
| 576 | if (rawContactDeltas == null || rawContactDeltas.isEmpty()) { |
| 577 | elog("No raw contact deltas"); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 578 | if (mListener != null) mListener.onBindEditorsFailed(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 579 | return; |
| 580 | } |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 581 | parseRawContactDeltas(rawContactDeltas, mPrimaryAccount); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 582 | if (mKindSectionDataMap.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 583 | elog("No kind section data parsed from RawContactDelta(s)"); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 584 | if (mListener != null) mListener.onBindEditorsFailed(); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | |
| 588 | // Setup the view |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 589 | addAccountInfo(); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 590 | addPhotoView(); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 591 | addKindSectionViews(); |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 592 | |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 593 | if (mIsExpanded) { |
| 594 | showAllFields(); |
| 595 | } |
| 596 | |
| 597 | if (mListener != null) mListener.onEditorsBound(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 600 | private void parseRawContactDeltas(RawContactDeltaList rawContactDeltas, |
| 601 | AccountWithDataSet primaryAccount) { |
| 602 | if (primaryAccount != null) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 603 | // Use the first writable contact that matches the primary account |
| 604 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 605 | if (!rawContactDelta.isVisible()) continue; |
| 606 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 607 | if (accountType == null || !accountType.areContactsWritable()) continue; |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 608 | if (matchesAccount(primaryAccount, rawContactDelta)) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 609 | vlog("parse: matched primary account raw contact"); |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 610 | mPrimaryRawContactDelta = rawContactDelta; |
| 611 | break; |
| 612 | } |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 615 | if (mPrimaryRawContactDelta == null) { |
| 616 | // Fall back to the first writable raw contact |
| 617 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 618 | if (!rawContactDelta.isVisible()) continue; |
| 619 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 620 | if (accountType != null && accountType.areContactsWritable()) { |
| 621 | vlog("parse: falling back to the first writable raw contact as primary"); |
| 622 | mPrimaryRawContactDelta = rawContactDelta; |
| 623 | break; |
| 624 | } |
| 625 | } |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame] | 626 | } |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 627 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 628 | if (mPrimaryRawContactDelta != null) { |
| 629 | RawContactModifier.ensureKindExists(mPrimaryRawContactDelta, |
| 630 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager), |
| 631 | StructuredName.CONTENT_ITEM_TYPE); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 632 | RawContactModifier.ensureKindExists(mPrimaryRawContactDelta, |
| 633 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager), |
| 634 | Photo.CONTENT_ITEM_TYPE); |
| 635 | } |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 636 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 637 | // Build the kind section data list map |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 638 | vlog("parse: " + rawContactDeltas.size() + " rawContactDelta(s)"); |
| 639 | for (int j = 0; j < rawContactDeltas.size(); j++) { |
| 640 | final RawContactDelta rawContactDelta = rawContactDeltas.get(j); |
| 641 | vlog("parse: " + j + " rawContactDelta" + rawContactDelta); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 642 | if (rawContactDelta == null || !rawContactDelta.isVisible()) continue; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 643 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 644 | if (accountType == null) continue; |
| 645 | final List<DataKind> dataKinds = accountType.getSortedDataKinds(); |
| 646 | final int dataKindSize = dataKinds == null ? 0 : dataKinds.size(); |
| 647 | vlog("parse: " + dataKindSize + " dataKinds(s)"); |
| 648 | for (int i = 0; i < dataKindSize; i++) { |
| 649 | final DataKind dataKind = dataKinds.get(i); |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 650 | if (dataKind == null || !dataKind.editable) { |
| 651 | vlog("parse: " + i + " " + dataKind.mimeType + " dropped read-only"); |
| 652 | continue; |
| 653 | } |
| 654 | final String mimeType = dataKind.mimeType; |
| 655 | |
| 656 | // Skip psuedo mime types |
| 657 | if (DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME.equals(mimeType) |
| 658 | || DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) { |
| 659 | vlog("parse: " + i + " " + dataKind.mimeType + " dropped pseudo type"); |
| 660 | continue; |
| 661 | } |
| 662 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 663 | final List<KindSectionData> kindSectionDataList = |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 664 | getKindSectionDataList(mimeType); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 665 | final KindSectionData kindSectionData = |
| 666 | new KindSectionData(accountType, dataKind, rawContactDelta); |
| 667 | kindSectionDataList.add(kindSectionData); |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 668 | |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 669 | // Note we must create nickname entries |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 670 | if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType) |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 671 | && kindSectionData.getValuesDeltas().isEmpty()) { |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 672 | RawContactModifier.insertChild(rawContactDelta, dataKind); |
| 673 | } |
| 674 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 675 | vlog("parse: " + i + " " + dataKind.mimeType + " " + |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 676 | kindSectionData.getValuesDeltas().size() + " value(s)"); |
Walter Jang | ac679af | 2015-06-01 12:17:06 -0700 | [diff] [blame] | 677 | } |
| 678 | } |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 681 | private List<KindSectionData> getKindSectionDataList(String mimeType) { |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 682 | // Put structured names and nicknames together |
| 683 | mimeType = Nickname.CONTENT_ITEM_TYPE.equals(mimeType) |
| 684 | ? StructuredName.CONTENT_ITEM_TYPE : mimeType; |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 685 | List<KindSectionData> kindSectionDataList = mKindSectionDataMap.get(mimeType); |
| 686 | if (kindSectionDataList == null) { |
| 687 | kindSectionDataList = new ArrayList<>(); |
| 688 | mKindSectionDataMap.put(mimeType, kindSectionDataList); |
| 689 | } |
| 690 | return kindSectionDataList; |
| 691 | } |
| 692 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 693 | /** Whether the given RawContactDelta belong to the given account. */ |
| 694 | private boolean matchesAccount(AccountWithDataSet accountWithDataSet, |
| 695 | RawContactDelta rawContactDelta) { |
| 696 | if (accountWithDataSet == null) return false; |
| 697 | return Objects.equals(accountWithDataSet.name, rawContactDelta.getAccountName()) |
| 698 | && Objects.equals(accountWithDataSet.type, rawContactDelta.getAccountType()) |
| 699 | && Objects.equals(accountWithDataSet.dataSet, rawContactDelta.getDataSet()); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 702 | private void addAccountInfo() { |
| 703 | if (mPrimaryRawContactDelta == null) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 704 | mAccountHeaderContainer.setVisibility(View.GONE); |
| 705 | mAccountSelectorContainer.setVisibility(View.GONE); |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | // Get the account information for the primary raw contact delta |
| 710 | final Pair<String,String> accountInfo = EditorUiUtils.getAccountInfo(getContext(), |
| 711 | mIsUserProfile, mPrimaryRawContactDelta.getAccountName(), |
| 712 | mPrimaryRawContactDelta.getAccountType(mAccountTypeManager)); |
| 713 | |
| 714 | // The account header and selector show the same information so both shouldn't be visible |
| 715 | // at the same time |
| 716 | final List<AccountWithDataSet> accounts = |
| 717 | AccountTypeManager.getInstance(getContext()).getAccounts(true); |
| 718 | if (mHasNewContact && !mIsUserProfile && accounts.size() > 1) { |
| 719 | mAccountHeaderContainer.setVisibility(View.GONE); |
| 720 | addAccountSelector(accountInfo); |
| 721 | } else { |
| 722 | addAccountHeader(accountInfo); |
| 723 | mAccountSelectorContainer.setVisibility(View.GONE); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | private void addAccountHeader(Pair<String,String> accountInfo) { |
Walter Jang | 03cea2e | 2015-09-18 17:04:21 -0700 | [diff] [blame] | 728 | if (TextUtils.isEmpty(accountInfo.first)) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 729 | // Hide this view so the other text view will be centered vertically |
| 730 | mAccountHeaderName.setVisibility(View.GONE); |
| 731 | } else { |
| 732 | mAccountHeaderName.setVisibility(View.VISIBLE); |
| 733 | mAccountHeaderName.setText(accountInfo.first); |
| 734 | } |
| 735 | mAccountHeaderType.setText(accountInfo.second); |
| 736 | |
| 737 | mAccountHeaderContainer.setContentDescription( |
| 738 | EditorUiUtils.getAccountInfoContentDescription( |
| 739 | accountInfo.first, accountInfo.second)); |
| 740 | } |
| 741 | |
| 742 | private void addAccountSelector(Pair<String,String> accountInfo) { |
| 743 | mAccountSelectorContainer.setVisibility(View.VISIBLE); |
| 744 | |
Walter Jang | 03cea2e | 2015-09-18 17:04:21 -0700 | [diff] [blame] | 745 | if (TextUtils.isEmpty(accountInfo.first)) { |
Walter Jang | 708ea9e | 2015-09-10 15:42:05 -0700 | [diff] [blame] | 746 | // Hide this view so the other text view will be centered vertically |
| 747 | mAccountSelectorName.setVisibility(View.GONE); |
| 748 | } else { |
| 749 | mAccountSelectorName.setVisibility(View.VISIBLE); |
| 750 | mAccountSelectorName.setText(accountInfo.first); |
| 751 | } |
| 752 | mAccountSelectorType.setText(accountInfo.second); |
| 753 | |
| 754 | mAccountSelectorContainer.setContentDescription( |
| 755 | EditorUiUtils.getAccountInfoContentDescription( |
| 756 | accountInfo.first, accountInfo.second)); |
| 757 | |
| 758 | mAccountSelector.setOnClickListener(new View.OnClickListener() { |
| 759 | @Override |
| 760 | public void onClick(View v) { |
| 761 | final ListPopupWindow popup = new ListPopupWindow(getContext(), null); |
| 762 | final AccountsListAdapter adapter = |
| 763 | new AccountsListAdapter(getContext(), |
| 764 | AccountsListAdapter.AccountListFilter.ACCOUNTS_CONTACT_WRITABLE, |
| 765 | mPrimaryAccount); |
| 766 | popup.setWidth(mAccountSelectorContainer.getWidth()); |
| 767 | popup.setAnchorView(mAccountSelectorContainer); |
| 768 | popup.setAdapter(adapter); |
| 769 | popup.setModal(true); |
| 770 | popup.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
| 771 | popup.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 772 | @Override |
| 773 | public void onItemClick(AdapterView<?> parent, View view, int position, |
| 774 | long id) { |
| 775 | UiClosables.closeQuietly(popup); |
| 776 | final AccountWithDataSet newAccount = adapter.getItem(position); |
| 777 | if (mListener != null && !mPrimaryAccount.equals(newAccount)) { |
| 778 | mListener.onRebindEditorsForNewContact( |
| 779 | mPrimaryRawContactDelta, |
| 780 | mPrimaryAccount, |
| 781 | newAccount); |
| 782 | } |
| 783 | } |
| 784 | }); |
| 785 | popup.show(); |
| 786 | } |
| 787 | }); |
| 788 | } |
| 789 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 790 | private void addPhotoView() { |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 791 | // Get the kind section data and values delta that we will display in the photo view |
| 792 | Pair<KindSectionData,ValuesDelta> pair = getPrimaryPhotoKindSectionData(mPhotoId); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 793 | if (pair == null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 794 | wlog("photo: no kind section data parsed"); |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 795 | mPhotoView.setReadOnly(true); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 796 | return; |
| 797 | } |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 798 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 799 | // Set the photo view |
| 800 | final ValuesDelta primaryValuesDelta = pair.second; |
| 801 | mPhotoView.setPhoto(primaryValuesDelta, mMaterialPalette); |
| 802 | |
| 803 | // Find the raw contact ID and values delta that will be written when the photo is edited |
| 804 | final KindSectionData primaryKindSectionData = pair.first; |
| 805 | if (mHasNewContact && mPrimaryRawContactDelta != null |
| 806 | && !primaryKindSectionData.getValuesDeltas().isEmpty()) { |
| 807 | // If we're editing a read-only contact we want to display the photo from the |
| 808 | // read-only contact in a photo editor view, but update the new raw contact |
| 809 | // that was created. |
| 810 | mPhotoRawContactId = mPrimaryRawContactDelta.getRawContactId(); |
| 811 | mPhotoValuesDelta = primaryKindSectionData.getValuesDeltas().get(0); |
| 812 | mPhotoView.setReadOnly(false); |
| 813 | return; |
| 814 | } |
| 815 | if (primaryKindSectionData.getAccountType().areContactsWritable() && |
| 816 | !primaryKindSectionData.getValuesDeltas().isEmpty()) { |
| 817 | mPhotoRawContactId = primaryKindSectionData.getRawContactDelta().getRawContactId(); |
| 818 | mPhotoValuesDelta = primaryKindSectionData.getValuesDeltas().get(0); |
| 819 | mPhotoView.setReadOnly(false); |
| 820 | return; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 821 | } |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 822 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 823 | final KindSectionData writableKindSectionData = getFirstWritablePhotoKindSectionData(); |
| 824 | if (writableKindSectionData == null |
| 825 | || writableKindSectionData.getValuesDeltas().isEmpty()) { |
| 826 | mPhotoView.setReadOnly(true); |
| 827 | return; |
| 828 | } |
| 829 | mPhotoRawContactId = writableKindSectionData.getRawContactDelta().getRawContactId(); |
| 830 | mPhotoValuesDelta = writableKindSectionData.getValuesDeltas().get(0); |
| 831 | mPhotoView.setReadOnly(false); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 834 | private Pair<KindSectionData,ValuesDelta> getPrimaryPhotoKindSectionData(long id) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 835 | final String mimeType = Photo.CONTENT_ITEM_TYPE; |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 836 | final List<KindSectionData> kindSectionDataList = mKindSectionDataMap.get(mimeType); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 837 | |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 838 | KindSectionData resultKindSectionData = null; |
| 839 | ValuesDelta resultValuesDelta = null; |
| 840 | if (id > 0) { |
| 841 | // Look for a match for the ID that was passed in |
| 842 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 843 | resultValuesDelta = kindSectionData.getValuesDeltaById(id); |
| 844 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 845 | vlog("photo: matched kind section data by ID"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 846 | resultKindSectionData = kindSectionData; |
| 847 | break; |
Walter Jang | 398cd4b | 2015-06-16 11:17:53 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 851 | if (resultKindSectionData == null) { |
| 852 | // Look for a super primary photo |
| 853 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 854 | resultValuesDelta = kindSectionData.getSuperPrimaryValuesDelta(); |
| 855 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 856 | wlog("photo: matched super primary kind section data"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 857 | resultKindSectionData = kindSectionData; |
| 858 | break; |
| 859 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 860 | } |
| 861 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 862 | if (resultKindSectionData == null) { |
| 863 | // Fall back to the first non-empty value |
| 864 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 865 | resultValuesDelta = kindSectionData.getFirstNonEmptyValuesDelta(); |
| 866 | if (resultValuesDelta != null) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 867 | vlog("photo: using first non empty value"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 868 | resultKindSectionData = kindSectionData; |
| 869 | break; |
| 870 | } |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 871 | } |
| 872 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 873 | if (resultKindSectionData == null || resultValuesDelta == null) { |
| 874 | final List<ValuesDelta> valuesDeltaList = kindSectionDataList.get(0).getValuesDeltas(); |
| 875 | if (valuesDeltaList != null && !valuesDeltaList.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 876 | vlog("photo: falling back to first empty entry"); |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 877 | resultValuesDelta = valuesDeltaList.get(0); |
| 878 | resultKindSectionData = kindSectionDataList.get(0); |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 879 | } |
| 880 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 881 | return resultKindSectionData != null && resultValuesDelta != null |
| 882 | ? new Pair<>(resultKindSectionData, resultValuesDelta) : null; |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 883 | } |
| 884 | |
Walter Jang | 31a74ad | 2015-10-02 19:17:39 -0700 | [diff] [blame^] | 885 | private KindSectionData getFirstWritablePhotoKindSectionData() { |
| 886 | final String mimeType = Photo.CONTENT_ITEM_TYPE; |
| 887 | final List<KindSectionData> kindSectionDataList = mKindSectionDataMap.get(mimeType); |
| 888 | for (KindSectionData kindSectionData : kindSectionDataList) { |
| 889 | if (kindSectionData.getAccountType().areContactsWritable()) { |
| 890 | return kindSectionData; |
| 891 | } |
| 892 | } |
| 893 | return null; |
| 894 | } |
| 895 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 896 | private void addKindSectionViews() { |
| 897 | // Sort the kinds |
| 898 | final TreeSet<Map.Entry<String,List<KindSectionData>>> entries = |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 899 | new TreeSet<>(KIND_SECTION_DATA_MAP_ENTRY_COMPARATOR); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 900 | entries.addAll(mKindSectionDataMap.entrySet()); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 901 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 902 | vlog("kind: " + entries.size() + " kindSection(s)"); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 903 | int i = -1; |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 904 | for (Map.Entry<String, List<KindSectionData>> entry : entries) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 905 | i++; |
| 906 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 907 | final String mimeType = entry.getKey(); |
| 908 | final List<KindSectionData> kindSectionDataList = entry.getValue(); |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 909 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 910 | // Ignore mime types that we've already handled |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 911 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 912 | vlog("kind: " + i + " " + mimeType + " dropped"); |
| 913 | continue; |
| 914 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 915 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 916 | if (kindSectionDataList != null && !kindSectionDataList.isEmpty()) { |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 917 | vlog("kind: " + i + " " + mimeType + ": " + kindSectionDataList.size() + |
| 918 | " kindSectionData(s)"); |
| 919 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 920 | final CompactKindSectionView kindSectionView = inflateKindSectionView( |
| 921 | mKindSectionViews, kindSectionDataList, mimeType); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 922 | mKindSectionViews.addView(kindSectionView); |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 923 | |
| 924 | // Keep a pointer to all the KindSectionsViews for each mimeType |
| 925 | getKindSectionViews(mimeType).add(kindSectionView); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Walter Jang | f10ca15 | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 930 | private List<CompactKindSectionView> getKindSectionViews(String mimeType) { |
| 931 | List<CompactKindSectionView> kindSectionViews = mKindSectionViewsMap.get(mimeType); |
| 932 | if (kindSectionViews == null) { |
| 933 | kindSectionViews = new ArrayList<>(); |
| 934 | mKindSectionViewsMap.put(mimeType, kindSectionViews); |
| 935 | } |
| 936 | return kindSectionViews; |
| 937 | } |
| 938 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 939 | private CompactKindSectionView inflateKindSectionView(ViewGroup viewGroup, |
| 940 | List<KindSectionData> kindSectionDataList, String mimeType) { |
| 941 | final CompactKindSectionView kindSectionView = (CompactKindSectionView) |
| 942 | mLayoutInflater.inflate(R.layout.compact_item_kind_section, viewGroup, |
| 943 | /* attachToRoot =*/ false); |
| 944 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 945 | if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 946 | || Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 947 | // Phone numbers and email addresses are always displayed, |
| 948 | // even if they are empty |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 949 | kindSectionView.setHideWhenEmpty(false); |
| 950 | } |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 951 | |
Walter Jang | 192a01c | 2015-09-22 15:23:55 -0700 | [diff] [blame] | 952 | // Since phone numbers and email addresses displayed even if they are empty, |
| 953 | // they will be the only types you add new values to initially for new contacts |
| 954 | kindSectionView.setShowOneEmptyEditor(true); |
| 955 | |
| 956 | // Sort so the editors wind up in the order we want |
| 957 | if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 958 | Collections.sort(kindSectionDataList, new NameEditorComparator(getContext(), |
| 959 | mPrimaryRawContactDelta)); |
| 960 | } else { |
| 961 | Collections.sort(kindSectionDataList, new EditorComparator(getContext())); |
Walter Jang | 3e5ae0d | 2015-09-20 12:43:37 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 964 | kindSectionView.setState(kindSectionDataList, mViewIdGenerator, mListener); |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 965 | |
| 966 | return kindSectionView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 967 | } |
| 968 | |
Walter Jang | d675315 | 2015-10-02 09:23:13 -0700 | [diff] [blame] | 969 | private void showAllFields() { |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 970 | // Stop hiding empty editors and allow the user to enter values for all kinds now |
| 971 | for (int i = 0; i < mKindSectionViews.getChildCount(); i++) { |
| 972 | final CompactKindSectionView kindSectionView = |
| 973 | (CompactKindSectionView) mKindSectionViews.getChildAt(i); |
| 974 | kindSectionView.setHideWhenEmpty(false); |
| 975 | kindSectionView.updateEmptyEditors(/* shouldAnimate =*/ true); |
| 976 | } |
| 977 | mIsExpanded = true; |
Walter Jang | 79658e1 | 2015-09-24 10:36:26 -0700 | [diff] [blame] | 978 | |
Walter Jang | f5dfea4 | 2015-09-16 12:30:36 -0700 | [diff] [blame] | 979 | // Hide the more fields button |
| 980 | mMoreFields.setVisibility(View.GONE); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 983 | private static void vlog(String message) { |
| 984 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 985 | Log.v(TAG, message); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 986 | } |
| 987 | } |
Walter Jang | 363d3fd | 2015-09-16 10:29:07 -0700 | [diff] [blame] | 988 | |
| 989 | private static void wlog(String message) { |
| 990 | if (Log.isLoggable(TAG, Log.WARN)) { |
| 991 | Log.w(TAG, message); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | private static void elog(String message) { |
| 996 | Log.e(TAG, message); |
| 997 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 998 | } |