Fix system locale propagation during user creation.

Since Android N, the system locale is stored in Settings.System. Because
of this change, we need to propagate the previous user's system locale
to the newly created user.

When the user switch happens, updateUserConfigurationLocked is called
for the next user. Usually, some configuration values (font scale and
system locale) are overwritten by the next user's settings. However,
the first time the next user logs in (and only the first time), the
settings value is empty. So, we need to decide between keeping the
passed configuration's value or resetting to the default. For the
fontScale, it is reset to the default (issue 27187556). For the system
locale, the previous user's locale should be used. This CL addresses
this.

At the same time, the inherited configuration should be stored to the
settings, otherwise the inherited configuration is lost the next time
the second user logs in.

Bug: 27803966
Change-Id: I4632671316d26e00ab6fe80ff3433f097f0e0954
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 8cc165d..210abeb 100755
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -2219,19 +2219,37 @@
          * @param outConfig Where to place the configuration settings.
          */
         public static void getConfiguration(ContentResolver cr, Configuration outConfig) {
-            getConfigurationForUser(cr, outConfig, UserHandle.myUserId());
+            adjustConfigurationForUser(cr, outConfig, UserHandle.myUserId(),
+                    false /* updateSettingsIfEmpty */);
         }
 
         /** @hide */
-        public static void getConfigurationForUser(ContentResolver cr, Configuration outConfig,
-                int userHandle) {
+        public static void adjustConfigurationForUser(ContentResolver cr, Configuration outConfig,
+                int userHandle, boolean updateSettingsIfEmpty) {
             outConfig.fontScale = Settings.System.getFloatForUser(
                     cr, FONT_SCALE, DEFAULT_FONT_SCALE, userHandle);
             if (outConfig.fontScale < 0) {
                 outConfig.fontScale = DEFAULT_FONT_SCALE;
             }
-            outConfig.setLocales(LocaleList.forLanguageTags(
-                    Settings.System.getStringForUser(cr, SYSTEM_LOCALES, userHandle)));
+
+            final String localeValue =
+                    Settings.System.getStringForUser(cr, SYSTEM_LOCALES, userHandle);
+            if (localeValue != null) {
+                outConfig.setLocales(LocaleList.forLanguageTags(localeValue));
+            } else {
+                // Do not update configuration with emtpy settings since we need to take over the
+                // locale list of previous user if the settings value is empty. This happens when a
+                // new user is created.
+
+                if (updateSettingsIfEmpty) {
+                    // Make current configuration persistent. This is necessary the first time a
+                    // user log in. At the first login, the configuration settings are empty, so we
+                    // need to store the adjusted configuration as the initial settings.
+                    Settings.System.putStringForUser(
+                            cr, SYSTEM_LOCALES, outConfig.getLocales().toLanguageTags(),
+                            userHandle);
+                }
+            }
         }
 
         /**