Layout read-only contacts correctly

- remove unnecessary headers between multiple phones/emails
- modify the layout a bit

Bug: 5149895
Change-Id: I9bbccd8a68d2ffe3d0ed7bbbb8af0f93ffccc4f7
diff --git a/res/layout/item_read_only_field.xml b/res/layout/item_read_only_field.xml
index 03778cc..5f8367f 100644
--- a/res/layout/item_read_only_field.xml
+++ b/res/layout/item_read_only_field.xml
@@ -14,7 +14,8 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">
@@ -23,13 +24,29 @@
         android:id="@+id/kind_title_layout"
         layout="@layout/edit_kind_title" />
 
-    <TextView android:id="@+id/data"
-        android:layout_width="wrap_content"
-        android:layout_height="0px"
-        android:layout_weight="1"
-        android:layout_marginLeft="16dip"
-        android:textAppearance="?android:attr/textAppearanceMedium"
-        android:textColor="?android:attr/textColorSecondary"
-        android:singleLine="true"/>
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:minHeight="@dimen/editor_min_line_item_height"
+        android:orientation="horizontal">
+        <TextView
+            android:id="@+id/data"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center_vertical"
+            android:layout_marginLeft="16dip"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:textColor="?android:attr/textColorPrimary"
+            android:singleLine="true"/>
+        <TextView
+            android:id="@+id/type"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center_vertical"
+            android:layout_marginLeft="8dip"
+            android:textAppearance="?android:attr/textAppearanceSmall"
+            android:textColor="?android:attr/textColorSecondary"
+            android:singleLine="true"/>
+    </LinearLayout>
 
-</LinearLayout>
\ No newline at end of file
+</LinearLayout>
diff --git a/src/com/android/contacts/editor/ExternalRawContactEditorView.java b/src/com/android/contacts/editor/ExternalRawContactEditorView.java
index 8a18114..e91cfcb 100644
--- a/src/com/android/contacts/editor/ExternalRawContactEditorView.java
+++ b/src/com/android/contacts/editor/ExternalRawContactEditorView.java
@@ -27,6 +27,7 @@
 
 import android.content.ContentUris;
 import android.content.Context;
+import android.content.res.Resources;
 import android.net.Uri;
 import android.provider.ContactsContract.CommonDataKinds.Email;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
@@ -199,35 +200,42 @@
             mEditExternallyButton.setVisibility(View.VISIBLE);
         }
 
+        final Resources res = mContext.getResources();
         // Phones
         ArrayList<ValuesDelta> phones = state.getMimeEntries(Phone.CONTENT_ITEM_TYPE);
         if (phones != null) {
-            for (ValuesDelta phone : phones) {
-                View field = mInflater.inflate(
-                        R.layout.item_read_only_field, mGeneral, false);
-                TextView v;
-                v = (TextView) field.findViewById(R.id.kind_title);
-                v.setText(mContext.getText(R.string.phoneLabelsGroup));
-                v = (TextView) field.findViewById(R.id.data);
-                v.setText(PhoneNumberUtils.formatNumber(phone.getAsString(Phone.NUMBER),
+            for (int i = 0; i < phones.size(); i++) {
+                ValuesDelta phone = phones.get(i);
+                final String phoneNumber = PhoneNumberUtils.formatNumber(
+                        phone.getAsString(Phone.NUMBER),
                         phone.getAsString(Phone.NORMALIZED_NUMBER),
-                        ContactsUtils.getCurrentCountryIso(getContext())));
-                mGeneral.addView(field);
+                        ContactsUtils.getCurrentCountryIso(getContext()));
+                final CharSequence phoneType;
+                if (phone.containsKey(Phone.TYPE)) {
+                    phoneType = Phone.getTypeLabel(
+                            res, phone.getAsInteger(Phone.TYPE), phone.getAsString(Phone.LABEL));
+                } else {
+                    phoneType = null;
+                }
+                bindData(mContext.getText(R.string.phoneLabelsGroup),
+                        phoneNumber, phoneType, i == 0);
             }
         }
 
         // Emails
         ArrayList<ValuesDelta> emails = state.getMimeEntries(Email.CONTENT_ITEM_TYPE);
         if (emails != null) {
-            for (ValuesDelta email : emails) {
-                View field = mInflater.inflate(
-                        R.layout.item_read_only_field, mGeneral, false);
-                TextView v;
-                v = (TextView) field.findViewById(R.id.kind_title);
-                v.setText(mContext.getText(R.string.emailLabelsGroup));
-                v = (TextView) field.findViewById(R.id.data);
-                v.setText(email.getAsString(Email.DATA));
-                mGeneral.addView(field);
+            for (int i = 0; i < emails.size(); i++) {
+                ValuesDelta email = emails.get(i);
+                final String emailAddress = email.getAsString(Email.DATA);
+                final CharSequence emailType;
+                if (email.containsKey(Email.TYPE)) {
+                    emailType = Email.getTypeLabel(
+                            res, email.getAsInteger(Email.TYPE), email.getAsString(Email.LABEL));
+                } else {
+                    emailType = null;
+                }
+                bindData(mContext.getText(R.string.emailLabelsGroup), emailAddress, null, i == 0);
             }
         }
 
@@ -239,6 +247,28 @@
         }
     }
 
+    private void bindData(
+            CharSequence titleText, CharSequence data, CharSequence type, boolean isFirstEntry) {
+        final View field = mInflater.inflate(R.layout.item_read_only_field, mGeneral, false);
+        if (isFirstEntry) {
+            final TextView titleView = (TextView) field.findViewById(R.id.kind_title);
+            titleView.setText(titleText);
+        } else {
+            View titleContainer = field.findViewById(R.id.kind_title_layout);
+            titleContainer.setVisibility(View.GONE);
+        }
+        final TextView dataView = (TextView) field.findViewById(R.id.data);
+        dataView.setText(data);
+        final TextView typeView = (TextView) field.findViewById(R.id.type);
+        if (!TextUtils.isEmpty(type)) {
+            typeView.setText(type);
+        } else {
+            typeView.setVisibility(View.GONE);
+        }
+
+        mGeneral.addView(field);
+    }
+
     @Override
     public long getRawContactId() {
         return mRawContactId;
diff --git a/src/com/android/contacts/model/EntityDelta.java b/src/com/android/contacts/model/EntityDelta.java
index 97ab347..a045eb4 100644
--- a/src/com/android/contacts/model/EntityDelta.java
+++ b/src/com/android/contacts/model/EntityDelta.java
@@ -561,6 +561,11 @@
             return mAfter;
         }
 
+        public boolean containsKey(String key) {
+            return ((mAfter != null && mAfter.containsKey(key)) ||
+                    (mBefore != null && mBefore.containsKey(key)));
+        }
+
         public String getAsString(String key) {
             if (mAfter != null && mAfter.containsKey(key)) {
                 return mAfter.getAsString(key);