Fix exception sorting conversations

Test: robotests
Fixes: 178618139
Change-Id: I524187a5840163d65dce35827785f3139b6fede7
diff --git a/src/com/android/settings/notification/app/ConversationListPreferenceController.java b/src/com/android/settings/notification/app/ConversationListPreferenceController.java
index afb965f..948a361 100644
--- a/src/com/android/settings/notification/app/ConversationListPreferenceController.java
+++ b/src/com/android/settings/notification/app/ConversationListPreferenceController.java
@@ -163,8 +163,16 @@
                         return o1.getNotificationChannel().getId().compareTo(
                                 o2.getNotificationChannel().getId());
                     }
-                    return sCollator.compare(o1.getShortcutInfo().getLabel(),
-                            o2.getShortcutInfo().getLabel());
+                    if (o1.getShortcutInfo().getLabel() == null
+                            && o2.getShortcutInfo().getLabel() != null) {
+                        return 1;
+                    }
+                    if (o1.getShortcutInfo().getLabel() != null
+                            && o2.getShortcutInfo().getLabel() == null) {
+                        return -1;
+                    }
+                    return sCollator.compare(o1.getShortcutInfo().getLabel().toString(),
+                            o2.getShortcutInfo().getLabel().toString());
                 }
             };
 }
diff --git a/tests/robotests/src/com/android/settings/notification/app/ConversationListPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/app/ConversationListPreferenceControllerTest.java
index 6714031..8113f22 100644
--- a/tests/robotests/src/com/android/settings/notification/app/ConversationListPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/app/ConversationListPreferenceControllerTest.java
@@ -33,6 +33,12 @@
 import android.os.Bundle;
 import android.provider.Settings;
 import android.service.notification.ConversationChannelWrapper;
+import android.text.SpannableStringBuilder;
+import android.text.Spanned;
+import android.text.style.BulletSpan;
+import android.text.style.QuoteSpan;
+import android.text.style.SubscriptSpan;
+import android.text.style.UnderlineSpan;
 
 import androidx.preference.Preference;
 import androidx.preference.PreferenceCategory;
@@ -211,6 +217,29 @@
                 ccw.getNotificationChannel().getConversationId());
     }
 
+    @Test
+    public void testCompareSpans() {
+        ConversationChannelWrapper one = new ConversationChannelWrapper();
+        String text1 = "one one";
+        SpannableStringBuilder builder1 = new SpannableStringBuilder(text1);
+        Object first1 = new SubscriptSpan();
+        builder1.setSpan(first1, 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+        ShortcutInfo s1 = new ShortcutInfo.Builder(mContext, "one").setShortLabel(
+                builder1).build();
+        one.setShortcutInfo(s1);
+
+        ConversationChannelWrapper two = new ConversationChannelWrapper();
+        String text2 = "two two";
+        SpannableStringBuilder builder2 = new SpannableStringBuilder(text2);
+        Object first2 = new SubscriptSpan();
+        builder2.setSpan(first2, 2, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+        ShortcutInfo s2 = new ShortcutInfo.Builder(mContext, "two").setShortLabel(
+                builder2).build();
+        two.setShortcutInfo(s2);
+
+        assertThat(mController.mConversationComparator.compare(one, two)).isLessThan(0);
+    }
+
     private final class TestPreferenceController extends ConversationListPreferenceController {
 
         private TestPreferenceController(Context context, NotificationBackend backend) {