From b46fdd427c5fc2ca69506cad0b35ea805477319d Mon Sep 17 00:00:00 2001 From: Roozbeh Pournader Date: Wed, 19 Aug 2015 14:56:22 -0700 Subject: [PATCH] Make res.Configuration support locale lists. We also deprecate the locale attribute, but works around the cases in which people would call it. Also add various methods to LocaleList to support the features Configuration requires. Change-Id: Iacc537e5fc1a3d4c1ea7e5517347876ca4e07e0a --- api/current.txt | 8 +- api/system-current.txt | 8 +- .../android/content/res/Configuration.java | 210 ++++++++++++------ core/java/android/util/LocaleList.java | 89 +++++++- 4 files changed, 247 insertions(+), 68 deletions(-) diff --git a/api/current.txt b/api/current.txt index df74b45b3e037..933e4dffad8eb 100644 --- a/api/current.txt +++ b/api/current.txt @@ -9663,12 +9663,14 @@ package android.content.res { method public int diff(android.content.res.Configuration); method public boolean equals(android.content.res.Configuration); method public int getLayoutDirection(); + method public android.util.LocaleList getLocales(); method public boolean isLayoutSizeAtLeast(int); method public boolean isScreenRound(); method public static boolean needNewResources(int, int); method public void readFromParcel(android.os.Parcel); method public void setLayoutDirection(java.util.Locale); method public void setLocale(java.util.Locale); + method public void setLocales(android.util.LocaleList); method public void setTo(android.content.res.Configuration); method public void setToDefaults(); method public int updateFrom(android.content.res.Configuration); @@ -9742,7 +9744,7 @@ package android.content.res { field public int hardKeyboardHidden; field public int keyboard; field public int keyboardHidden; - field public java.util.Locale locale; + field public deprecated java.util.Locale locale; field public int mcc; field public int mnc; field public int navigation; @@ -34200,11 +34202,15 @@ package android.util { public final class LocaleList { ctor public LocaleList(); + ctor public LocaleList(java.util.Locale); ctor public LocaleList(java.util.Locale[]); + method public static android.util.LocaleList forLanguageTags(java.lang.String); method public java.util.Locale get(int); + method public static android.util.LocaleList getEmptyLocaleList(); method public java.util.Locale getPrimary(); method public boolean isEmpty(); method public int size(); + method public java.lang.String toLanguageTags(); } public final class Log { diff --git a/api/system-current.txt b/api/system-current.txt index bcae74b7c45c7..97e3bb743059b 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -10000,12 +10000,14 @@ package android.content.res { method public int diff(android.content.res.Configuration); method public boolean equals(android.content.res.Configuration); method public int getLayoutDirection(); + method public android.util.LocaleList getLocales(); method public boolean isLayoutSizeAtLeast(int); method public boolean isScreenRound(); method public static boolean needNewResources(int, int); method public void readFromParcel(android.os.Parcel); method public void setLayoutDirection(java.util.Locale); method public void setLocale(java.util.Locale); + method public void setLocales(android.util.LocaleList); method public void setTo(android.content.res.Configuration); method public void setToDefaults(); method public int updateFrom(android.content.res.Configuration); @@ -10079,7 +10081,7 @@ package android.content.res { field public int hardKeyboardHidden; field public int keyboard; field public int keyboardHidden; - field public java.util.Locale locale; + field public deprecated java.util.Locale locale; field public int mcc; field public int mnc; field public int navigation; @@ -36494,11 +36496,15 @@ package android.util { public final class LocaleList { ctor public LocaleList(); + ctor public LocaleList(java.util.Locale); ctor public LocaleList(java.util.Locale[]); + method public static android.util.LocaleList forLanguageTags(java.lang.String); method public java.util.Locale get(int); + method public static android.util.LocaleList getEmptyLocaleList(); method public java.util.Locale getPrimary(); method public boolean isEmpty(); method public int size(); + method public java.lang.String toLanguageTags(); } public final class Log { diff --git a/core/java/android/content/res/Configuration.java b/core/java/android/content/res/Configuration.java index fd6047684eb51..3ecbaa05a4f1c 100644 --- a/core/java/android/content/res/Configuration.java +++ b/core/java/android/content/res/Configuration.java @@ -22,11 +22,13 @@ import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; +import android.annotation.Nullable; import android.content.pm.ActivityInfo; import android.os.Build; import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; +import android.util.LocaleList; import android.view.View; import java.io.IOException; @@ -36,7 +38,7 @@ import java.util.Locale; /** * This class describes all device configuration information that can * impact the resources the application retrieves. This includes both - * user-specified configuration options (locale and scaling) as well + * user-specified configuration options (locale list and scaling) as well * as device configurations (such as input modes, screen size and screen orientation). *

You can acquire this object from {@link Resources}, using {@link * Resources#getConfiguration}. Thus, from an activity, you can get it by chaining the request @@ -78,8 +80,13 @@ public final class Configuration implements Parcelable, Comparablelocale * resource qualifier. + * + * @deprecated Do not set or read this directly. Use {@link #getLocales()} and + * {@link #setLocales(LocaleList)}. */ - public Locale locale; + @Deprecated public Locale locale; + + private LocaleList mLocaleList; /** * Locale should persist on setting. This is hidden because it is really @@ -648,6 +655,15 @@ public final class Configuration implements Parcelable, Comparable.equals() on the input locale and the + * {@link #locale} attribute would return true if they are not null, but there is + * no guarantee that they would be the same object. + * + * See also the note about layout direction in {@link #setLocales(LocaleList)}. * * @param loc The locale. Can be null. */ - public void setLocale(Locale loc) { - locale = loc; - setLayoutDirection(locale); + public void setLocale(@Nullable Locale loc) { + setLocales(new LocaleList(loc)); } /** @@ -1335,19 +1414,19 @@ public final class Configuration implements Parcelable, Comparablenull. + * @throws IllegalArgumentException if any of the input locales repeat. + */ + public LocaleList(@Nullable Locale locale) { + if (locale == null) { + mList = sEmptyList; + } else { + mList = new Locale[1]; + mList[0] = (Locale) locale.clone(); + } + } + /** * @throws NullPointerException if any of the input locales is null. * @throws IllegalArgumentException if any of the input locales repeat. @@ -79,4 +147,21 @@ public final class LocaleList { mList = localeList; } } + + public static LocaleList getEmptyLocaleList() { + return sEmptyLocaleList; + } + + public static LocaleList forLanguageTags(@Nullable String list) { + if (list == null || list.equals("")) { + return getEmptyLocaleList(); + } else { + final String[] tags = list.split(","); + final Locale[] localeArray = new Locale[tags.length]; + for (int i = 0; i < localeArray.length; ++i) { + localeArray[i] = Locale.forLanguageTag(tags[i]); + } + return new LocaleList(localeArray); + } + } }