Fix group message title when switching to RTL languages

Generated titles should not be reused after changing the locale.
Because:
1 - Change of locale means none of the separators used are valid
anymore. For example a string like "name, name2, name3" cannot be used
in a language that does not use "," as a separator.
2 - All the text direction markers should be reassessed when the locale
changes. Please note that TextDirectionHeuristics.LOCALE translates
to either RLM or LRM based on the current locale. The generated string
should not be used in another locale with a different text direction.

Bug: 159221409
Bug: 161947401
Test: Manual
Change-Id: I725f23a306ff97889d08879c44a38c94f128d326
diff --git a/src/com/android/car/messenger/MessageNotificationDelegate.java b/src/com/android/car/messenger/MessageNotificationDelegate.java
index 8541c5d..5fa6db0 100644
--- a/src/com/android/car/messenger/MessageNotificationDelegate.java
+++ b/src/com/android/car/messenger/MessageNotificationDelegate.java
@@ -59,6 +59,7 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
@@ -75,6 +76,7 @@
     /** Tracks whether a projection application is active in the foreground. **/
     private ProjectionStateListener mProjectionStateListener;
     private CompletableFuture<Void> mPhoneNumberInfoFuture;
+    private Locale mGeneratedGroupConversationTitlesLocale;
     private static int mBitmapSize;
     private static float mCornerRadiusPercent;
     private static boolean mShouldLoadExistingMessages;
@@ -396,6 +398,13 @@
      */
     private void setGroupConversationTitle(ConversationKey conversationKey) {
         ConversationNotificationInfo notificationInfo = mNotificationInfos.get(conversationKey);
+        Locale locale = Locale.getDefault();
+
+        // Do not reuse the old titles if locale has changed. The new locale might need different
+        // formatting or text direction.
+        if (locale != mGeneratedGroupConversationTitlesLocale) {
+            mGeneratedGroupConversationTitles.clear();
+        }
         if (!notificationInfo.isGroupConvo()
                 || mGeneratedGroupConversationTitles.contains(conversationKey)) {
             return;
@@ -416,7 +425,10 @@
 
         notificationInfo.setConvoTitle(Utils.constructGroupConversationTitle(names,
                 mContext.getString(R.string.name_separator), mNotificationConversationTitleLength));
-        if (allNamesLoaded) mGeneratedGroupConversationTitles.add(conversationKey);
+        if (allNamesLoaded) {
+            mGeneratedGroupConversationTitlesLocale = locale;
+            mGeneratedGroupConversationTitles.add(conversationKey);
+        }
     }
 
     private void loadPhoneNumberInfo(@Nullable String phoneNumber,