Fix account selector UI in Contact editor.

1) Hide the "Saving to" account container when there're both
read-only and writable contacts.
2) Add more space between name field and whatever are above it.
3) Show linked contacts selector when there's only one read-only.

BUG 25219324

Change-Id: I9229132a833400de5cb542bc5363a4e3f36c9d1a
diff --git a/res/layout/compact_account_info.xml b/res/layout/compact_account_info.xml
index 43686f3..a31e12d 100644
--- a/res/layout/compact_account_info.xml
+++ b/res/layout/compact_account_info.xml
@@ -24,6 +24,7 @@
         android:minHeight="@dimen/editor_min_line_item_height"
         android:orientation="horizontal"
         android:background="?android:attr/selectableItemBackground"
+        android:layout_marginBottom="@dimen/compact_editor_name_top_margin"
         android:visibility="gone"
         >
 
diff --git a/res/layout/editor_account_selector.xml b/res/layout/editor_account_selector.xml
index b3d7eb0..3a1bf83 100644
--- a/res/layout/editor_account_selector.xml
+++ b/res/layout/editor_account_selector.xml
@@ -23,6 +23,7 @@
     android:layout_width="match_parent"
     android:minHeight="@dimen/editor_min_line_item_height"
     android:orientation="horizontal"
+    android:layout_marginBottom="@dimen/compact_editor_name_top_margin"
     android:visibility="gone" >
 
     <ImageView
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 16bc213..27ec230 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -261,4 +261,7 @@
 
     <!-- Width of padding between columns of photos in photo picker -->
     <dimen name="photo_picker_column_padding_width">1dp</dimen>
+
+    <!-- Margin between name field and whatever fields are above it. -->
+    <dimen name="compact_editor_name_top_margin">8dp</dimen>
 </resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 3f721b3..5db5480 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -774,7 +774,10 @@
     <string name="compact_editor_account_selector_title">Saving to</string>
 
     <!-- Label for the linked contacts selector which indicates the number of raw contacts which have been linked together into the aggregate being viewed. [CHAR LIMIT=40] -->
-    <string name="compact_editor_linked_contacts_selector_title">Linked contacts (<xliff:g id="count">%d</xliff:g>)</string>
+    <plurals name="compact_editor_linked_contacts_selector_title">
+        <item quantity="one">Linked contact</item>
+        <item quantity="other">Linked contacts (<xliff:g id="count">%d</xliff:g>)</item>
+    </plurals>
 
     <!-- Quick contact display name with phonetic name -->
     <string name="quick_contact_display_name_with_phonetic"><xliff:g id="display_name">%s</xliff:g> (<xliff:g id="phonetic_name">%s</xliff:g>)</string>
diff --git a/src/com/android/contacts/editor/CompactRawContactsEditorView.java b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
index 8a4d479..303a450 100644
--- a/src/com/android/contacts/editor/CompactRawContactsEditorView.java
+++ b/src/com/android/contacts/editor/CompactRawContactsEditorView.java
@@ -756,27 +756,69 @@
             if (accounts.size() > 1) {
                 addAccountSelector(accountInfo, rawContactDelta);
             } else {
-                addAccountHeader(accountInfo);
+                addAccountHeader(accountInfo, rawContactDeltas);
             }
         } else {
-            addAccountHeader(accountInfo);
+            addAccountHeader(accountInfo, rawContactDeltas);
         }
 
         // The raw contact selector should only display linked raw contacts that can be edited in
         // the full editor (i.e. they are not newly created raw contacts)
-        Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
-        final RawContactAccountListAdapter adapter =
-                new RawContactAccountListAdapter(getContext(), rawContactDeltas);
-        if (adapter.getCount() > 1) {
-            final String accountsSummary = getResources().getString(
-                    R.string.compact_editor_linked_contacts_selector_title,
-                    adapter.getCount());
+        final RawContactAccountListAdapter adapter =  new RawContactAccountListAdapter(getContext(),
+                getRawContactDeltaListForSelector(rawContactDeltas));
+        if (adapter.getCount() > 0) {
+            final String accountsSummary = getResources().getQuantityString(
+                    R.plurals.compact_editor_linked_contacts_selector_title,
+                    adapter.getCount(), adapter.getCount());
             addRawContactAccountSelector(accountsSummary, adapter);
         }
     }
 
-    private void addAccountHeader(Pair<String,String> accountInfo) {
+    private RawContactDeltaList getRawContactDeltaListForSelector(
+            RawContactDeltaList rawContactDeltas) {
+        // Sort raw contacts so google accounts come first
+        Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
+
+        final RawContactDeltaList result = new RawContactDeltaList();
+        for (RawContactDelta rawContactDelta : rawContactDeltas) {
+            if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
+                // Only add raw contacts that can be opened in the editor
+                result.add(rawContactDelta);
+            }
+        }
+        // Don't return a list of size 1 that would just open the raw contact being edited
+        // in the compact editor in the full editor
+        if (result.size() == 1 && result.get(0).getRawContactAccountType(
+                getContext()).areContactsWritable()) {
+            result.clear();
+            return result;
+        }
+        return result;
+    }
+
+    // Returns true if there're multiple writable and no read only, or there're both writable and
+    // read only. For ME profile, return false if there's a read only contact and unsaved local one.
+    private boolean shouldHideAccountHeader(RawContactDeltaList rawContactDeltas) {
+        int writable = 0;
+        int readonly = 0;
+        for (RawContactDelta rawContactDelta : rawContactDeltas) {
+            if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
+                if (rawContactDelta.getRawContactAccountType(getContext()).areContactsWritable()) {
+                    writable++;
+                } else {
+                    readonly++;
+                }
+            }
+        }
+        return (writable > 1 || (writable > 0 && readonly > 0));
+    }
+
+    private void addAccountHeader(Pair<String,String> accountInfo,
+            RawContactDeltaList rawContactDeltas) {
         mAccountHeaderContainer.setVisibility(View.VISIBLE);
+        if (shouldHideAccountHeader(rawContactDeltas)) {
+            mAccountHeaderContainer.setVisibility(View.GONE);
+        }
 
         // Set the account name
         final String accountName = TextUtils.isEmpty(accountInfo.first)