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