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