Allow the SIM country code to supplement the SIM language

The SIM language preferences take priority - but they only store
a language (e.g. fr = French), not an exact locale
(e.g. fr_FR = French/France).
So, the SIM MCC is used as a supplement to provide an exact locale.

Bug: 22054390
Bug: 21942138

(cherry picked from commit 581ed0704ebaba7043b112d1e39acc7bf1c8d498)

Change-Id: I87a3c1dd12832ac73fdafefefbb3281ad1bc7321
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 359535e..de794de 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -2727,21 +2727,36 @@
 
         // Try and fetch the locale from the carrier properties or from the SIM language
         // preferences (EF-PL and EF-LI)...
+        final int mcc = info.getMcc();
+        final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc);
         final Phone defaultPhone = getPhone(info.getSubscriptionId());
         if (defaultPhone != null) {
             final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
             if (localeFromDefaultSim != null) {
-                return localeFromDefaultSim.toLanguageTag();
+                // The SIM language preferences only store a language (e.g. fr = French), not an
+                // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
+                // the SIM and carrier preferences does not include a country we add the country
+                // determined from the SIM MCC to provide an exact locale.
+                // Note this can result in unusual locale combinatons (e.g. en_DE) being returned.
+                if ((localeFromDefaultSim.getCountry().isEmpty()) && (mccLocale != null)) {
+                    final Locale combinedLocale = new Locale (localeFromDefaultSim.getLanguage(),
+                                                              mccLocale.getCountry());
+                    if (DBG) log("Using SIM language and mcc country:" + combinedLocale);
+                    return combinedLocale.toLanguageTag();
+                } else {
+                    if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
+                    return localeFromDefaultSim.toLanguageTag();
+                }
             }
         }
 
         // .. if that doesn't work, try and guess the language from the sim MCC.
-        final int mcc = info.getMcc();
-        final Locale locale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc);
-        if (locale != null) {
-            return locale.toLanguageTag();
+        if (mccLocale != null) {
+            if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
+            return mccLocale.toLanguageTag();
         }
 
+        if (DBG) log("No locale found - returning null");
         return null;
     }