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