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; |
| 26 | import com.android.contacts.common.model.account.AccountType.EditField; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame^] | 27 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 28 | import com.android.contacts.common.model.dataitem.DataKind; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 29 | import com.android.contacts.common.util.MaterialColorMapUtils; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 30 | import com.android.contacts.editor.CompactContactEditorFragment.PhotoHandler; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 31 | |
| 32 | import android.content.Context; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 35 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 36 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 37 | import android.provider.ContactsContract.CommonDataKinds.Nickname; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 38 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 39 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 40 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
| 41 | import android.text.TextUtils; |
| 42 | import android.util.AttributeSet; |
| 43 | import android.util.Log; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame^] | 44 | import android.util.Pair; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 45 | import android.view.LayoutInflater; |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 46 | import android.view.View; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 47 | import android.view.ViewGroup; |
| 48 | import android.widget.LinearLayout; |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame^] | 49 | import android.widget.TextView; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 50 | |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.List; |
| 53 | |
| 54 | /** |
| 55 | * View to display information from multiple {@link RawContactDelta}s grouped together |
| 56 | * (e.g. all the phone numbers from a {@link com.android.contacts.common.model.Contact} together. |
| 57 | */ |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 58 | public class CompactRawContactsEditorView extends LinearLayout implements View.OnClickListener { |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 59 | |
| 60 | private static final String TAG = "CompactEditorView"; |
| 61 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Callbacks for hosts of {@link CompactRawContactsEditorView}s. |
| 64 | */ |
| 65 | public interface Listener { |
| 66 | |
| 67 | /** |
| 68 | * Invoked when the compact editor should be expanded to show all fields. |
| 69 | */ |
| 70 | public void onExpandEditor(); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Invoked when the structured name editor field has changed. |
| 74 | * |
| 75 | * @param rawContactId The raw contact ID from the underlying {@link RawContactDelta}. |
| 76 | * @param valuesDelta The values from the underlying {@link RawContactDelta}. |
| 77 | */ |
| 78 | public void onNameFieldChanged(long rawContactId, ValuesDelta valuesDelta); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Marks a name as super primary when it is changed. |
| 83 | * |
| 84 | * This is for the case when two or more raw contacts with names are joined where neither is |
| 85 | * marked as super primary. If the user hits back (which causes a save) after changing the |
| 86 | * name that was arbitrarily displayed, we want that to be the name that is used. |
| 87 | * |
| 88 | * Should only be set when a super primary name does not already exist since we only show |
| 89 | * one name field. |
| 90 | */ |
| 91 | static final class NameEditorListener implements Editor.EditorListener { |
| 92 | |
| 93 | private final ValuesDelta mValuesDelta; |
| 94 | private final long mRawContactId; |
| 95 | private final Listener mListener; |
| 96 | |
| 97 | public NameEditorListener(ValuesDelta valuesDelta, long rawContactId, |
| 98 | Listener listener) { |
| 99 | mValuesDelta = valuesDelta; |
| 100 | mRawContactId = rawContactId; |
| 101 | mListener = listener; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onRequest(int request) { |
| 106 | if (request == Editor.EditorListener.FIELD_CHANGED) { |
| 107 | mValuesDelta.setSuperPrimary(true); |
| 108 | if (mListener != null) { |
| 109 | mListener.onNameFieldChanged(mRawContactId, mValuesDelta); |
| 110 | } |
| 111 | } else if (request == Editor.EditorListener.FIELD_TURNED_EMPTY) { |
| 112 | mValuesDelta.setSuperPrimary(false); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void onDeleteRequested(Editor editor) { |
| 118 | } |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private Listener mListener; |
| 122 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 123 | private AccountTypeManager mAccountTypeManager; |
| 124 | private LayoutInflater mLayoutInflater; |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 125 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 126 | private ViewIdGenerator mViewIdGenerator; |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 127 | private MaterialColorMapUtils.MaterialPalette mMaterialPalette; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 128 | |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame^] | 129 | private View mAccountContainer; |
| 130 | private TextView mAccountTypeView; |
| 131 | private TextView mAccountNameView; |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 132 | private CompactPhotoEditorView mPhoto; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 133 | private ViewGroup mNames; |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 134 | private ViewGroup mPhoneticNames; |
| 135 | private ViewGroup mNicknames; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 136 | private ViewGroup mPhoneNumbers; |
| 137 | private ViewGroup mEmails; |
| 138 | private ViewGroup mOther; |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 139 | private View mMoreFields; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 140 | |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 141 | // The ValuesDelta for the non super primary name that was displayed to the user. |
| 142 | private ValuesDelta mNameValuesDelta; |
| 143 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 144 | private long mPhotoRawContactId; |
| 145 | |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 146 | private StructuredNameEditorView mDefaultNameEditorView; |
| 147 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 148 | public CompactRawContactsEditorView(Context context) { |
| 149 | super(context); |
| 150 | } |
| 151 | |
| 152 | public CompactRawContactsEditorView(Context context, AttributeSet attrs) { |
| 153 | super(context, attrs); |
| 154 | } |
| 155 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Sets the receiver for {@link CompactRawContactsEditorView} callbacks. |
| 158 | */ |
| 159 | public void setListener(Listener listener) { |
| 160 | mListener = listener; |
| 161 | } |
| 162 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 163 | @Override |
| 164 | protected void onFinishInflate() { |
| 165 | super.onFinishInflate(); |
| 166 | |
| 167 | mAccountTypeManager = AccountTypeManager.getInstance(getContext()); |
| 168 | mLayoutInflater = (LayoutInflater) |
| 169 | getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 170 | |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame^] | 171 | mAccountContainer = findViewById(R.id.account_container); |
| 172 | mAccountTypeView = (TextView) findViewById(R.id.account_type); |
| 173 | mAccountNameView = (TextView) findViewById(R.id.account_name); |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 174 | mPhoto = (CompactPhotoEditorView) findViewById(R.id.photo_editor); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 175 | mNames = (LinearLayout) findViewById(R.id.names); |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 176 | mPhoneticNames = (LinearLayout) findViewById(R.id.phonetic_names); |
| 177 | mNicknames = (LinearLayout) findViewById(R.id.nicknames); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 178 | mPhoneNumbers = (LinearLayout) findViewById(R.id.phone_numbers); |
| 179 | mEmails = (LinearLayout) findViewById(R.id.emails); |
| 180 | mOther = (LinearLayout) findViewById(R.id.other); |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 181 | mMoreFields = findViewById(R.id.more_fields); |
| 182 | mMoreFields.setOnClickListener(this); |
| 183 | } |
| 184 | |
Walter Jang | b6ca272 | 2015-02-20 11:10:25 -0800 | [diff] [blame] | 185 | @Override |
| 186 | public void onClick(View view) { |
| 187 | if (view.getId() == R.id.more_fields && mListener != null ) { |
| 188 | mListener.onExpandEditor(); |
| 189 | } |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void setEnabled(boolean enabled) { |
| 194 | super.setEnabled(enabled); |
| 195 | setEnabled(enabled, mNames); |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 196 | setEnabled(enabled, mPhoneticNames); |
| 197 | setEnabled(enabled, mNicknames); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 198 | setEnabled(enabled, mPhoneNumbers); |
| 199 | setEnabled(enabled, mEmails); |
| 200 | setEnabled(enabled, mOther); |
| 201 | } |
| 202 | |
| 203 | private void setEnabled(boolean enabled, ViewGroup viewGroup) { |
| 204 | if (viewGroup != null) { |
| 205 | final int childCount = viewGroup.getChildCount(); |
| 206 | for (int i = 0; i < childCount; i++) { |
| 207 | viewGroup.getChildAt(i).setEnabled(enabled); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 212 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 213 | * Pass through to {@link CompactPhotoEditorView#setPhotoHandler}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 214 | */ |
| 215 | public void setPhotoHandler(PhotoHandler photoHandler) { |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 216 | mPhoto.setPhotoHandler(photoHandler); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 220 | * Pass through to {@link CompactPhotoEditorView#setPhoto}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 221 | */ |
| 222 | public void setPhoto(Bitmap bitmap) { |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 223 | mPhoto.setPhoto(bitmap); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
Walter Jang | 41b3ea1 | 2015-03-09 17:30:06 -0700 | [diff] [blame] | 227 | * Pass through to {@link CompactPhotoEditorView#setFullSizedPhoto(Uri)}. |
| 228 | */ |
| 229 | public void setFullSizePhoto(Uri photoUri) { |
| 230 | mPhoto.setFullSizedPhoto(photoUri); |
| 231 | } |
| 232 | |
| 233 | /** |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 234 | * Pass through to {@link CompactPhotoEditorView#isWritablePhotoSet}. |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 235 | */ |
| 236 | public boolean isWritablePhotoSet() { |
Walter Jang | a5e4bb2 | 2015-02-24 13:08:16 -0800 | [diff] [blame] | 237 | return mPhoto.isWritablePhotoSet(); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 241 | * Get the raw contact ID for the CompactHeaderView photo. |
| 242 | */ |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 243 | public long getPhotoRawContactId() { |
| 244 | return mPhotoRawContactId; |
| 245 | } |
| 246 | |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 247 | public StructuredNameEditorView getDefaultNameEditorView() { |
| 248 | return mDefaultNameEditorView; |
| 249 | } |
| 250 | |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 251 | public StructuredNameEditorView getStructuredNameEditorView() { |
| 252 | // We only ever show one StructuredName |
| 253 | return mNames.getChildCount() == 0 |
| 254 | ? null : (StructuredNameEditorView) mNames.getChildAt(0); |
| 255 | } |
| 256 | |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 257 | public PhoneticNameEditorView getFirstPhoneticNameEditorView() { |
| 258 | // There should only ever be one phonetic name |
| 259 | return mPhoneticNames.getChildCount() == 0 |
| 260 | ? null : (PhoneticNameEditorView) mPhoneticNames.getChildAt(0); |
| 261 | } |
| 262 | |
Walter Jang | d35e5ef | 2015-02-24 09:18:16 -0800 | [diff] [blame] | 263 | public View getAggregationAnchorView() { |
| 264 | // Since there is only one structured name we can just return it as the anchor for |
| 265 | // the aggregation suggestions popup |
| 266 | if (mNames.getChildCount() == 0) { |
| 267 | return null; |
| 268 | } |
| 269 | return mNames.getChildAt(0).findViewById(R.id.anchor_view); |
| 270 | } |
| 271 | |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 272 | /** |
| 273 | * @param readOnlyDisplayName The display name to set on the new raw contact created in order |
| 274 | * to edit a read-only contact. |
| 275 | */ |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 276 | public void setState(RawContactDeltaList rawContactDeltas, |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 277 | MaterialColorMapUtils.MaterialPalette materialPalette, ViewIdGenerator viewIdGenerator, |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame^] | 278 | long photoId, long nameId, String readOnlyDisplayName, boolean hasNewContact, |
| 279 | boolean isUserProfile) { |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 280 | mNames.removeAllViews(); |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 281 | mPhoneticNames.removeAllViews(); |
| 282 | mNicknames.removeAllViews(); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 283 | mPhoneNumbers.removeAllViews(); |
| 284 | mEmails.removeAllViews(); |
| 285 | mOther.removeAllViews(); |
| 286 | |
| 287 | if (rawContactDeltas == null || rawContactDeltas.isEmpty()) { |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | mViewIdGenerator = viewIdGenerator; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 292 | setId(mViewIdGenerator.getId(rawContactDeltas.get(0), /* dataKind =*/ null, |
| 293 | /* valuesDelta =*/ null, ViewIdGenerator.NO_VIEW_INDEX)); |
Walter Jang | f46abd8 | 2015-02-20 16:52:04 -0800 | [diff] [blame] | 294 | mMaterialPalette = materialPalette; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 295 | |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 296 | vlog("Setting compact editor state from " + rawContactDeltas); |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame^] | 297 | addAccountInfo(rawContactDeltas, hasNewContact, readOnlyDisplayName, isUserProfile); |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 298 | addPhotoView(rawContactDeltas, viewIdGenerator, photoId, readOnlyDisplayName); |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 299 | addStructuredNameView(rawContactDeltas, nameId, readOnlyDisplayName); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 300 | addEditorViews(rawContactDeltas); |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 301 | removeExtraEmptyTextFields(mPhoneNumbers); |
| 302 | removeExtraEmptyTextFields(mEmails); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Walter Jang | 2d3f31c | 2015-06-18 23:15:31 -0700 | [diff] [blame^] | 305 | private void addAccountInfo(RawContactDeltaList rawContactDeltas, boolean hasNewContact, |
| 306 | String readOnlyDisplayName, boolean isUserProfile) { |
| 307 | // Only show account info for inserts and first time edits of read-only accounts |
| 308 | if (!hasNewContact && TextUtils.isEmpty(readOnlyDisplayName)) { |
| 309 | vlog("Account info hidden"); |
| 310 | mAccountContainer.setVisibility(View.GONE); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | // Get the default account |
| 315 | final AccountWithDataSet defaultAccountWithDataSet = |
| 316 | ContactEditorUtils.getInstance(getContext()).getDefaultAccount(); |
| 317 | if (defaultAccountWithDataSet == null) { |
| 318 | vlog("Account info hidden because default account not set"); |
| 319 | mAccountContainer.setVisibility(View.GONE); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | // We can assume the first writable raw contact is the newly created one because |
| 324 | // inserts have a delta list of size 1 and read-only contacts are should be of size 2 |
| 325 | RawContactDelta defaultAccountRawContactDelta = null; |
| 326 | AccountType accountType = null; |
| 327 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 328 | if (!rawContactDelta.isVisible()) continue; |
| 329 | accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 330 | if (accountType != null && accountType.areContactsWritable()) { |
| 331 | defaultAccountRawContactDelta = rawContactDelta; |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | if (defaultAccountRawContactDelta == null) { |
| 336 | vlog("Account info hidden because no raw contact delta matched"); |
| 337 | mAccountContainer.setVisibility(View.GONE); |
| 338 | return; |
| 339 | } |
| 340 | if (accountType == null) { |
| 341 | vlog("Account info hidden because no account type returned from raw contact delta"); |
| 342 | mAccountContainer.setVisibility(View.GONE); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | // Get the account information for the default account RawContactDelta |
| 347 | final Pair<String,String> accountInfo = EditorUiUtils.getAccountInfo(getContext(), |
| 348 | isUserProfile, defaultAccountRawContactDelta.getAccountName(), |
| 349 | accountType.getDisplayLabel(getContext())); |
| 350 | |
| 351 | // Set the account information already |
| 352 | if (accountInfo == null) { |
| 353 | vlog("Account info hidden because no account info could be composed"); |
| 354 | mAccountContainer.setVisibility(View.GONE); |
| 355 | return; |
| 356 | } |
| 357 | vlog("Account info loaded"); |
| 358 | if (accountInfo.first == null) { |
| 359 | mAccountNameView.setVisibility(View.GONE); |
| 360 | } else { |
| 361 | mAccountNameView.setVisibility(View.VISIBLE); |
| 362 | mAccountNameView.setText(accountInfo.first); |
| 363 | } |
| 364 | mAccountTypeView.setText(accountInfo.second); |
| 365 | |
| 366 | mAccountContainer.setContentDescription(EditorUiUtils.getAccountInfoContentDescription( |
| 367 | accountInfo.first, accountInfo.second)); |
| 368 | } |
| 369 | |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 370 | private void addPhotoView(RawContactDeltaList rawContactDeltas, |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 371 | ViewIdGenerator viewIdGenerator, long photoId, String readOnlyDisplayName) { |
| 372 | // If we're editing a read-only contact, the display name from the read-only |
| 373 | // contact is non empty and we can use it determine whether to back the photo editor with |
| 374 | // the empty new raw contact delta. See go/editing-read-only-contacts |
| 375 | final boolean readOnlyContact = !TextUtils.isEmpty(readOnlyDisplayName); |
| 376 | if (readOnlyContact) { |
| 377 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 378 | if (!rawContactDelta.isVisible()) continue; |
| 379 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 380 | |
| 381 | // Make sure we have a photo |
| 382 | RawContactModifier.ensureKindExists( |
| 383 | rawContactDelta, accountType, Photo.CONTENT_ITEM_TYPE); |
| 384 | |
| 385 | final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE); |
| 386 | if (accountType.areContactsWritable()) { |
| 387 | for (ValuesDelta valuesDelta : rawContactDelta.getMimeEntries( |
| 388 | Photo.CONTENT_ITEM_TYPE)) { |
| 389 | if (valuesDelta != null) { |
| 390 | // Break the loop but don't return because we need to keep going to |
| 391 | // in order to show the photo from the read-only contact. |
| 392 | mPhotoRawContactId = rawContactDelta.getRawContactId(); |
| 393 | mPhoto.setValues(dataKind, valuesDelta, rawContactDelta, |
| 394 | /* readOnly =*/ false, mMaterialPalette, viewIdGenerator); |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
Walter Jang | ac679af | 2015-06-01 12:17:06 -0700 | [diff] [blame] | 402 | // Look for a match for the photo ID that was passed in |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 403 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
Walter Jang | 3b21027 | 2015-03-04 11:34:11 -0800 | [diff] [blame] | 404 | if (!rawContactDelta.isVisible()) continue; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 405 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 406 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 407 | // Make sure we have a photo |
| 408 | RawContactModifier.ensureKindExists( |
| 409 | rawContactDelta, accountType, Photo.CONTENT_ITEM_TYPE); |
| 410 | |
| 411 | final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE); |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 412 | if (dataKind != null && dataKind.editable) { |
Walter Jang | ac679af | 2015-06-01 12:17:06 -0700 | [diff] [blame] | 413 | for (ValuesDelta valuesDelta |
| 414 | : rawContactDelta.getMimeEntries(Photo.CONTENT_ITEM_TYPE)) { |
| 415 | if (valuesDelta != null && valuesDelta.getId() != null |
| 416 | && valuesDelta.getId().equals(photoId)) { |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 417 | if (readOnlyContact) { |
| 418 | mPhoto.setPhoto(valuesDelta); |
| 419 | } else { |
| 420 | mPhotoRawContactId = rawContactDelta.getRawContactId(); |
| 421 | mPhoto.setValues(dataKind, valuesDelta, rawContactDelta, |
| 422 | !accountType.areContactsWritable(), |
| 423 | mMaterialPalette, viewIdGenerator); |
| 424 | } |
Walter Jang | ac679af | 2015-06-01 12:17:06 -0700 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // Look for a non-empty super primary photo |
| 432 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 433 | if (!rawContactDelta.isVisible()) continue; |
| 434 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 435 | final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE); |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 436 | if (dataKind != null && dataKind.editable) { |
Walter Jang | 3b21027 | 2015-03-04 11:34:11 -0800 | [diff] [blame] | 437 | final ValuesDelta valuesDelta = getNonEmptySuperPrimaryValuesDeltas( |
| 438 | rawContactDelta, Photo.CONTENT_ITEM_TYPE, dataKind); |
| 439 | if (valuesDelta != null) { |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 440 | if (readOnlyContact) { |
| 441 | mPhoto.setPhoto(valuesDelta); |
| 442 | } else { |
| 443 | mPhotoRawContactId = rawContactDelta.getRawContactId(); |
| 444 | mPhoto.setValues(dataKind, valuesDelta, rawContactDelta, |
| 445 | !accountType.areContactsWritable(), mMaterialPalette, |
| 446 | viewIdGenerator); |
| 447 | } |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | } |
| 451 | } |
Walter Jang | 3b21027 | 2015-03-04 11:34:11 -0800 | [diff] [blame] | 452 | // We didn't find a non-empty super primary photo, use the first non-empty one |
| 453 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 454 | if (!rawContactDelta.isVisible()) continue; |
| 455 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 456 | final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE); |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 457 | if (dataKind != null && dataKind.editable) { |
Walter Jang | 3b21027 | 2015-03-04 11:34:11 -0800 | [diff] [blame] | 458 | final List<ValuesDelta> valuesDeltas = getNonEmptyValuesDeltas( |
| 459 | rawContactDelta, Photo.CONTENT_ITEM_TYPE, dataKind); |
| 460 | if (valuesDeltas != null && !valuesDeltas.isEmpty()) { |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 461 | if (readOnlyContact) { |
| 462 | mPhoto.setPhoto(valuesDeltas.get(0)); |
| 463 | } else { |
| 464 | mPhotoRawContactId = rawContactDelta.getRawContactId(); |
| 465 | mPhoto.setValues(dataKind, valuesDeltas.get(0), rawContactDelta, |
| 466 | !accountType.areContactsWritable(), mMaterialPalette, |
| 467 | viewIdGenerator); |
| 468 | } |
Walter Jang | 3b21027 | 2015-03-04 11:34:11 -0800 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | // No suitable non-empty photo |
| 474 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 475 | if (!rawContactDelta.isVisible()) continue; |
| 476 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 477 | final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE); |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 478 | if (dataKind != null && dataKind.editable) { |
Walter Jang | 3b21027 | 2015-03-04 11:34:11 -0800 | [diff] [blame] | 479 | final ValuesDelta valuesDelta = rawContactDelta.getSuperPrimaryEntry( |
| 480 | dataKind.mimeType, /* forceSelection =*/ true); |
| 481 | if (valuesDelta != null) { |
Walter Jang | fa127a1 | 2015-06-18 09:48:18 -0700 | [diff] [blame] | 482 | if (readOnlyContact) { |
| 483 | mPhoto.setPhoto(valuesDelta); |
| 484 | } else { |
| 485 | mPhotoRawContactId = rawContactDelta.getRawContactId(); |
| 486 | mPhoto.setValues(dataKind, valuesDelta, rawContactDelta, |
| 487 | !accountType.areContactsWritable(), mMaterialPalette, |
| 488 | viewIdGenerator); |
| 489 | } |
Walter Jang | 3b21027 | 2015-03-04 11:34:11 -0800 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | // Should not happen since we ensure the kind exists but if we unexpectedly get here |
| 495 | // we must remove the photo section so that it does not take up the entire view |
| 496 | mPhoto.setVisibility(View.GONE); |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Walter Jang | 06f73a1 | 2015-06-17 11:15:48 -0700 | [diff] [blame] | 499 | private void addStructuredNameView(RawContactDeltaList rawContactDeltas, long nameId, |
| 500 | String readOnlyDisplayName) { |
| 501 | // If we're editing a read-only contact we want to display the name from the read-only |
| 502 | // contact in a structured name editor backed by the new raw contact that was created. |
| 503 | // The new raw contact is writable and merging it with the read-only contact allows us |
| 504 | // to edit the read-only contact. See go/editing-read-only-contacts |
| 505 | if (!TextUtils.isEmpty(readOnlyDisplayName)) { |
| 506 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 507 | if (!rawContactDelta.isVisible()) continue; |
| 508 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 509 | |
| 510 | // Make sure we have a structured name |
| 511 | RawContactModifier.ensureKindExists( |
| 512 | rawContactDelta, accountType, StructuredName.CONTENT_ITEM_TYPE); |
| 513 | |
| 514 | if (accountType.areContactsWritable()) { |
| 515 | for (ValuesDelta valuesDelta : rawContactDelta.getMimeEntries( |
| 516 | StructuredName.CONTENT_ITEM_TYPE)) { |
| 517 | if (valuesDelta != null) { |
| 518 | mNameValuesDelta = valuesDelta; |
| 519 | final NameEditorListener nameEditorListener = new NameEditorListener( |
| 520 | mNameValuesDelta, rawContactDelta.getRawContactId(), mListener); |
| 521 | final StructuredNameEditorView nameEditorView = |
| 522 | inflateStructuredNameEditorView(mNames, accountType, |
| 523 | mNameValuesDelta, rawContactDelta, nameEditorListener, |
| 524 | !accountType.areContactsWritable()); |
| 525 | nameEditorView.setDisplayName(readOnlyDisplayName); |
| 526 | mNames.addView(nameEditorView); |
| 527 | mDefaultNameEditorView = nameEditorView; |
| 528 | return; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
Walter Jang | 65d3520 | 2015-06-16 13:08:34 -0700 | [diff] [blame] | 535 | // Look for a match for the name ID that was passed in |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 536 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 537 | if (!rawContactDelta.isVisible()) continue; |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 538 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 539 | |
| 540 | // Make sure we have a structured name |
| 541 | RawContactModifier.ensureKindExists( |
| 542 | rawContactDelta, accountType, StructuredName.CONTENT_ITEM_TYPE); |
| 543 | |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 544 | // Note use of pseudo mime type to get the DataKind and StructuredName to get value |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 545 | final DataKind dataKind = accountType.getKindForMimetype( |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 546 | DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME); |
| 547 | if (dataKind == null || !dataKind.editable) continue; |
| 548 | |
Walter Jang | 398cd4b | 2015-06-16 11:17:53 -0700 | [diff] [blame] | 549 | for (ValuesDelta valuesDelta : rawContactDelta.getMimeEntries( |
| 550 | StructuredName.CONTENT_ITEM_TYPE)) { |
| 551 | if (valuesDelta != null && valuesDelta.getId() != null |
| 552 | && valuesDelta.getId().equals(nameId)) { |
| 553 | mNameValuesDelta = valuesDelta; |
| 554 | final NameEditorListener nameEditorListener = new NameEditorListener( |
| 555 | mNameValuesDelta, rawContactDelta.getRawContactId(), mListener); |
| 556 | mNames.addView(inflateStructuredNameEditorView(mNames, accountType, |
Walter Jang | 65d3520 | 2015-06-16 13:08:34 -0700 | [diff] [blame] | 557 | mNameValuesDelta, rawContactDelta, nameEditorListener, |
| 558 | !accountType.areContactsWritable())); |
Walter Jang | 398cd4b | 2015-06-16 11:17:53 -0700 | [diff] [blame] | 559 | return; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | // Look for a super primary name |
| 564 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 565 | if (!rawContactDelta.isVisible()) continue; |
| 566 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 567 | |
| 568 | final DataKind dataKind = accountType.getKindForMimetype( |
| 569 | DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME); |
| 570 | if (dataKind == null || !dataKind.editable) continue; |
| 571 | |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 572 | final ValuesDelta superPrimaryValuesDelta = getNonEmptySuperPrimaryValuesDeltas( |
| 573 | rawContactDelta, StructuredName.CONTENT_ITEM_TYPE, dataKind); |
| 574 | if (superPrimaryValuesDelta != null) { |
| 575 | // Our first preference is for a non-empty super primary name |
Walter Jang | a26490b | 2015-05-27 13:08:24 -0700 | [diff] [blame] | 576 | final NameEditorListener nameEditorListener = new NameEditorListener( |
| 577 | superPrimaryValuesDelta, rawContactDelta.getRawContactId(), mListener); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 578 | mNames.addView(inflateStructuredNameEditorView(mNames, accountType, |
Walter Jang | 65d3520 | 2015-06-16 13:08:34 -0700 | [diff] [blame] | 579 | superPrimaryValuesDelta, rawContactDelta, nameEditorListener, |
| 580 | !accountType.areContactsWritable())); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 581 | return; |
| 582 | } |
| 583 | } |
| 584 | // We didn't find a super primary name |
| 585 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 586 | if (!rawContactDelta.isVisible()) continue; |
| 587 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 588 | |
| 589 | final DataKind dataKind = accountType.getKindForMimetype( |
| 590 | DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME); |
| 591 | if (dataKind == null || !dataKind.editable) continue; |
| 592 | |
| 593 | final List<ValuesDelta> nonEmptyValuesDeltas = getNonEmptyValuesDeltas( |
| 594 | rawContactDelta, StructuredName.CONTENT_ITEM_TYPE, dataKind); |
| 595 | if (nonEmptyValuesDeltas != null && !nonEmptyValuesDeltas.isEmpty()) { |
Walter Jang | 65d3520 | 2015-06-16 13:08:34 -0700 | [diff] [blame] | 596 | // Take the first non-empty name |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 597 | mNameValuesDelta = nonEmptyValuesDeltas.get(0); |
| 598 | final NameEditorListener nameEditorListener = new NameEditorListener( |
| 599 | mNameValuesDelta, rawContactDelta.getRawContactId(), mListener); |
| 600 | mNames.addView(inflateStructuredNameEditorView(mNames, accountType, |
Walter Jang | 65d3520 | 2015-06-16 13:08:34 -0700 | [diff] [blame] | 601 | mNameValuesDelta, rawContactDelta, nameEditorListener, |
| 602 | !accountType.areContactsWritable())); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 603 | return; |
| 604 | } |
| 605 | } |
| 606 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
| 607 | if (!rawContactDelta.isVisible()) continue; |
| 608 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 609 | |
| 610 | final DataKind dataKind = accountType.getKindForMimetype( |
| 611 | DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME); |
| 612 | if (dataKind == null || !dataKind.editable) continue; |
| 613 | |
| 614 | // Fall back to the first entry |
| 615 | final ArrayList<ValuesDelta> valuesDeltas = rawContactDelta.getMimeEntries( |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 616 | StructuredName.CONTENT_ITEM_TYPE); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 617 | if (valuesDeltas != null && !valuesDeltas.isEmpty()) { |
| 618 | mNameValuesDelta = valuesDeltas.get(0); |
| 619 | final NameEditorListener nameEditorListener = new NameEditorListener( |
| 620 | mNameValuesDelta, rawContactDelta.getRawContactId(), mListener); |
| 621 | mNames.addView(inflateStructuredNameEditorView(mNames, accountType, |
Walter Jang | 65d3520 | 2015-06-16 13:08:34 -0700 | [diff] [blame] | 622 | mNameValuesDelta, rawContactDelta, nameEditorListener, |
| 623 | !accountType.areContactsWritable())); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 624 | return; |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 629 | private void addEditorViews(RawContactDeltaList rawContactDeltas) { |
| 630 | for (RawContactDelta rawContactDelta : rawContactDeltas) { |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 631 | if (!rawContactDelta.isVisible()) continue; |
Walter Jang | 3efae4a | 2015-02-18 11:12:00 -0800 | [diff] [blame] | 632 | final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager); |
| 633 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 634 | for (DataKind dataKind : accountType.getSortedDataKinds()) { |
Walter Jang | ab50e6f | 2015-06-15 08:57:22 -0700 | [diff] [blame] | 635 | if (!dataKind.editable) continue; |
| 636 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 637 | final String mimeType = dataKind.mimeType; |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 638 | vlog(mimeType + " " + dataKind.fieldList.size() + " field(s)"); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 639 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType) |
Walter Jang | 1044645 | 2015-02-20 13:51:16 -0800 | [diff] [blame] | 640 | || StructuredName.CONTENT_ITEM_TYPE.equals(mimeType) |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 641 | || GroupMembership.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 642 | // Photos and structured names are handled separately and |
| 643 | // group membership is not supported |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 644 | continue; |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 645 | } else if (DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) { |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 646 | // Only add phonetic names if there is a non-empty one. Note the use of |
| 647 | // StructuredName mimeType below, even though we matched a pseudo mime type. |
Walter Jang | 3a37a1a | 2015-03-04 07:41:32 -0800 | [diff] [blame] | 648 | final ValuesDelta valuesDelta = rawContactDelta.getSuperPrimaryEntry( |
| 649 | StructuredName.CONTENT_ITEM_TYPE, /* forceSelection =*/ true); |
| 650 | if (hasNonEmptyValue(dataKind, valuesDelta)) { |
| 651 | mPhoneticNames.addView(inflatePhoneticNameEditorView( |
| 652 | mPhoneticNames, accountType, valuesDelta, rawContactDelta)); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 653 | } |
| 654 | } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 655 | // Only add nicknames if there is a non-empty one |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 656 | if (hasNonEmptyValuesDelta(rawContactDelta, mimeType, dataKind)) { |
Walter Jang | 28c98b7 | 2015-06-01 15:56:37 -0700 | [diff] [blame] | 657 | mNicknames.addView(inflateKindSectionView(mNicknames, dataKind, |
Walter Jang | 88b0a8c | 2015-06-08 13:15:56 -0700 | [diff] [blame] | 658 | rawContactDelta)); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 659 | } |
| 660 | } else if (Phone.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 28c98b7 | 2015-06-01 15:56:37 -0700 | [diff] [blame] | 661 | mPhoneNumbers.addView(inflateKindSectionView(mPhoneNumbers, dataKind, |
Walter Jang | 88b0a8c | 2015-06-08 13:15:56 -0700 | [diff] [blame] | 662 | rawContactDelta)); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 663 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Walter Jang | 88b0a8c | 2015-06-08 13:15:56 -0700 | [diff] [blame] | 664 | mEmails.addView(inflateKindSectionView(mEmails, dataKind, rawContactDelta)); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 665 | } else if (hasNonEmptyValuesDelta(rawContactDelta, mimeType, dataKind)) { |
Walter Jang | 88b0a8c | 2015-06-08 13:15:56 -0700 | [diff] [blame] | 666 | mOther.addView(inflateKindSectionView(mOther, dataKind, rawContactDelta)); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 672 | // TODO: avoid inflating extra views and deleting them |
| 673 | private void removeExtraEmptyTextFields(ViewGroup viewGroup) { |
| 674 | // If there is one (or less) editors, leave it whether it is empty or not |
| 675 | if (viewGroup.getChildCount() <= 1) { |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 676 | return; |
| 677 | } |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 678 | // Determine if there are any non-empty editors |
| 679 | boolean hasAtLeastOneNonEmptyEditorView = false; |
| 680 | for (int i = 0; i < viewGroup.getChildCount(); i++) { |
| 681 | if (!isEmptyEditorView(viewGroup.getChildAt(i))) { |
| 682 | hasAtLeastOneNonEmptyEditorView = true; |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 683 | break; |
| 684 | } |
| 685 | } |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 686 | if (hasAtLeastOneNonEmptyEditorView) { |
| 687 | // There is at least one non-empty editor, remove all the empty ones |
| 688 | for (int i = 0; i < viewGroup.getChildCount(); i++) { |
| 689 | if (isEmptyEditorView(viewGroup.getChildAt(i))) { |
| 690 | viewGroup.getChildAt(i).setVisibility(View.GONE); |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } else { |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 694 | // There is no non-empty editor, keep the first empty view and remove the rest |
| 695 | for (int i = 1; i < viewGroup.getChildCount(); i++) { |
| 696 | viewGroup.getChildAt(i).setVisibility(View.GONE); |
Walter Jang | b1c8762 | 2015-02-13 17:51:38 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
Walter Jang | e720fde | 2015-02-17 10:54:14 -0800 | [diff] [blame] | 701 | private static boolean isEmptyEditorView(View view) { |
| 702 | if (view instanceof TextFieldsEditorView) { |
| 703 | final TextFieldsEditorView textFieldsEditorView = (TextFieldsEditorView) view; |
| 704 | return textFieldsEditorView.isEmpty(); |
| 705 | } |
| 706 | if (view instanceof KindSectionView) { |
| 707 | final KindSectionView kindSectionView = (KindSectionView) view; |
| 708 | return kindSectionView.hasEmptyEditor(); |
| 709 | } |
| 710 | return false; |
| 711 | } |
| 712 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 713 | private static boolean hasNonEmptyValuesDelta(RawContactDelta rawContactDelta, |
| 714 | String mimeType, DataKind dataKind) { |
| 715 | return !getNonEmptyValuesDeltas(rawContactDelta, mimeType, dataKind).isEmpty(); |
| 716 | } |
| 717 | |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 718 | private static ValuesDelta getNonEmptySuperPrimaryValuesDeltas(RawContactDelta rawContactDelta, |
| 719 | String mimeType, DataKind dataKind) { |
| 720 | for (ValuesDelta valuesDelta : getNonEmptyValuesDeltas( |
| 721 | rawContactDelta, mimeType, dataKind)) { |
| 722 | if (valuesDelta.isSuperPrimary()) { |
| 723 | return valuesDelta; |
| 724 | } |
| 725 | } |
| 726 | return null; |
| 727 | } |
| 728 | |
| 729 | static List<ValuesDelta> getNonEmptyValuesDeltas(RawContactDelta rawContactDelta, |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 730 | String mimeType, DataKind dataKind) { |
| 731 | final List<ValuesDelta> result = new ArrayList<>(); |
| 732 | if (rawContactDelta == null) { |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 733 | vlog("Null RawContactDelta"); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 734 | return result; |
| 735 | } |
| 736 | if (!rawContactDelta.hasMimeEntries(mimeType)) { |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 737 | vlog("No ValueDeltas"); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 738 | return result; |
| 739 | } |
| 740 | for (ValuesDelta valuesDelta : rawContactDelta.getMimeEntries(mimeType)) { |
Walter Jang | 3a37a1a | 2015-03-04 07:41:32 -0800 | [diff] [blame] | 741 | if (hasNonEmptyValue(dataKind, valuesDelta)) { |
| 742 | result.add(valuesDelta); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | return result; |
| 746 | } |
| 747 | |
Walter Jang | 3a37a1a | 2015-03-04 07:41:32 -0800 | [diff] [blame] | 748 | private static boolean hasNonEmptyValue(DataKind dataKind, ValuesDelta valuesDelta) { |
| 749 | if (valuesDelta == null) { |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 750 | vlog("Null valuesDelta"); |
Walter Jang | 3a37a1a | 2015-03-04 07:41:32 -0800 | [diff] [blame] | 751 | return false; |
| 752 | } |
| 753 | for (EditField editField : dataKind.fieldList) { |
| 754 | final String column = editField.column; |
| 755 | final String value = valuesDelta == null ? null : valuesDelta.getAsString(column); |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 756 | vlog("Field " + column + " empty=" + TextUtils.isEmpty(value) + " value=" + value); |
Walter Jang | 3a37a1a | 2015-03-04 07:41:32 -0800 | [diff] [blame] | 757 | if (!TextUtils.isEmpty(value)) { |
| 758 | return true; |
| 759 | } |
| 760 | } |
| 761 | return false; |
| 762 | } |
| 763 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 764 | private StructuredNameEditorView inflateStructuredNameEditorView(ViewGroup viewGroup, |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 765 | AccountType accountType, ValuesDelta valuesDelta, RawContactDelta rawContactDelta, |
Walter Jang | 65d3520 | 2015-06-16 13:08:34 -0700 | [diff] [blame] | 766 | NameEditorListener nameEditorListener, boolean readOnly) { |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 767 | final StructuredNameEditorView result = (StructuredNameEditorView) mLayoutInflater.inflate( |
| 768 | R.layout.structured_name_editor_view, viewGroup, /* attachToRoot =*/ false); |
Walter Jang | 151f3e6 | 2015-02-26 15:29:40 -0800 | [diff] [blame] | 769 | if (nameEditorListener != null) { |
| 770 | result.setEditorListener(nameEditorListener); |
| 771 | } |
Walter Jang | 3a37a1a | 2015-03-04 07:41:32 -0800 | [diff] [blame] | 772 | result.setDeletable(false); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 773 | result.setValues( |
| 774 | accountType.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME), |
| 775 | valuesDelta, |
| 776 | rawContactDelta, |
Walter Jang | 65d3520 | 2015-06-16 13:08:34 -0700 | [diff] [blame] | 777 | readOnly, |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 778 | mViewIdGenerator); |
| 779 | return result; |
| 780 | } |
| 781 | |
| 782 | private PhoneticNameEditorView inflatePhoneticNameEditorView(ViewGroup viewGroup, |
| 783 | AccountType accountType, ValuesDelta valuesDelta, RawContactDelta rawContactDelta) { |
| 784 | final PhoneticNameEditorView result = (PhoneticNameEditorView) mLayoutInflater.inflate( |
| 785 | R.layout.phonetic_name_editor_view, viewGroup, /* attachToRoot =*/ false); |
Walter Jang | 3a37a1a | 2015-03-04 07:41:32 -0800 | [diff] [blame] | 786 | result.setDeletable(false); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 787 | result.setValues( |
| 788 | accountType.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME), |
| 789 | valuesDelta, |
| 790 | rawContactDelta, |
| 791 | /* readOnly =*/ false, |
| 792 | mViewIdGenerator); |
| 793 | return result; |
| 794 | } |
| 795 | |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 796 | private KindSectionView inflateKindSectionView(ViewGroup viewGroup, DataKind dataKind, |
Walter Jang | 88b0a8c | 2015-06-08 13:15:56 -0700 | [diff] [blame] | 797 | RawContactDelta rawContactDelta) { |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 798 | final KindSectionView result = (KindSectionView) mLayoutInflater.inflate( |
| 799 | R.layout.item_kind_section, viewGroup, /* attachToRoot =*/ false); |
| 800 | result.setState( |
| 801 | dataKind, |
| 802 | rawContactDelta, |
| 803 | /* readOnly =*/ false, |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 804 | mViewIdGenerator); |
| 805 | return result; |
| 806 | } |
| 807 | |
Walter Jang | bf63a6d | 2015-05-05 09:14:35 -0700 | [diff] [blame] | 808 | private static void vlog(String message) { |
| 809 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 810 | Log.v(TAG, message); |
Walter Jang | cab3dce | 2015-02-09 17:48:03 -0800 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | } |