Lightweight fix for incorrect locale changes - DO NOT MERGE

Previously, if the sim's MCC locale is reported AND
the user didn't have a persistent locale setting, then
the device locale would change.

This modifies the logic to only allow device locale changes
if the user has not explicitly set a locale and the user has
not completed setup.

An MCC-based update is still (erroneously) considered a
user-actioned change.

This is only intended to affect users that previously had an
unrecognized MCC. Android does a better job of recognizing
MCCs and we don't want those users that have received an OTA
to suddenly experience a locale change.

Bug: 19232829
Change-Id: I5b150573ec84f453bff54f3ca331b63e2c4d32d3
diff --git a/src/java/com/android/internal/telephony/MccTable.java b/src/java/com/android/internal/telephony/MccTable.java
index 95f9943..91355ae 100644
--- a/src/java/com/android/internal/telephony/MccTable.java
+++ b/src/java/com/android/internal/telephony/MccTable.java
@@ -24,6 +24,7 @@
 import android.os.Build;
 import android.os.RemoteException;
 import android.os.SystemProperties;
+import android.provider.Settings;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Slog;
@@ -223,6 +224,29 @@
         }
     }
 
+    // Bug 19232829: It is possible to get through provisioning without setting up a persistent
+    // locale value. We don't modify the locale if the device has completed "provisioning" because
+    // we don't want to change the locale if the user inserts a new SIM or a new version of Android
+    // is better at recognizing MCC values than an older version.
+    private static boolean canUpdateLocale(Context context) {
+        return !(userHasPersistedLocale() || isDeviceProvisioned(context));
+    }
+
+    private static boolean userHasPersistedLocale() {
+        String persistSysLanguage = SystemProperties.get("persist.sys.language", "");
+        String persistSysCountry = SystemProperties.get("persist.sys.country", "");
+        return !(persistSysLanguage.isEmpty() && persistSysCountry.isEmpty());
+    }
+
+    private static boolean isDeviceProvisioned(Context context) {
+        try {
+            return Settings.Global.getInt(
+                    context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED) != 0;
+        } catch (Settings.SettingNotFoundException e) {
+            return false;
+        }
+    }
+
     /**
      * Return Locale for the language and country or null if no good match.
      *
@@ -243,22 +267,12 @@
         }
 
         // Check whether a developer is trying to test an arbitrary MCC.
-        boolean debuggingMccOverride = false;
-        if (Build.IS_DEBUGGABLE) {
-            String overrideMcc = SystemProperties.get("persist.sys.override_mcc", "");
-            if (!overrideMcc.isEmpty()) {
-                debuggingMccOverride = true;
-            }
-        }
+        boolean debuggingMccOverride = isDebuggingMccOverride();
 
         // If this is a regular user and they already have a persisted locale, we're done.
-        if (!debuggingMccOverride) {
-            String persistSysLanguage = SystemProperties.get("persist.sys.language", "");
-            String persistSysCountry = SystemProperties.get("persist.sys.country", "");
-            if (!(persistSysLanguage.isEmpty() && persistSysCountry.isEmpty())) {
-                Slog.d(LOG_TAG, "getLocaleForLanguageCountry: skipping already persisted");
-                return null;
-            }
+        if (!(debuggingMccOverride || canUpdateLocale(context))) {
+            Slog.d(LOG_TAG, "getLocaleForLanguageCountry: not permitted to update locale");
+            return null;
         }
 
         // Find the best match we actually have a localization for.
@@ -315,6 +329,16 @@
         return null;
     }
 
+    private static boolean isDebuggingMccOverride() {
+        if (Build.IS_DEBUGGABLE) {
+            String overrideMcc = SystemProperties.get("persist.sys.override_mcc", "");
+            if (!overrideMcc.isEmpty()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     /**
      * Utility code to set the system locale if it's not set already
      * @param context Context to act on.