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