Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yohei Yukawa | 23cbe85 | 2016-05-17 16:42:58 -0700 | [diff] [blame] | 17 | package android.os; |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 18 | |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 19 | import android.annotation.IntRange; |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 22 | import android.annotation.Size; |
Artur Satayev | afdb23a | 2019-12-10 17:47:53 +0000 | [diff] [blame] | 23 | import android.compat.annotation.UnsupportedAppUsage; |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 24 | import android.icu.util.ULocale; |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 25 | |
| 26 | import com.android.internal.annotations.GuardedBy; |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 27 | |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 28 | import java.util.Arrays; |
| 29 | import java.util.Collection; |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 30 | import java.util.HashSet; |
| 31 | import java.util.Locale; |
| 32 | |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 33 | /** |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 34 | * LocaleList is an immutable list of Locales, typically used to keep an ordered list of user |
| 35 | * preferences for locales. |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 36 | */ |
Yohei Yukawa | 789d8fd | 2015-12-03 11:27:05 -0800 | [diff] [blame] | 37 | public final class LocaleList implements Parcelable { |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 38 | private final Locale[] mList; |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 39 | // This is a comma-separated list of the locales in the LocaleList created at construction time, |
| 40 | // basically the result of running each locale's toLanguageTag() method and concatenating them |
| 41 | // with commas in between. |
Yohei Yukawa | 789d8fd | 2015-12-03 11:27:05 -0800 | [diff] [blame] | 42 | @NonNull |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 43 | private final String mStringRepresentation; |
| 44 | |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 45 | private static final Locale[] sEmptyList = new Locale[0]; |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 46 | private static final LocaleList sEmptyLocaleList = new LocaleList(); |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 47 | |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 48 | /** |
| 49 | * Retrieves the {@link Locale} at the specified index. |
| 50 | * |
| 51 | * @param index The position to retrieve. |
| 52 | * @return The {@link Locale} in the given index. |
| 53 | */ |
| 54 | public Locale get(int index) { |
| 55 | return (0 <= index && index < mList.length) ? mList[index] : null; |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 58 | /** |
| 59 | * Returns whether the {@link LocaleList} contains no {@link Locale} items. |
| 60 | * |
| 61 | * @return {@code true} if this {@link LocaleList} has no {@link Locale} items, {@code false} |
| 62 | * otherwise. |
| 63 | */ |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 64 | public boolean isEmpty() { |
| 65 | return mList.length == 0; |
| 66 | } |
| 67 | |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 68 | /** |
| 69 | * Returns the number of {@link Locale} items in this {@link LocaleList}. |
| 70 | */ |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 71 | @IntRange(from=0) |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 72 | public int size() { |
| 73 | return mList.length; |
| 74 | } |
| 75 | |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 76 | /** |
| 77 | * Searches this {@link LocaleList} for the specified {@link Locale} and returns the index of |
| 78 | * the first occurrence. |
| 79 | * |
| 80 | * @param locale The {@link Locale} to search for. |
| 81 | * @return The index of the first occurrence of the {@link Locale} or {@code -1} if the item |
| 82 | * wasn't found. |
| 83 | */ |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 84 | @IntRange(from=-1) |
| 85 | public int indexOf(Locale locale) { |
| 86 | for (int i = 0; i < mList.length; i++) { |
| 87 | if (mList[i].equals(locale)) { |
| 88 | return i; |
| 89 | } |
| 90 | } |
| 91 | return -1; |
| 92 | } |
| 93 | |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 94 | @Override |
| 95 | public boolean equals(Object other) { |
| 96 | if (other == this) |
| 97 | return true; |
| 98 | if (!(other instanceof LocaleList)) |
| 99 | return false; |
| 100 | final Locale[] otherList = ((LocaleList) other).mList; |
| 101 | if (mList.length != otherList.length) |
| 102 | return false; |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 103 | for (int i = 0; i < mList.length; i++) { |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 104 | if (!mList[i].equals(otherList[i])) |
| 105 | return false; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int hashCode() { |
| 112 | int result = 1; |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 113 | for (int i = 0; i < mList.length; i++) { |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 114 | result = 31 * result + mList[i].hashCode(); |
| 115 | } |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public String toString() { |
| 121 | StringBuilder sb = new StringBuilder(); |
| 122 | sb.append("["); |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 123 | for (int i = 0; i < mList.length; i++) { |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 124 | sb.append(mList[i]); |
| 125 | if (i < mList.length - 1) { |
| 126 | sb.append(','); |
| 127 | } |
| 128 | } |
| 129 | sb.append("]"); |
| 130 | return sb.toString(); |
| 131 | } |
| 132 | |
Yohei Yukawa | 789d8fd | 2015-12-03 11:27:05 -0800 | [diff] [blame] | 133 | @Override |
| 134 | public int describeContents() { |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 140 | dest.writeString(mStringRepresentation); |
| 141 | } |
| 142 | |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 143 | /** |
| 144 | * Retrieves a String representation of the language tags in this list. |
| 145 | */ |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 146 | @NonNull |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 147 | public String toLanguageTags() { |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 148 | return mStringRepresentation; |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 152 | * Creates a new {@link LocaleList}. |
| 153 | * |
| 154 | * <p>For empty lists of {@link Locale} items it is better to use {@link #getEmptyLocaleList()}, |
| 155 | * which returns a pre-constructed empty list.</p> |
| 156 | * |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 157 | * @throws NullPointerException if any of the input locales is <code>null</code>. |
| 158 | * @throws IllegalArgumentException if any of the input locales repeat. |
| 159 | */ |
Raph Levien | 10ea92a | 2016-05-02 12:56:01 -0700 | [diff] [blame] | 160 | public LocaleList(@NonNull Locale... list) { |
| 161 | if (list.length == 0) { |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 162 | mList = sEmptyList; |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 163 | mStringRepresentation = ""; |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 164 | } else { |
| 165 | final Locale[] localeList = new Locale[list.length]; |
| 166 | final HashSet<Locale> seenLocales = new HashSet<Locale>(); |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 167 | final StringBuilder sb = new StringBuilder(); |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 168 | for (int i = 0; i < list.length; i++) { |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 169 | final Locale l = list[i]; |
| 170 | if (l == null) { |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 171 | throw new NullPointerException("list[" + i + "] is null"); |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 172 | } else if (seenLocales.contains(l)) { |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 173 | throw new IllegalArgumentException("list[" + i + "] is a repetition"); |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 174 | } else { |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 175 | final Locale localeClone = (Locale) l.clone(); |
| 176 | localeList[i] = localeClone; |
| 177 | sb.append(localeClone.toLanguageTag()); |
| 178 | if (i < list.length - 1) { |
| 179 | sb.append(','); |
| 180 | } |
| 181 | seenLocales.add(localeClone); |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | mList = localeList; |
Roozbeh Pournader | f036ead | 2015-10-19 16:56:39 -0700 | [diff] [blame] | 185 | mStringRepresentation = sb.toString(); |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 188 | |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 189 | /** |
| 190 | * Constructs a locale list, with the topLocale moved to the front if it already is |
| 191 | * in otherLocales, or added to the front if it isn't. |
| 192 | * |
| 193 | * {@hide} |
| 194 | */ |
| 195 | public LocaleList(@NonNull Locale topLocale, LocaleList otherLocales) { |
| 196 | if (topLocale == null) { |
| 197 | throw new NullPointerException("topLocale is null"); |
| 198 | } |
| 199 | |
| 200 | final int inputLength = (otherLocales == null) ? 0 : otherLocales.mList.length; |
| 201 | int topLocaleIndex = -1; |
| 202 | for (int i = 0; i < inputLength; i++) { |
| 203 | if (topLocale.equals(otherLocales.mList[i])) { |
| 204 | topLocaleIndex = i; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | final int outputLength = inputLength + (topLocaleIndex == -1 ? 1 : 0); |
| 210 | final Locale[] localeList = new Locale[outputLength]; |
| 211 | localeList[0] = (Locale) topLocale.clone(); |
| 212 | if (topLocaleIndex == -1) { |
| 213 | // topLocale was not in otherLocales |
| 214 | for (int i = 0; i < inputLength; i++) { |
| 215 | localeList[i + 1] = (Locale) otherLocales.mList[i].clone(); |
| 216 | } |
| 217 | } else { |
| 218 | for (int i = 0; i < topLocaleIndex; i++) { |
| 219 | localeList[i + 1] = (Locale) otherLocales.mList[i].clone(); |
| 220 | } |
| 221 | for (int i = topLocaleIndex + 1; i < inputLength; i++) { |
| 222 | localeList[i] = (Locale) otherLocales.mList[i].clone(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | final StringBuilder sb = new StringBuilder(); |
| 227 | for (int i = 0; i < outputLength; i++) { |
| 228 | sb.append(localeList[i].toLanguageTag()); |
| 229 | if (i < outputLength - 1) { |
| 230 | sb.append(','); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | mList = localeList; |
| 235 | mStringRepresentation = sb.toString(); |
| 236 | } |
| 237 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 238 | public static final @android.annotation.NonNull Parcelable.Creator<LocaleList> CREATOR |
Yohei Yukawa | 789d8fd | 2015-12-03 11:27:05 -0800 | [diff] [blame] | 239 | = new Parcelable.Creator<LocaleList>() { |
| 240 | @Override |
| 241 | public LocaleList createFromParcel(Parcel source) { |
| 242 | return LocaleList.forLanguageTags(source.readString()); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public LocaleList[] newArray(int size) { |
| 247 | return new LocaleList[size]; |
| 248 | } |
| 249 | }; |
| 250 | |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 251 | /** |
| 252 | * Retrieve an empty instance of {@link LocaleList}. |
| 253 | */ |
Yohei Yukawa | 789d8fd | 2015-12-03 11:27:05 -0800 | [diff] [blame] | 254 | @NonNull |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 255 | public static LocaleList getEmptyLocaleList() { |
| 256 | return sEmptyLocaleList; |
| 257 | } |
| 258 | |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 259 | /** |
| 260 | * Generates a new LocaleList with the given language tags. |
| 261 | * |
| 262 | * @param list The language tags to be included as a single {@link String} separated by commas. |
| 263 | * @return A new instance with the {@link Locale} items identified by the given tags. |
| 264 | */ |
Yohei Yukawa | 789d8fd | 2015-12-03 11:27:05 -0800 | [diff] [blame] | 265 | @NonNull |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 266 | public static LocaleList forLanguageTags(@Nullable String list) { |
| 267 | if (list == null || list.equals("")) { |
| 268 | return getEmptyLocaleList(); |
| 269 | } else { |
| 270 | final String[] tags = list.split(","); |
| 271 | final Locale[] localeArray = new Locale[tags.length]; |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 272 | for (int i = 0; i < localeArray.length; i++) { |
Roozbeh Pournader | b46fdd4 | 2015-08-19 14:56:22 -0700 | [diff] [blame] | 273 | localeArray[i] = Locale.forLanguageTag(tags[i]); |
| 274 | } |
| 275 | return new LocaleList(localeArray); |
| 276 | } |
| 277 | } |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 278 | |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 279 | private static String getLikelyScript(Locale locale) { |
| 280 | final String script = locale.getScript(); |
| 281 | if (!script.isEmpty()) { |
| 282 | return script; |
| 283 | } else { |
| 284 | // TODO: Cache the results if this proves to be too slow |
| 285 | return ULocale.addLikelySubtags(ULocale.forLocale(locale)).getScript(); |
| 286 | } |
| 287 | } |
| 288 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 289 | private static final String STRING_EN_XA = "en-XA"; |
| 290 | private static final String STRING_AR_XB = "ar-XB"; |
| 291 | private static final Locale LOCALE_EN_XA = new Locale("en", "XA"); |
| 292 | private static final Locale LOCALE_AR_XB = new Locale("ar", "XB"); |
| 293 | private static final int NUM_PSEUDO_LOCALES = 2; |
| 294 | |
| 295 | private static boolean isPseudoLocale(String locale) { |
| 296 | return STRING_EN_XA.equals(locale) || STRING_AR_XB.equals(locale); |
| 297 | } |
| 298 | |
Igor Viarheichyk | 025402c | 2017-10-06 12:46:49 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Returns true if locale is a pseudo-locale, false otherwise. |
| 301 | * {@hide} |
| 302 | */ |
| 303 | public static boolean isPseudoLocale(Locale locale) { |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 304 | return LOCALE_EN_XA.equals(locale) || LOCALE_AR_XB.equals(locale); |
| 305 | } |
| 306 | |
Maurice Lam | 522ecbe | 2018-12-04 20:21:40 -0800 | [diff] [blame] | 307 | /** |
| 308 | * Returns true if locale is a pseudo-locale, false otherwise. |
Maurice Lam | 522ecbe | 2018-12-04 20:21:40 -0800 | [diff] [blame] | 309 | */ |
Maurice Lam | 522ecbe | 2018-12-04 20:21:40 -0800 | [diff] [blame] | 310 | public static boolean isPseudoLocale(@Nullable ULocale locale) { |
| 311 | return isPseudoLocale(locale != null ? locale.toLocale() : null); |
| 312 | } |
| 313 | |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 314 | @IntRange(from=0, to=1) |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 315 | private static int matchScore(Locale supported, Locale desired) { |
| 316 | if (supported.equals(desired)) { |
| 317 | return 1; // return early so we don't do unnecessary computation |
| 318 | } |
| 319 | if (!supported.getLanguage().equals(desired.getLanguage())) { |
| 320 | return 0; |
| 321 | } |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 322 | if (isPseudoLocale(supported) || isPseudoLocale(desired)) { |
| 323 | // The locales are not the same, but the languages are the same, and one of the locales |
| 324 | // is a pseudo-locale. So this is not a match. |
| 325 | return 0; |
| 326 | } |
Roozbeh Pournader | b927c55 | 2016-01-15 11:23:42 -0800 | [diff] [blame] | 327 | final String supportedScr = getLikelyScript(supported); |
| 328 | if (supportedScr.isEmpty()) { |
| 329 | // If we can't guess a script, we don't know enough about the locales' language to find |
| 330 | // if the locales match. So we fall back to old behavior of matching, which considered |
| 331 | // locales with different regions different. |
| 332 | final String supportedRegion = supported.getCountry(); |
| 333 | return (supportedRegion.isEmpty() || |
| 334 | supportedRegion.equals(desired.getCountry())) |
| 335 | ? 1 : 0; |
| 336 | } |
| 337 | final String desiredScr = getLikelyScript(desired); |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 338 | // There is no match if the two locales use different scripts. This will most imporantly |
| 339 | // take care of traditional vs simplified Chinese. |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 340 | return supportedScr.equals(desiredScr) ? 1 : 0; |
| 341 | } |
| 342 | |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 343 | private int findFirstMatchIndex(Locale supportedLocale) { |
| 344 | for (int idx = 0; idx < mList.length; idx++) { |
| 345 | final int score = matchScore(supportedLocale, mList[idx]); |
| 346 | if (score > 0) { |
| 347 | return idx; |
| 348 | } |
| 349 | } |
| 350 | return Integer.MAX_VALUE; |
| 351 | } |
| 352 | |
Roozbeh Pournader | fb9236c | 2015-12-15 23:56:11 -0800 | [diff] [blame] | 353 | private static final Locale EN_LATN = Locale.forLanguageTag("en-Latn"); |
| 354 | |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 355 | private int computeFirstMatchIndex(Collection<String> supportedLocales, |
| 356 | boolean assumeEnglishIsSupported) { |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 357 | if (mList.length == 1) { // just one locale, perhaps the most common scenario |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 358 | return 0; |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 359 | } |
| 360 | if (mList.length == 0) { // empty locale list |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 361 | return -1; |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 362 | } |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 363 | |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 364 | int bestIndex = Integer.MAX_VALUE; |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 365 | // Try English first, so we can return early if it's in the LocaleList |
| 366 | if (assumeEnglishIsSupported) { |
| 367 | final int idx = findFirstMatchIndex(EN_LATN); |
| 368 | if (idx == 0) { // We have a match on the first locale, which is good enough |
| 369 | return 0; |
| 370 | } else if (idx < bestIndex) { |
| 371 | bestIndex = idx; |
Roozbeh Pournader | fb9236c | 2015-12-15 23:56:11 -0800 | [diff] [blame] | 372 | } |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 373 | } |
| 374 | for (String languageTag : supportedLocales) { |
| 375 | final Locale supportedLocale = Locale.forLanguageTag(languageTag); |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 376 | // We expect the average length of locale lists used for locale resolution to be |
| 377 | // smaller than three, so it's OK to do this as an O(mn) algorithm. |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 378 | final int idx = findFirstMatchIndex(supportedLocale); |
| 379 | if (idx == 0) { // We have a match on the first locale, which is good enough |
| 380 | return 0; |
| 381 | } else if (idx < bestIndex) { |
| 382 | bestIndex = idx; |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 385 | if (bestIndex == Integer.MAX_VALUE) { |
| 386 | // no match was found, so we fall back to the first locale in the locale list |
| 387 | return 0; |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 388 | } else { |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 389 | return bestIndex; |
Roozbeh Pournader | 2591cc8 | 2015-12-08 22:21:24 -0800 | [diff] [blame] | 390 | } |
Roozbeh Pournader | 8bca698 | 2015-11-18 17:41:24 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 393 | private Locale computeFirstMatch(Collection<String> supportedLocales, |
| 394 | boolean assumeEnglishIsSupported) { |
| 395 | int bestIndex = computeFirstMatchIndex(supportedLocales, assumeEnglishIsSupported); |
| 396 | return bestIndex == -1 ? null : mList[bestIndex]; |
| 397 | } |
| 398 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 399 | /** |
Roozbeh Pournader | fb9236c | 2015-12-15 23:56:11 -0800 | [diff] [blame] | 400 | * Returns the first match in the locale list given an unordered array of supported locales |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 401 | * in BCP 47 format. |
Roozbeh Pournader | fb9236c | 2015-12-15 23:56:11 -0800 | [diff] [blame] | 402 | * |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 403 | * @return The first {@link Locale} from this list that appears in the given array, or |
| 404 | * {@code null} if the {@link LocaleList} is empty. |
Roozbeh Pournader | fb9236c | 2015-12-15 23:56:11 -0800 | [diff] [blame] | 405 | */ |
| 406 | @Nullable |
| 407 | public Locale getFirstMatch(String[] supportedLocales) { |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 408 | return computeFirstMatch(Arrays.asList(supportedLocales), |
| 409 | false /* assume English is not supported */); |
Roozbeh Pournader | fb9236c | 2015-12-15 23:56:11 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
Adam Lesinski | b61e405 | 2016-05-19 18:23:05 -0700 | [diff] [blame] | 413 | * {@hide} |
| 414 | */ |
| 415 | public int getFirstMatchIndex(String[] supportedLocales) { |
| 416 | return computeFirstMatchIndex(Arrays.asList(supportedLocales), |
| 417 | false /* assume English is not supported */); |
| 418 | } |
| 419 | |
| 420 | /** |
Roozbeh Pournader | fb9236c | 2015-12-15 23:56:11 -0800 | [diff] [blame] | 421 | * Same as getFirstMatch(), but with English assumed to be supported, even if it's not. |
| 422 | * {@hide} |
| 423 | */ |
| 424 | @Nullable |
| 425 | public Locale getFirstMatchWithEnglishSupported(String[] supportedLocales) { |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 426 | return computeFirstMatch(Arrays.asList(supportedLocales), |
| 427 | true /* assume English is supported */); |
Roozbeh Pournader | fb9236c | 2015-12-15 23:56:11 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 431 | * {@hide} |
| 432 | */ |
| 433 | public int getFirstMatchIndexWithEnglishSupported(Collection<String> supportedLocales) { |
| 434 | return computeFirstMatchIndex(supportedLocales, true /* assume English is supported */); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * {@hide} |
| 439 | */ |
| 440 | public int getFirstMatchIndexWithEnglishSupported(String[] supportedLocales) { |
| 441 | return getFirstMatchIndexWithEnglishSupported(Arrays.asList(supportedLocales)); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Returns true if the collection of locale tags only contains empty locales and pseudolocales. |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 446 | * Assumes that there is no repetition in the input. |
| 447 | * {@hide} |
| 448 | */ |
Adam Lesinski | b61e405 | 2016-05-19 18:23:05 -0700 | [diff] [blame] | 449 | public static boolean isPseudoLocalesOnly(@Nullable String[] supportedLocales) { |
| 450 | if (supportedLocales == null) { |
| 451 | return true; |
| 452 | } |
| 453 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 454 | if (supportedLocales.length > NUM_PSEUDO_LOCALES + 1) { |
| 455 | // This is for optimization. Since there's no repetition in the input, if we have more |
| 456 | // than the number of pseudo-locales plus one for the empty string, it's guaranteed |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 457 | // that we have some meaninful locale in the collection, so the list is not "practically |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 458 | // empty". |
| 459 | return false; |
| 460 | } |
| 461 | for (String locale : supportedLocales) { |
| 462 | if (!locale.isEmpty() && !isPseudoLocale(locale)) { |
| 463 | return false; |
| 464 | } |
| 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 469 | private final static Object sLock = new Object(); |
| 470 | |
| 471 | @GuardedBy("sLock") |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 472 | private static LocaleList sLastExplicitlySetLocaleList = null; |
| 473 | @GuardedBy("sLock") |
| 474 | private static LocaleList sDefaultLocaleList = null; |
| 475 | @GuardedBy("sLock") |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 476 | private static LocaleList sDefaultAdjustedLocaleList = null; |
| 477 | @GuardedBy("sLock") |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 478 | private static Locale sLastDefaultLocale = null; |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 479 | |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 480 | /** |
| 481 | * The result is guaranteed to include the default Locale returned by Locale.getDefault(), but |
| 482 | * not necessarily at the top of the list. The default locale not being at the top of the list |
| 483 | * is an indication that the system has set the default locale to one of the user's other |
| 484 | * preferred locales, having concluded that the primary preference is not supported but a |
| 485 | * secondary preference is. |
| 486 | * |
Clara Bayarri | 66f6bd3 | 2016-04-26 12:18:36 +0100 | [diff] [blame] | 487 | * <p>Note that the default LocaleList would change if Locale.setDefault() is called. This |
| 488 | * method takes that into account by always checking the output of Locale.getDefault() and |
| 489 | * recalculating the default LocaleList if needed.</p> |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 490 | */ |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 491 | @NonNull @Size(min=1) |
| 492 | public static LocaleList getDefault() { |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 493 | final Locale defaultLocale = Locale.getDefault(); |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 494 | synchronized (sLock) { |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 495 | if (!defaultLocale.equals(sLastDefaultLocale)) { |
| 496 | sLastDefaultLocale = defaultLocale; |
| 497 | // It's either the first time someone has asked for the default locale list, or |
| 498 | // someone has called Locale.setDefault() since we last set or adjusted the default |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 499 | // locale list. So let's recalculate the locale list. |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 500 | if (sDefaultLocaleList != null |
Roozbeh Pournader | fee4484 | 2016-02-04 15:24:24 -0800 | [diff] [blame] | 501 | && defaultLocale.equals(sDefaultLocaleList.get(0))) { |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 502 | // The default Locale has changed, but it happens to be the first locale in the |
| 503 | // default locale list, so we don't need to construct a new locale list. |
| 504 | return sDefaultLocaleList; |
| 505 | } |
| 506 | sDefaultLocaleList = new LocaleList(defaultLocale, sLastExplicitlySetLocaleList); |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 507 | sDefaultAdjustedLocaleList = sDefaultLocaleList; |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 508 | } |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 509 | // sDefaultLocaleList can't be null, since it can't be set to null by |
| 510 | // LocaleList.setDefault(), and if getDefault() is called before a call to |
| 511 | // setDefault(), sLastDefaultLocale would be null and the check above would set |
| 512 | // sDefaultLocaleList. |
| 513 | return sDefaultLocaleList; |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 514 | } |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /** |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 518 | * Returns the default locale list, adjusted by moving the default locale to its first |
| 519 | * position. |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 520 | */ |
| 521 | @NonNull @Size(min=1) |
| 522 | public static LocaleList getAdjustedDefault() { |
| 523 | getDefault(); // to recalculate the default locale list, if necessary |
| 524 | synchronized (sLock) { |
| 525 | return sDefaultAdjustedLocaleList; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 530 | * Also sets the default locale by calling Locale.setDefault() with the first locale in the |
| 531 | * list. |
| 532 | * |
| 533 | * @throws NullPointerException if the input is <code>null</code>. |
| 534 | * @throws IllegalArgumentException if the input is empty. |
| 535 | */ |
| 536 | public static void setDefault(@NonNull @Size(min=1) LocaleList locales) { |
| 537 | setDefault(locales, 0); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * This may be used directly by system processes to set the default locale list for apps. For |
| 542 | * such uses, the default locale list would always come from the user preferences, but the |
| 543 | * default locale may have been chosen to be a locale other than the first locale in the locale |
| 544 | * list (based on the locales the app supports). |
| 545 | * |
| 546 | * {@hide} |
| 547 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 548 | @UnsupportedAppUsage |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 549 | public static void setDefault(@NonNull @Size(min=1) LocaleList locales, int localeIndex) { |
| 550 | if (locales == null) { |
| 551 | throw new NullPointerException("locales is null"); |
| 552 | } |
| 553 | if (locales.isEmpty()) { |
| 554 | throw new IllegalArgumentException("locales is empty"); |
| 555 | } |
| 556 | synchronized (sLock) { |
| 557 | sLastDefaultLocale = locales.get(localeIndex); |
| 558 | Locale.setDefault(sLastDefaultLocale); |
| 559 | sLastExplicitlySetLocaleList = locales; |
| 560 | sDefaultLocaleList = locales; |
Roozbeh Pournader | 834641b | 2016-01-23 22:34:57 -0800 | [diff] [blame] | 561 | if (localeIndex == 0) { |
| 562 | sDefaultAdjustedLocaleList = sDefaultLocaleList; |
| 563 | } else { |
| 564 | sDefaultAdjustedLocaleList = new LocaleList( |
| 565 | sLastDefaultLocale, sDefaultLocaleList); |
| 566 | } |
Roozbeh Pournader | 2b5ab18 | 2016-01-06 12:22:03 -0800 | [diff] [blame] | 567 | } |
Roozbeh Pournader | a23748a | 2015-08-31 14:30:36 -0700 | [diff] [blame] | 568 | } |
Roozbeh Pournader | 0ba0d6b | 2015-08-19 11:19:51 -0700 | [diff] [blame] | 569 | } |