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