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