Merge "API for launching activities to the side in side-by-side mode."
diff --git a/api/current.txt b/api/current.txt
index 43002e2..d03cf15 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -9968,6 +9968,7 @@
     method public java.lang.String getQuantityString(int, int, java.lang.Object...) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getQuantityString(int, int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.CharSequence getQuantityText(int, int) throws android.content.res.Resources.NotFoundException;
+    method public java.util.Locale getResolvedLocale();
     method public java.lang.String getResourceEntryName(int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getResourceName(int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getResourcePackageName(int) throws android.content.res.Resources.NotFoundException;
diff --git a/api/system-current.txt b/api/system-current.txt
index c7ee972..780a6774 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -10329,6 +10329,7 @@
     method public java.lang.String getQuantityString(int, int, java.lang.Object...) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getQuantityString(int, int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.CharSequence getQuantityText(int, int) throws android.content.res.Resources.NotFoundException;
+    method public java.util.Locale getResolvedLocale();
     method public java.lang.String getResourceEntryName(int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getResourceName(int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getResourcePackageName(int) throws android.content.res.Resources.NotFoundException;
diff --git a/api/test-current.txt b/api/test-current.txt
index f911b44..00abd31 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -9968,6 +9968,7 @@
     method public java.lang.String getQuantityString(int, int, java.lang.Object...) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getQuantityString(int, int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.CharSequence getQuantityText(int, int) throws android.content.res.Resources.NotFoundException;
+    method public java.util.Locale getResolvedLocale();
     method public java.lang.String getResourceEntryName(int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getResourceName(int) throws android.content.res.Resources.NotFoundException;
     method public java.lang.String getResourcePackageName(int) throws android.content.res.Resources.NotFoundException;
diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java
index cdca8698..60c6e82 100644
--- a/core/java/android/content/res/Resources.java
+++ b/core/java/android/content/res/Resources.java
@@ -160,6 +160,7 @@
     final DisplayMetrics mMetrics = new DisplayMetrics();
 
     private final Configuration mConfiguration = new Configuration();
+    private Locale mResolvedLocale = null;
     private PluralRules mPluralRule;
 
     private CompatibilityInfo mCompatibilityInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
@@ -315,6 +316,16 @@
     }
 
     /**
+     * Return the Locale resulting from locale negotiation between the Resources and the
+     * Configuration objects used to construct the Resources. The locale is used for retrieving
+     * resources as well as for determining plural rules.
+     */
+    @NonNull
+    public Locale getResolvedLocale() {
+        return mResolvedLocale;
+    }
+
+    /**
      * Return the string value associated with a particular resource ID.  The
      * returned object will be a String if this is a plain string; it will be
      * some other type of CharSequence if it is styled.
@@ -378,7 +389,7 @@
     private PluralRules getPluralRule() {
         synchronized (sSync) {
             if (mPluralRule == null) {
-                mPluralRule = PluralRules.forLocale(mConfiguration.getLocales().getPrimary());
+                mPluralRule = PluralRules.forLocale(mResolvedLocale);
             }
             return mPluralRule;
         }
@@ -441,7 +452,7 @@
     @NonNull
     public String getString(@StringRes int id, Object... formatArgs) throws NotFoundException {
         final String raw = getString(id);
-        return String.format(mConfiguration.getLocales().getPrimary(), raw, formatArgs);
+        return String.format(mResolvedLocale, raw, formatArgs);
     }
 
     /**
@@ -472,7 +483,7 @@
     public String getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs)
             throws NotFoundException {
         String raw = getQuantityText(id, quantity).toString();
-        return String.format(mConfiguration.getLocales().getPrimary(), raw, formatArgs);
+        return String.format(mResolvedLocale, raw, formatArgs);
     }
 
     /**
@@ -1931,8 +1942,10 @@
             mCompatibilityInfo.applyToDisplayMetrics(mMetrics);
 
             final int configChanges = calcConfigChanges(config);
+
             LocaleList locales = mConfiguration.getLocales();
-            if (locales.isEmpty()) {
+            final boolean setLocalesToDefault = locales.isEmpty();
+            if (setLocalesToDefault) {
                 locales = LocaleList.getDefault();
                 mConfiguration.setLocales(locales);
             }
@@ -1961,9 +1974,12 @@
                 keyboardHidden = mConfiguration.keyboardHidden;
             }
 
-            // TODO: Pass the whole locale list to setConfiguration()
+            if (setLocalesToDefault || mResolvedLocale == null
+                    || (configChanges & Configuration.NATIVE_CONFIG_LOCALE) != 0) {
+                mResolvedLocale = locales.getFirstMatch(mAssets.getLocales());
+            }
             mAssets.setConfiguration(mConfiguration.mcc, mConfiguration.mnc,
-                    adjustLanguageTag(locales.getPrimary().toLanguageTag()),
+                    adjustLanguageTag(mResolvedLocale.toLanguageTag()),
                     mConfiguration.orientation,
                     mConfiguration.touchscreen,
                     mConfiguration.densityDpi, mConfiguration.keyboard,
@@ -1988,7 +2004,7 @@
         }
         synchronized (sSync) {
             if (mPluralRule != null) {
-                mPluralRule = PluralRules.forLocale(mConfiguration.getLocales().getPrimary());
+                mPluralRule = PluralRules.forLocale(mResolvedLocale);
             }
         }
     }