Merge "Tweaks requested for multi select"
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index cb605c6..81d475e 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -276,27 +276,64 @@
     private void addPhotoView(RawContactDeltaList rawContactDeltas,
             ViewIdGenerator viewIdGenerator) {
         for (RawContactDelta rawContactDelta : rawContactDeltas) {
-            if (!rawContactDelta.isVisible()) {
-                continue;
-            }
+            if (!rawContactDelta.isVisible()) continue;
             final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
 
             // Make sure we have a photo
             RawContactModifier.ensureKindExists(
                     rawContactDelta, accountType, Photo.CONTENT_ITEM_TYPE);
 
+            // Look for a non-empty super primary photo
             final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE);
             if (dataKind != null) {
-                if (Photo.CONTENT_ITEM_TYPE.equals(dataKind.mimeType)) {
+                final ValuesDelta valuesDelta = getNonEmptySuperPrimaryValuesDeltas(
+                        rawContactDelta, Photo.CONTENT_ITEM_TYPE, dataKind);
+                if (valuesDelta != null) {
                     mPhotoRawContactId = rawContactDelta.getRawContactId();
-                    final ValuesDelta valuesDelta = rawContactDelta.getSuperPrimaryEntry(
-                            dataKind.mimeType, /* forceSelection =*/ true);
                     mPhoto.setValues(dataKind, valuesDelta, rawContactDelta,
-                            /* readOnly =*/ !dataKind.editable, mMaterialPalette, viewIdGenerator);
+                            /* readOnly =*/ !dataKind.editable, mMaterialPalette,
+                            viewIdGenerator);
                     return;
                 }
             }
         }
+        // We didn't find a non-empty super primary photo, use the first non-empty one
+        for (RawContactDelta rawContactDelta : rawContactDeltas) {
+            if (!rawContactDelta.isVisible()) continue;
+            final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
+            final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE);
+            if (dataKind != null) {
+                final List<ValuesDelta> valuesDeltas = getNonEmptyValuesDeltas(
+                        rawContactDelta, Photo.CONTENT_ITEM_TYPE, dataKind);
+                if (valuesDeltas != null && !valuesDeltas.isEmpty()) {
+                    mPhotoRawContactId = rawContactDelta.getRawContactId();
+                    mPhoto.setValues(dataKind, valuesDeltas.get(0), rawContactDelta,
+                            /* readOnly =*/ !dataKind.editable, mMaterialPalette,
+                            viewIdGenerator);
+                    return;
+                }
+            }
+        }
+        // No suitable non-empty photo
+        for (RawContactDelta rawContactDelta : rawContactDeltas) {
+            if (!rawContactDelta.isVisible()) continue;
+            final AccountType accountType = rawContactDelta.getAccountType(mAccountTypeManager);
+            final DataKind dataKind = accountType.getKindForMimetype(Photo.CONTENT_ITEM_TYPE);
+            if (dataKind != null) {
+                final ValuesDelta valuesDelta = rawContactDelta.getSuperPrimaryEntry(
+                        dataKind.mimeType, /* forceSelection =*/ true);
+                if (valuesDelta != null) {
+                    mPhotoRawContactId = rawContactDelta.getRawContactId();
+                    mPhoto.setValues(dataKind, valuesDelta, rawContactDelta,
+                            /* readOnly =*/ !dataKind.editable, mMaterialPalette,
+                            viewIdGenerator);
+                    return;
+                }
+            }
+        }
+        // Should not happen since we ensure the kind exists but if we unexpectedly get here
+        // we must remove the photo section so that it does not take up the entire view
+        mPhoto.setVisibility(View.GONE);
     }
 
     private void addStructuredNameView(RawContactDeltaList rawContactDeltas) {
@@ -389,13 +426,11 @@
                 } else if (DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME.equals(mimeType)) {
                     // Only add phonetic names if there is a non-empty one. Note the use of
                     // StructuredName mimeType below, even though we matched a pseudo mime type.
-                    if (hasNonEmptyValuesDelta(
-                            rawContactDelta, StructuredName.CONTENT_ITEM_TYPE, dataKind)) {
-                        for (ValuesDelta valuesDelta : getNonEmptyValuesDeltas(
-                                rawContactDelta, StructuredName.CONTENT_ITEM_TYPE, dataKind)) {
-                            mPhoneticNames.addView(inflatePhoneticNameEditorView(
-                                    mPhoneticNames, accountType, valuesDelta, rawContactDelta));
-                        }
+                    final ValuesDelta valuesDelta = rawContactDelta.getSuperPrimaryEntry(
+                            StructuredName.CONTENT_ITEM_TYPE, /* forceSelection =*/ true);
+                    if (hasNonEmptyValue(dataKind, valuesDelta)) {
+                        mPhoneticNames.addView(inflatePhoneticNameEditorView(
+                                mPhoneticNames, accountType, valuesDelta, rawContactDelta));
                     }
                 } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType)) {
                     // Only add nicknames if there is a non-empty one
@@ -486,22 +521,30 @@
             return result;
         }
         for (ValuesDelta valuesDelta : rawContactDelta.getMimeEntries(mimeType)) {
-            if (valuesDelta == null) {
-                log(Log.VERBOSE, "Null valuesDelta");
-            }
-            for (EditField editField : dataKind.fieldList) {
-                final String column = editField.column;
-                final String value = valuesDelta == null ? null : valuesDelta.getAsString(column);
-                log(Log.VERBOSE, "Field " + column + " empty=" + TextUtils.isEmpty(value) +
-                        " value=" + value);
-                if (!TextUtils.isEmpty(value)) {
-                    result.add(valuesDelta);
-                }
+            if (hasNonEmptyValue(dataKind, valuesDelta)) {
+                result.add(valuesDelta);
             }
         }
         return result;
     }
 
+    private static boolean hasNonEmptyValue(DataKind dataKind, ValuesDelta valuesDelta) {
+        if (valuesDelta == null) {
+            log(Log.VERBOSE, "Null valuesDelta");
+            return false;
+        }
+        for (EditField editField : dataKind.fieldList) {
+            final String column = editField.column;
+            final String value = valuesDelta == null ? null : valuesDelta.getAsString(column);
+            log(Log.VERBOSE, "Field " + column + " empty=" + TextUtils.isEmpty(value) +
+                    " value=" + value);
+            if (!TextUtils.isEmpty(value)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private StructuredNameEditorView inflateStructuredNameEditorView(ViewGroup viewGroup,
             AccountType accountType, ValuesDelta valuesDelta, RawContactDelta rawContactDelta,
             NameEditorListener nameEditorListener) {
@@ -510,6 +553,7 @@
         if (nameEditorListener != null) {
             result.setEditorListener(nameEditorListener);
         }
+        result.setDeletable(false);
         result.setValues(
                 accountType.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME),
                 valuesDelta,
@@ -523,6 +567,7 @@
             AccountType accountType, ValuesDelta valuesDelta, RawContactDelta rawContactDelta) {
         final PhoneticNameEditorView result = (PhoneticNameEditorView) mLayoutInflater.inflate(
                 R.layout.phonetic_name_editor_view, viewGroup, /* attachToRoot =*/ false);
+        result.setDeletable(false);
         result.setValues(
                 accountType.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME),
                 valuesDelta,
diff --git a/src/com/android/contacts/editor/ContactEditorBaseFragment.java b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
index a7dd5f3..09408ec 100644
--- a/src/com/android/contacts/editor/ContactEditorBaseFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorBaseFragment.java
@@ -774,7 +774,7 @@
 
     private boolean revert() {
         if (mState.isEmpty() || !hasPendingChanges()) {
-            onSplitContactConfirmed();
+            onCancelEditConfirmed();
         } else {
             CancelEditDialogFragment.show(this);
         }
diff --git a/src/com/android/contacts/editor/TextFieldsEditorView.java b/src/com/android/contacts/editor/TextFieldsEditorView.java
index d4fa45d..1798b0f 100644
--- a/src/com/android/contacts/editor/TextFieldsEditorView.java
+++ b/src/com/android/contacts/editor/TextFieldsEditorView.java
@@ -182,14 +182,10 @@
      * Creates or removes the type/label button. Doesn't do anything if already correctly configured
      */
     private void setupExpansionView(boolean shouldExist, boolean collapsed) {
-        if (shouldExist) {
-            mExpansionViewContainer.setVisibility(View.VISIBLE);
-            mExpansionView.setImageResource(collapsed
-                    ? R.drawable.ic_menu_expander_minimized_holo_light
-                    : R.drawable.ic_menu_expander_maximized_holo_light);
-        } else {
-            mExpansionViewContainer.setVisibility(View.GONE);
-        }
+        mExpansionView.setImageResource(collapsed
+                ? R.drawable.ic_menu_expander_minimized_holo_light
+                : R.drawable.ic_menu_expander_maximized_holo_light);
+        mExpansionViewContainer.setVisibility(shouldExist ? View.VISIBLE : View.INVISIBLE);
     }
 
     @Override