Merge "Make filter header clickable"
diff --git a/res/drawable-hdpi/ic_dial_action_search.png b/res/drawable-hdpi/ic_dial_action_search.png
deleted file mode 100644
index 898ce11..0000000
--- a/res/drawable-hdpi/ic_dial_action_search.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-hdpi/ic_see_contacts_holo_dark.png b/res/drawable-hdpi/ic_see_contacts_holo_dark.png
new file mode 100644
index 0000000..8e2182f
--- /dev/null
+++ b/res/drawable-hdpi/ic_see_contacts_holo_dark.png
Binary files differ
diff --git a/res/drawable-mdpi/ic_dial_action_search.png b/res/drawable-mdpi/ic_dial_action_search.png
deleted file mode 100644
index 88cba92..0000000
--- a/res/drawable-mdpi/ic_dial_action_search.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-mdpi/ic_see_contacts_holo_dark.png b/res/drawable-mdpi/ic_see_contacts_holo_dark.png
new file mode 100644
index 0000000..ef1d2df
--- /dev/null
+++ b/res/drawable-mdpi/ic_see_contacts_holo_dark.png
Binary files differ
diff --git a/res/drawable-xhdpi/ic_dial_action_search.png b/res/drawable-xhdpi/ic_dial_action_search.png
deleted file mode 100644
index fe3aa24..0000000
--- a/res/drawable-xhdpi/ic_dial_action_search.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-xhdpi/ic_see_contacts_holo_dark.png b/res/drawable-xhdpi/ic_see_contacts_holo_dark.png
new file mode 100644
index 0000000..279fff9
--- /dev/null
+++ b/res/drawable-xhdpi/ic_see_contacts_holo_dark.png
Binary files differ
diff --git a/res/layout/dialpad_additional_buttons.xml b/res/layout/dialpad_additional_buttons.xml
index a5e247b..f0e4a42 100644
--- a/res/layout/dialpad_additional_buttons.xml
+++ b/res/layout/dialpad_additional_buttons.xml
@@ -25,7 +25,8 @@
     android:background="@drawable/dialpad_background"
     android:orientation="horizontal">
 
-    <!-- Onscreen "Search button -->
+    <!-- Onscreen "All contacts" button.
+         TODO: rename this id. -->
     <ImageButton android:id="@+id/searchButton"
         android:layout_width="0px"
         android:layout_weight="0.30"
@@ -34,7 +35,7 @@
         android:state_enabled="false"
         android:background="@drawable/btn_dial"
         android:contentDescription="@string/description_search_button"
-        android:src="@drawable/ic_dial_action_search"/>
+        android:src="@drawable/ic_see_contacts_holo_dark"/>
 
     <!-- Onscreen "Dial" button, diused on all platforms by
          default. Its usage can be disabled using resources (see
diff --git a/res/values-sw580dp-w720dp/styles.xml b/res/values-sw580dp-w720dp/styles.xml
index eadb88c..5ad25c6 100644
--- a/res/values-sw580dp-w720dp/styles.xml
+++ b/res/values-sw580dp-w720dp/styles.xml
@@ -36,7 +36,7 @@
         <item name="list_item_presence_icon_margin">4dip</item>
         <item name="list_item_photo_size">64dip</item>
         <item name="list_item_profile_photo_size">80dip</item>
-        <item name="list_item_prefix_highlight_color">#729a27</item>
+        <item name="list_item_prefix_highlight_color">#99cc00</item>
         <item name="list_item_header_text_indent">8dip</item>
         <item name="list_item_header_text_color">@color/people_app_theme_color</item>
         <item name="list_item_header_height">24dip</item>
diff --git a/res/values-sw580dp/styles.xml b/res/values-sw580dp/styles.xml
index c60afca..ee65159 100644
--- a/res/values-sw580dp/styles.xml
+++ b/res/values-sw580dp/styles.xml
@@ -36,7 +36,7 @@
         <item name="list_item_presence_icon_margin">4dip</item>
         <item name="list_item_photo_size">64dip</item>
         <item name="list_item_profile_photo_size">80dip</item>
-        <item name="list_item_prefix_highlight_color">#729a27</item>
+        <item name="list_item_prefix_highlight_color">#99cc00</item>
         <item name="list_item_header_text_indent">8dip</item>
         <item name="list_item_header_text_size">14sp</item>
         <item name="list_item_header_text_color">@color/people_app_theme_color</item>
diff --git a/res/values/colors.xml b/res/values/colors.xml
index f224832..561eefc 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -71,4 +71,7 @@
     <!--  Color of the semi-transparent shadow box on contact tiles -->
     <color name="contact_tile_shadow_box_color">#7F000000</color>
 
+    <!--  Color of the vertical stripe that goes on the left of a block quote inside a stream item -->
+    <color name="stream_item_stripe_color">#CCCCCC</color>
+
 </resources>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 87ff84b..cd02d04 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -191,4 +191,7 @@
     <!--  Vertical and horizontal padding in between contact tiles -->
     <dimen name="contact_tile_divider_padding">1dip</dimen>
 
+    <!--  Width of the lead margin on the left of a block quote inside a stream item -->
+    <dimen name="stream_item_stripe_width">8dip</dimen>
+
 </resources>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 3c6694d..4317050 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -140,7 +140,7 @@
         <item name="list_item_presence_icon_size">16dip</item>
         <item name="list_item_photo_size">@dimen/contact_browser_list_item_photo_size</item>
         <item name="list_item_profile_photo_size">70dip</item>
-        <item name="list_item_prefix_highlight_color">#729a27</item>
+        <item name="list_item_prefix_highlight_color">#99cc00</item>
         <item name="list_item_header_text_color">@color/people_app_theme_color</item>
         <item name="list_item_header_text_size">14dip</item>
         <item name="list_item_header_height">26dip</item>
diff --git a/src/com/android/contacts/ContactsUtils.java b/src/com/android/contacts/ContactsUtils.java
index 2f13481..d3d8f34 100644
--- a/src/com/android/contacts/ContactsUtils.java
+++ b/src/com/android/contacts/ContactsUtils.java
@@ -18,10 +18,7 @@
 
 import com.android.contacts.model.AccountTypeManager;
 import com.android.contacts.model.AccountWithDataSet;
-import com.android.i18n.phonenumbers.NumberParseException;
 import com.android.i18n.phonenumbers.PhoneNumberUtil;
-import com.android.i18n.phonenumbers.PhoneNumberUtil.MatchType;
-import com.android.i18n.phonenumbers.Phonenumber.PhoneNumber;
 
 import android.content.Context;
 import android.content.Intent;
@@ -105,47 +102,49 @@
      */
     public static final boolean shouldCollapse(Context context, CharSequence mimetype1,
             CharSequence data1, CharSequence mimetype2, CharSequence data2) {
-        if (TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)
-                && TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype2)) {
-            if (data1 == data2) {
-                return true;
-            }
-            if (data1 == null || data2 == null) {
-                return false;
-            }
+        // different mimetypes? don't collapse
+        if (!TextUtils.equals(mimetype1, mimetype2)) return false;
 
-            // If the number contains semicolons, PhoneNumberUtils.compare
-            // only checks the substring before that (which is fine for caller-id usually)
-            // but not for collapsing numbers. so we check each segment indidually to be more strict
-            // TODO: This should be replaced once we have a more robust phonenumber-library
-            String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
-            String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
-            if (dataParts1.length != dataParts2.length) {
-                return false;
-            }
-            PhoneNumberUtil util = PhoneNumberUtil.getInstance();
-            for (int i = 0; i < dataParts1.length; i++) {
-                try {
-                    PhoneNumber phoneNumber1 = util.parse(dataParts1[i], "ZZ" /* Unknown */);
-                    PhoneNumber phoneNumber2 = util.parse(dataParts2[i], "ZZ" /* Unknown */);
-                    MatchType matchType = util.isNumberMatch(phoneNumber1, phoneNumber2);
-                    if (matchType != MatchType.SHORT_NSN_MATCH) {
-                        return false;
-                    }
-                } catch (NumberParseException e) {
-                    if (!TextUtils.equals(dataParts1[i], dataParts2[i])) {
-                        return false;
-                    }
-                }
-            }
+        // exact same string? good, bail out early
+        if (TextUtils.equals(data1, data2)) return true;
 
-            return true;
-        } else {
-            if (mimetype1 == mimetype2 && data1 == data2) {
-                return true;
+        // so if either is null, these two must be different
+        if (data1 == null || data2 == null) return false;
+
+        // if this is not about phone numbers, we know this is not a match (of course, some
+        // mimetypes could have more sophisticated matching is the future, e.g. addresses)
+        if (!TextUtils.equals(Phone.CONTENT_ITEM_TYPE, mimetype1)) return false;
+
+        // Now do the full phone number thing. split into parts, seperated by waiting symbol
+        // and compare them individually
+        final String[] dataParts1 = data1.toString().split(WAIT_SYMBOL_AS_STRING);
+        final String[] dataParts2 = data2.toString().split(WAIT_SYMBOL_AS_STRING);
+        if (dataParts1.length != dataParts2.length) return false;
+        final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
+        for (int i = 0; i < dataParts1.length; i++) {
+            final String dataPart1 = dataParts1[i];
+            final String dataPart2 = dataParts2[i];
+
+            // substrings equal? shortcut, don't parse
+            if (TextUtils.equals(dataPart1, dataPart2)) continue;
+
+            // do a full parse of the numbers
+            switch (util.isNumberMatch(dataPart1, dataPart2)) {
+                case NOT_A_NUMBER:
+                    // don't understand the numbers? let's play it safe
+                    return false;
+                case NO_MATCH:
+                    return false;
+                case EXACT_MATCH:
+                case SHORT_NSN_MATCH:
+                case NSN_MATCH:
+                    break;
+                default:
+                    throw new IllegalStateException("Unknown result value from phone number " +
+                            "library");
             }
-            return TextUtils.equals(mimetype1, mimetype2) && TextUtils.equals(data1, data2);
         }
+        return true;
     }
 
     /**
diff --git a/src/com/android/contacts/activities/DialtactsActivity.java b/src/com/android/contacts/activities/DialtactsActivity.java
index bddd472..25f6fc2 100644
--- a/src/com/android/contacts/activities/DialtactsActivity.java
+++ b/src/com/android/contacts/activities/DialtactsActivity.java
@@ -516,11 +516,7 @@
 
     private void setupDialer() {
         final Tab tab = getActionBar().newTab();
-        // TODO: Temporarily disable tab text labels (in all 4 tabs in this
-        //   activity) so that the current tabs will all fit onscreen in
-        //   portrait (bug 4520620).  (Also note we do setText("") rather
-        //   leaving the text null, to work around bug 4521549.)
-        tab.setText("");  // R.string.dialerIconLabel
+        tab.setContentDescription(R.string.dialerIconLabel);
         tab.setTabListener(mTabListener);
         tab.setIcon(R.drawable.ic_tab_dialer);
         getActionBar().addTab(tab);
@@ -528,7 +524,7 @@
 
     private void setupCallLog() {
         final Tab tab = getActionBar().newTab();
-        tab.setText("");  // R.string.recentCallsIconLabel
+        tab.setContentDescription(R.string.recentCallsIconLabel);
         tab.setIcon(R.drawable.ic_tab_recent);
         tab.setTabListener(mTabListener);
         getActionBar().addTab(tab);
@@ -536,7 +532,7 @@
 
     private void setupFavorites() {
         final Tab tab = getActionBar().newTab();
-        tab.setText("");  // R.string.contactsFavoritesLabel
+        tab.setContentDescription(R.string.contactsFavoritesLabel);
         tab.setIcon(R.drawable.ic_tab_starred);
         tab.setTabListener(mTabListener);
         getActionBar().addTab(tab);
diff --git a/src/com/android/contacts/activities/PeopleActivity.java b/src/com/android/contacts/activities/PeopleActivity.java
index 31e5064..332a953 100644
--- a/src/com/android/contacts/activities/PeopleActivity.java
+++ b/src/com/android/contacts/activities/PeopleActivity.java
@@ -491,13 +491,9 @@
                     tabToOpen = TabState.ALL;
                     break;
 
-                // TODO: handle FREQUENT and STREQUENT according to the spec
                 case ContactsRequest.ACTION_FREQUENT:
                 case ContactsRequest.ACTION_STREQUENT:
-                    // For now they are treated the same as STARRED
                 case ContactsRequest.ACTION_STARRED:
-                    filter = ContactListFilter.createFilterWithType(
-                            ContactListFilter.FILTER_TYPE_STARRED);
                     tabToOpen = TabState.FAVORITES;
                     break;
                 case ContactsRequest.ACTION_VIEW_CONTACT:
diff --git a/src/com/android/contacts/detail/ContactDetailDisplayUtils.java b/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
index cd4add6..f94629b 100644
--- a/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
+++ b/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
@@ -22,6 +22,7 @@
 import com.android.contacts.R;
 import com.android.contacts.preference.ContactsPreferences;
 import com.android.contacts.util.ContactBadgeUtil;
+import com.android.contacts.util.HtmlUtils;
 import com.android.contacts.util.StreamItemEntry;
 import com.android.contacts.util.StreamItemPhotoEntry;
 import com.google.common.annotations.VisibleForTesting;
@@ -236,7 +237,7 @@
         String photoUri = null;
         if (!contactData.getStreamItems().isEmpty()) {
             StreamItemEntry firstEntry = contactData.getStreamItems().get(0);
-            snippet = Html.fromHtml(firstEntry.getText());
+            snippet = HtmlUtils.fromHtml(context, firstEntry.getText());
             if (!firstEntry.getPhotos().isEmpty()) {
                 StreamItemPhotoEntry firstPhoto = firstEntry.getPhotos().get(0);
                 photoUri = firstPhoto.getPhotoUri();
@@ -338,10 +339,11 @@
                 R.id.stream_item_attribution);
         TextView commentsView = (TextView) rootView.findViewById(R.id.stream_item_comments);
         ImageGetter imageGetter = new DefaultImageGetter(context.getPackageManager());
-        htmlView.setText(Html.fromHtml(streamItem.getText(), imageGetter, null));
+        htmlView.setText(HtmlUtils.fromHtml(context, streamItem.getText(), imageGetter, null));
         attributionView.setText(ContactBadgeUtil.getSocialDate(streamItem, context));
         if (streamItem.getComments() != null) {
-            commentsView.setText(Html.fromHtml(streamItem.getComments(), imageGetter, null));
+            commentsView.setText(HtmlUtils.fromHtml(context, streamItem.getComments(), imageGetter,
+                    null));
             commentsView.setVisibility(View.VISIBLE);
         } else {
             commentsView.setVisibility(View.GONE);
diff --git a/src/com/android/contacts/list/ContactListPinnedHeaderView.java b/src/com/android/contacts/list/ContactListPinnedHeaderView.java
index 5e95e4d..913bb75 100644
--- a/src/com/android/contacts/list/ContactListPinnedHeaderView.java
+++ b/src/com/android/contacts/list/ContactListPinnedHeaderView.java
@@ -86,6 +86,7 @@
         mHeaderTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mHeaderTextSize);
         mHeaderTextView.setTypeface(mHeaderTextView.getTypeface(), Typeface.BOLD);
         mHeaderTextView.setGravity(Gravity.CENTER_VERTICAL);
+        mHeaderTextView.setAllCaps(true);
         addView(mHeaderTextView);
         mHeaderDivider = new View(mContext);
         mHeaderDivider.setBackgroundColor(mHeaderUnderlineColor);
diff --git a/src/com/android/contacts/list/ContactsSectionIndexer.java b/src/com/android/contacts/list/ContactsSectionIndexer.java
index 5ecb31c..c260667 100644
--- a/src/com/android/contacts/list/ContactsSectionIndexer.java
+++ b/src/com/android/contacts/list/ContactsSectionIndexer.java
@@ -16,6 +16,7 @@
 
 package com.android.contacts.list;
 
+import android.text.TextUtils;
 import android.widget.SectionIndexer;
 
 import java.util.Arrays;
@@ -29,6 +30,7 @@
     private String[] mSections;
     private int[] mPositions;
     private int mCount;
+    private static final String BLANK_HEADER_STRING = " ";
 
     /**
      * Constructor.
@@ -52,9 +54,9 @@
         mPositions = new int[counts.length];
         int position = 0;
         for (int i = 0; i < counts.length; i++) {
-            if (mSections[i] == null) {
-                mSections[i] = " ";
-            } else {
+            if (TextUtils.isEmpty(mSections[i])) {
+                mSections[i] = BLANK_HEADER_STRING;
+            } else if (!mSections[i].equals(BLANK_HEADER_STRING)) {
                 mSections[i] = mSections[i].trim();
             }
 
diff --git a/src/com/android/contacts/list/PhoneNumberListAdapter.java b/src/com/android/contacts/list/PhoneNumberListAdapter.java
index cac89b1..a86e082 100644
--- a/src/com/android/contacts/list/PhoneNumberListAdapter.java
+++ b/src/com/android/contacts/list/PhoneNumberListAdapter.java
@@ -90,15 +90,6 @@
                     + "directoryId: " + directoryId + ")");
         }
 
-        final ContactListFilter filter = getFilter();
-        if (filter != null &&
-                (filter.filterType != ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS &&
-                filter.filterType != ContactListFilter.FILTER_TYPE_ACCOUNT &&
-                filter.filterType != ContactListFilter.FILTER_TYPE_CUSTOM)) {
-            throw new IllegalArgumentException("Unexpected filter type came " +
-                    "(type: " + filter.filterType + ", toString: " + filter + ")");
-        }
-
         if (isSearchMode()) {
             String query = getQueryString();
             Builder builder = Phone.CONTENT_FILTER_URI.buildUpon();
@@ -122,7 +113,7 @@
             }
 
             loader.setProjection(PHONES_PROJECTION);
-            configureSelection(loader, directoryId, filter);
+            configureSelection(loader, directoryId, getFilter());
         }
 
         loader.setUri(uri);
@@ -145,9 +136,6 @@
         final List<String> selectionArgs = new ArrayList<String>();
 
         switch (filter.filterType) {
-            case ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS: {
-                break;
-            }
             case ContactListFilter.FILTER_TYPE_CUSTOM: {
                 selection.append(Contacts.IN_VISIBLE_GROUP + "=1");
                 selection.append(" AND " + Contacts.HAS_PHONE_NUMBER + "=1");
@@ -169,20 +157,17 @@
                 selection.append(")");
                 break;
             }
-            case ContactListFilter.FILTER_TYPE_GROUP: {
-                selection.append(Data.MIMETYPE + "=?"
-                        + " AND " + GroupMembership.GROUP_ROW_ID + "=?");
-                selectionArgs.add(GroupMembership.CONTENT_ITEM_TYPE);
-                selectionArgs.add(String.valueOf(filter.groupId));
-                break;
-            }
-
-            case ContactListFilter.FILTER_TYPE_SINGLE_CONTACT:
-            case ContactListFilter.FILTER_TYPE_STARRED:
+            case ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS:
+            case ContactListFilter.FILTER_TYPE_DEFAULT:
+                break; // No selection needed.
             case ContactListFilter.FILTER_TYPE_WITH_PHONE_NUMBERS_ONLY:
+                break; // This adapter is always "phone only", so no selection needed either.
             default:
-                throw new IllegalArgumentException("Unexpected filter type came " +
-                        "(type: " + filter.filterType + ", toString: " + filter + ")");
+                Log.w(TAG, "Unsupported filter type came " +
+                        "(type: " + filter.filterType + ", toString: " + filter + ")" +
+                        " showing all contacts.");
+                // No selection.
+                break;
         }
         loader.setSelection(selection.toString());
         loader.setSelectionArgs(selectionArgs.toArray(new String[0]));
diff --git a/src/com/android/contacts/socialwidget/SocialWidgetProvider.java b/src/com/android/contacts/socialwidget/SocialWidgetProvider.java
index dd44310..9d064a5 100644
--- a/src/com/android/contacts/socialwidget/SocialWidgetProvider.java
+++ b/src/com/android/contacts/socialwidget/SocialWidgetProvider.java
@@ -21,6 +21,7 @@
 import com.android.contacts.model.AccountType;
 import com.android.contacts.model.AccountTypeManager;
 import com.android.contacts.util.ContactBadgeUtil;
+import com.android.contacts.util.HtmlUtils;
 import com.android.contacts.util.StreamItemEntry;
 
 import android.app.PendingIntent;
@@ -36,7 +37,6 @@
 import android.net.Uri;
 import android.provider.ContactsContract.QuickContact;
 import android.provider.ContactsContract.StreamItems;
-import android.text.Html;
 import android.text.SpannableStringBuilder;
 import android.text.TextUtils;
 import android.text.style.AbsoluteSizeSpan;
@@ -208,7 +208,7 @@
         } else {
             // TODO: Rotate between all the stream items?
             StreamItemEntry streamItem = streamItems.get(0);
-            CharSequence status = Html.fromHtml(streamItem.getText());
+            CharSequence status = HtmlUtils.fromHtml(context, streamItem.getText());
             if (status.length() <= SHORT_SNIPPET_LENGTH) {
                 sb.append("\n");
             } else {
diff --git a/src/com/android/contacts/util/HtmlUtils.java b/src/com/android/contacts/util/HtmlUtils.java
new file mode 100644
index 0000000..4663657
--- /dev/null
+++ b/src/com/android/contacts/util/HtmlUtils.java
@@ -0,0 +1,101 @@
+package com.android.contacts.util;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.text.Html;
+import android.text.Html.ImageGetter;
+import android.text.Html.TagHandler;
+import android.text.SpannableStringBuilder;
+import android.text.Spanned;
+import android.text.style.ImageSpan;
+import android.text.style.QuoteSpan;
+
+import com.android.contacts.R;
+
+/**
+ * Provides static functions to perform custom HTML to text conversions.
+ * Specifically, it adjusts the color and padding of the vertical
+ * stripe on block quotes and alignment of inlined images.
+ */
+public class HtmlUtils {
+
+    /**
+     * Converts HTML string to a {@link Spanned} text, adjusting formatting.
+     */
+    public static Spanned fromHtml(Context context, String text) {
+        Spanned spanned = Html.fromHtml(text);
+        postprocess(context, spanned);
+        return spanned;
+    }
+
+    /**
+     * Converts HTML string to a {@link Spanned} text, adjusting formatting and using a custom
+     * image getter.
+     */
+    public static CharSequence fromHtml(Context context, String text, ImageGetter imageGetter,
+            TagHandler tagHandler) {
+        Spanned spanned = Html.fromHtml(text, imageGetter, tagHandler);
+        postprocess(context, spanned);
+        return spanned;
+    }
+
+    /**
+     * Replaces some spans with custom versions of those.
+     */
+    private static void postprocess(Context context, Spanned spanned) {
+        if (!(spanned instanceof SpannableStringBuilder)) {
+            return;
+        }
+
+        int length = spanned.length();
+
+        SpannableStringBuilder builder = (SpannableStringBuilder)spanned;
+        QuoteSpan[] quoteSpans = spanned.getSpans(0, length, QuoteSpan.class);
+        if (quoteSpans != null && quoteSpans.length != 0) {
+            Resources resources = context.getResources();
+            int color = resources.getColor(R.color.stream_item_stripe_color);
+            int width = resources.getDimensionPixelSize(R.dimen.stream_item_stripe_width);
+            for (int i = 0; i < quoteSpans.length; i++) {
+                replaceSpan(builder, quoteSpans[i], new StreamItemQuoteSpan(color, width));
+            }
+        }
+
+        ImageSpan[] imageSpans = spanned.getSpans(0, length, ImageSpan.class);
+        if (imageSpans != null) {
+            for (int i = 0; i < imageSpans.length; i++) {
+                ImageSpan span = imageSpans[i];
+                replaceSpan(builder, span, new ImageSpan(span.getDrawable(),
+                        ImageSpan.ALIGN_BASELINE));
+            }
+        }
+    }
+
+    /**
+     * Replaces one span with the other.
+     */
+    private static void replaceSpan(SpannableStringBuilder builder, Object originalSpan,
+            Object newSpan) {
+        builder.setSpan(newSpan,
+                builder.getSpanStart(originalSpan),
+                builder.getSpanEnd(originalSpan),
+                builder.getSpanFlags(originalSpan));
+        builder.removeSpan(originalSpan);
+    }
+
+    public static class StreamItemQuoteSpan extends QuoteSpan {
+        private final int mWidth;
+
+        public StreamItemQuoteSpan(int color, int width) {
+            super(color);
+            this.mWidth = width;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public int getLeadingMargin(boolean first) {
+            return mWidth;
+        }
+    }
+}
diff --git a/tests/src/com/android/contacts/ContactsUtilsTests.java b/tests/src/com/android/contacts/ContactsUtilsTests.java
index e8deb11..97a2c8e 100644
--- a/tests/src/com/android/contacts/ContactsUtilsTests.java
+++ b/tests/src/com/android/contacts/ContactsUtilsTests.java
@@ -53,74 +53,132 @@
     }
 
     public void testShouldCollapse() throws Exception {
-        checkShouldCollapse("1", true, null, null, null, null);
-        checkShouldCollapse("2", true, "a", "b", "a", "b");
+        assertCollapses("1", true, null, null, null, null);
+        assertCollapses("2", true, "a", "b", "a", "b");
 
-        checkShouldCollapse("11", false, "a", null, null, null);
-        checkShouldCollapse("12", false, null, "a", null, null);
-        checkShouldCollapse("13", false, null, null, "a", null);
-        checkShouldCollapse("14", false, null, null, null, "a");
+        assertCollapses("11", false, "a", null, null, null);
+        assertCollapses("12", false, null, "a", null, null);
+        assertCollapses("13", false, null, null, "a", null);
+        assertCollapses("14", false, null, null, null, "a");
 
-        checkShouldCollapse("21", false, "a", "b", null, null);
-        checkShouldCollapse("22", false, "a", "b", "a", null);
-        checkShouldCollapse("23", false, "a", "b", null, "b");
-        checkShouldCollapse("24", false, "a", "b", "a", "x");
-        checkShouldCollapse("25", false, "a", "b", "x", "b");
+        assertCollapses("21", false, "a", "b", null, null);
+        assertCollapses("22", false, "a", "b", "a", null);
+        assertCollapses("23", false, "a", "b", null, "b");
+        assertCollapses("24", false, "a", "b", "a", "x");
+        assertCollapses("25", false, "a", "b", "x", "b");
 
-        checkShouldCollapse("31", false, null, null, "a", "b");
-        checkShouldCollapse("32", false, "a", null, "a", "b");
-        checkShouldCollapse("33", false, null, "b", "a", "b");
-        checkShouldCollapse("34", false, "a", "x", "a", "b");
-        checkShouldCollapse("35", false, "x", "b", "a", "b");
+        assertCollapses("31", false, null, null, "a", "b");
+        assertCollapses("32", false, "a", null, "a", "b");
+        assertCollapses("33", false, null, "b", "a", "b");
+        assertCollapses("34", false, "a", "x", "a", "b");
+        assertCollapses("35", false, "x", "b", "a", "b");
 
-        checkShouldCollapse("41", true, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE,
+        assertCollapses("41", true, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE,
                 null);
-        checkShouldCollapse("42", true, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, "1");
+        assertCollapses("42", true, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, "1");
 
-        checkShouldCollapse("51", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE,
+        assertCollapses("51", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE,
                 "2");
-        checkShouldCollapse("52", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE,
+        assertCollapses("52", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE,
                 null);
-        checkShouldCollapse("53", false, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE,
+        assertCollapses("53", false, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE,
                 "2");
 
         // Test phone numbers
-        checkShouldCollapse("60", true,
+        assertCollapses("60", true,
                 Phone.CONTENT_ITEM_TYPE, "1234567",
                 Phone.CONTENT_ITEM_TYPE, "1234567");
-        checkShouldCollapse("61", false,
+        assertCollapses("61", false,
                 Phone.CONTENT_ITEM_TYPE, "1234567",
                 Phone.CONTENT_ITEM_TYPE, "1234568");
-        checkShouldCollapse("62", true,
+        assertCollapses("62", true,
                 Phone.CONTENT_ITEM_TYPE, "1234567;0",
                 Phone.CONTENT_ITEM_TYPE, "1234567;0");
-        checkShouldCollapse("63", false,
+        assertCollapses("63", false,
                 Phone.CONTENT_ITEM_TYPE, "1234567;89321",
-                Phone.CONTENT_ITEM_TYPE, "1234567;321");
-        checkShouldCollapse("64", true,
+                Phone.CONTENT_ITEM_TYPE, "1234567;89322");
+        assertCollapses("64", true,
                 Phone.CONTENT_ITEM_TYPE, "1234567;89321",
                 Phone.CONTENT_ITEM_TYPE, "1234567;89321");
-        checkShouldCollapse("65", false,
+        assertCollapses("65", false,
                 Phone.CONTENT_ITEM_TYPE, "1234567;0111111111",
                 Phone.CONTENT_ITEM_TYPE, "1234567;");
-        checkShouldCollapse("66", false,
+        assertCollapses("66", false,
                 Phone.CONTENT_ITEM_TYPE, "12345675426;91970xxxxx",
                 Phone.CONTENT_ITEM_TYPE, "12345675426");
-        checkShouldCollapse("67", false,
+        assertCollapses("67", false,
                 Phone.CONTENT_ITEM_TYPE, "12345675426;23456xxxxx",
                 Phone.CONTENT_ITEM_TYPE, "12345675426;234567xxxx");
-        checkShouldCollapse("68", true,
+        assertCollapses("68", true,
                 Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567",
                 Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567");
-        checkShouldCollapse("69", false,
+        assertCollapses("69", false,
                 Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567",
                 Phone.CONTENT_ITEM_TYPE, "1234567;1234567");
+
+        // test some numbers with country and area code
+        assertCollapses("70", true,
+                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678",
+                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678");
+        assertCollapses("71", true,
+                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678",
+                Phone.CONTENT_ITEM_TYPE, "+49 (89)12345678");
+        assertCollapses("72", true,
+                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234",
+                Phone.CONTENT_ITEM_TYPE, "+49 (8092)1234");
+        assertCollapses("73", true,
+                Phone.CONTENT_ITEM_TYPE, "0049 (8092) 1234",
+                Phone.CONTENT_ITEM_TYPE, "+49/80921234");
+        assertCollapses("74", false,
+                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678",
+                Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345679");
+
+        // test some numbers with wait symbol and area code
+        assertCollapses("80", true,
+                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89321",
+                Phone.CONTENT_ITEM_TYPE, "+49/80921234;89321");
+        assertCollapses("81", false,
+                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89321",
+                Phone.CONTENT_ITEM_TYPE, "+49/80921235;89321");
+        assertCollapses("82", false,
+                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89322",
+                Phone.CONTENT_ITEM_TYPE, "+49/80921234;89321");
+        assertCollapses("83", true,
+                Phone.CONTENT_ITEM_TYPE, "1234567;+49 (8092) 1234",
+                Phone.CONTENT_ITEM_TYPE, "1234567;+49/80921234");
+
+        // this makes sure that if if two segments are identical, we don't even try to parse
+        // (and therefore allow invalid phone numbers)
+        assertCollapses("84", true,
+                Phone.CONTENT_ITEM_TYPE, "+49/80921234;a89",
+                Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;a89");
+        assertCollapses("85", false,
+                Phone.CONTENT_ITEM_TYPE, "+49/80921234;a89",
+                Phone.CONTENT_ITEM_TYPE, "+49/80921234;b89");
     }
 
-    private void checkShouldCollapse(String message, boolean expected, CharSequence mimetype1,
+    private void assertCollapses(String message, boolean expected, CharSequence mimetype1,
             CharSequence data1, CharSequence mimetype2, CharSequence data2) {
         assertEquals(message, expected,
                 ContactsUtils.shouldCollapse(mContext, mimetype1, data1, mimetype2, data2));
+        assertEquals(message, expected,
+                ContactsUtils.shouldCollapse(mContext, mimetype2, data2, mimetype1, data1));
+
+        if (data1 == data2 && data1 != null) {
+            // make sure we also do a test where object equality is not given
+            final CharSequence data2_newref = data2 + "";
+
+            // this just makes sure the test is working
+            assertFalse(data1 == data2_newref);
+
+            // we have two different instances, now make sure we get the same result as before
+            assertEquals(message, expected,
+                    ContactsUtils.shouldCollapse(mContext, mimetype1, data1, mimetype2,
+                    data2_newref));
+            assertEquals(message, expected,
+                    ContactsUtils.shouldCollapse(mContext, mimetype2, data2_newref, mimetype1,
+                    data1));
+        }
     }
 
     public void testAreIntentActionEqual() throws Exception {