Merge changes I1f999162,Ib8fd7ed1,Id29bd66c

* changes:
  Fix result does not get correct locale tag.
  Temporarily disable broken PhoneInterfaceManager test
  Use supported locale replace the result from sim info.
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index c953de5..92f49e7 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -50,6 +50,7 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.LocaleList;
 import android.os.Looper;
 import android.os.Message;
 import android.os.Messenger;
@@ -7685,7 +7686,7 @@
                 if (!localeFromDefaultSim.getCountry().isEmpty()) {
                     if (DBG) log("Using locale from subId: " + subId + " locale: "
                             + localeFromDefaultSim);
-                    return localeFromDefaultSim.toLanguageTag();
+                    return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
                 } else {
                     simLanguage = localeFromDefaultSim.getLanguage();
                 }
@@ -7698,7 +7699,7 @@
             final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
             if (mccLocale != null) {
                 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
-                return mccLocale.toLanguageTag();
+                return matchLocaleFromSupportedLocaleList(phone, mccLocale);
             }
 
             if (DBG) log("No locale found - returning null");
@@ -7708,6 +7709,20 @@
         }
     }
 
+    @VisibleForTesting
+    String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
+        String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
+                phone.getContext());
+        for (String localeTag : supportedLocale) {
+            if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
+                    && TextUtils.equals(inputLocale.getCountry(),
+                    Locale.forLanguageTag(localeTag).getCountry())) {
+                return localeTag;
+            }
+        }
+        return inputLocale.toLanguageTag();
+    }
+
     private List<SubscriptionInfo> getAllSubscriptionInfoList() {
         return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
                 mApp.getAttributionTag());
diff --git a/tests/src/com/android/TelephonyTestBase.java b/tests/src/com/android/TelephonyTestBase.java
index 09abb15..ffda81b 100644
--- a/tests/src/com/android/TelephonyTestBase.java
+++ b/tests/src/com/android/TelephonyTestBase.java
@@ -24,7 +24,9 @@
 
 import com.android.internal.telephony.PhoneConfigurationManager;
 
-import org.mockito.MockitoAnnotations;
+import org.junit.Rule;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
 
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
@@ -34,11 +36,11 @@
  * Helper class to load Mockito Resources into a test.
  */
 public class TelephonyTestBase {
+    @Rule public final MockitoRule mocks = MockitoJUnit.rule();
 
     protected TestContext mContext;
 
     public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
         mContext = spy(new TestContext());
         // Set up the looper if it does not exist on the test thread.
         if (Looper.myLooper() == null) {
diff --git a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
index fe5d013..3b6d5ee 100644
--- a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
+++ b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
@@ -16,11 +16,17 @@
 
 package com.android.phone;
 
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
+import android.content.Context;
 import android.content.pm.PackageManager;
+import android.content.res.Resources;
 import android.telephony.RadioAccessFamily;
 import android.telephony.TelephonyManager;
 
@@ -36,6 +42,8 @@
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 
+import java.util.Locale;
+
 /**
  * Unit Test for CarrierConfigLoader.
  */
@@ -83,4 +91,51 @@
         verify(mPhone, never()).setAllowedNetworkTypes(
                 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER, defaultNetworkType, null);
     }
+
+    @Test
+    public void matchLocaleFromSupportedLocaleList_inputLocaleChangeToSupportedLocale_notMatched() {
+        Context context = mock(Context.class);
+        when(mPhone.getContext()).thenReturn(context);
+        Resources resources = mock(Resources.class);
+        when(context.getResources()).thenReturn(resources);
+        when(resources.getStringArray(anyInt()))
+                .thenReturn(new String[]{"fi-FI", "ff-Adlm-BF", "en-US"});
+
+        // Input empty string, then return default locale of ICU.
+        String resultInputEmpty = mPhoneInterfaceManager.matchLocaleFromSupportedLocaleList(mPhone,
+                Locale.forLanguageTag(""));
+
+        assertEquals("und", resultInputEmpty);
+
+        // Input en, then look up the matched supported locale. No matched, so return input locale.
+        String resultOnlyLanguage = mPhoneInterfaceManager.matchLocaleFromSupportedLocaleList(
+                mPhone,
+                Locale.forLanguageTag("en"));
+
+        assertEquals("en", resultOnlyLanguage);
+    }
+
+    @Test
+    public void matchLocaleFromSupportedLocaleList_inputLocaleChangeToSupportedLocale() {
+        Context context = mock(Context.class);
+        when(mPhone.getContext()).thenReturn(context);
+        Resources resources = mock(Resources.class);
+        when(context.getResources()).thenReturn(resources);
+        when(resources.getStringArray(anyInt())).thenReturn(new String[]{"zh-Hant-TW"});
+
+        // Input zh-TW, then look up the matched supported locale, zh-Hant-TW, instead.
+        String resultInputZhTw = mPhoneInterfaceManager.matchLocaleFromSupportedLocaleList(mPhone,
+                Locale.forLanguageTag("zh-TW"));
+
+        assertEquals("zh-Hant-TW", resultInputZhTw);
+
+        when(resources.getStringArray(anyInt())).thenReturn(
+                new String[]{"fi-FI", "ff-Adlm-BF", "ff-Latn-BF"});
+
+        // Input ff-BF, then find the matched supported locale, ff-Latn-BF, instead.
+        String resultFfBf = mPhoneInterfaceManager.matchLocaleFromSupportedLocaleList(mPhone,
+                Locale.forLanguageTag("ff-BF"));
+
+        assertEquals("ff-Latn-BF", resultFfBf);
+    }
 }