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