blob: 87e1b7d21f53336e4ddfedd588c6282dc0d989b6 [file] [log] [blame]
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -07001/*
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 Yukawa23cbe852016-05-17 16:42:58 -070017package android.os;
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070018
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -080019import android.annotation.IntRange;
Roozbeh Pournadera23748a2015-08-31 14:30:36 -070020import android.annotation.NonNull;
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070021import android.annotation.Nullable;
Roozbeh Pournadera23748a2015-08-31 14:30:36 -070022import android.annotation.Size;
Dianne Hackbornbf5ba6b2018-02-20 10:31:02 -080023import android.content.LocaleProto;
Roozbeh Pournader2591cc82015-12-08 22:21:24 -080024import android.icu.util.ULocale;
Dianne Hackbornbf5ba6b2018-02-20 10:31:02 -080025import android.util.proto.ProtoOutputStream;
Roozbeh Pournadera23748a2015-08-31 14:30:36 -070026
27import com.android.internal.annotations.GuardedBy;
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070028
Roozbeh Pournader834641b2016-01-23 22:34:57 -080029import java.util.Arrays;
30import java.util.Collection;
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070031import java.util.HashSet;
32import java.util.Locale;
33
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070034/**
Clara Bayarri66f6bd32016-04-26 12:18:36 +010035 * LocaleList is an immutable list of Locales, typically used to keep an ordered list of user
36 * preferences for locales.
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070037 */
Yohei Yukawa789d8fd2015-12-03 11:27:05 -080038public final class LocaleList implements Parcelable {
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070039 private final Locale[] mList;
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -070040 // This is a comma-separated list of the locales in the LocaleList created at construction time,
41 // basically the result of running each locale's toLanguageTag() method and concatenating them
42 // with commas in between.
Yohei Yukawa789d8fd2015-12-03 11:27:05 -080043 @NonNull
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -070044 private final String mStringRepresentation;
45
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070046 private static final Locale[] sEmptyList = new Locale[0];
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -070047 private static final LocaleList sEmptyLocaleList = new LocaleList();
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070048
Clara Bayarri66f6bd32016-04-26 12:18:36 +010049 /**
50 * Retrieves the {@link Locale} at the specified index.
51 *
52 * @param index The position to retrieve.
53 * @return The {@link Locale} in the given index.
54 */
55 public Locale get(int index) {
56 return (0 <= index && index < mList.length) ? mList[index] : null;
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070057 }
58
Clara Bayarri66f6bd32016-04-26 12:18:36 +010059 /**
60 * Returns whether the {@link LocaleList} contains no {@link Locale} items.
61 *
62 * @return {@code true} if this {@link LocaleList} has no {@link Locale} items, {@code false}
63 * otherwise.
64 */
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070065 public boolean isEmpty() {
66 return mList.length == 0;
67 }
68
Clara Bayarri66f6bd32016-04-26 12:18:36 +010069 /**
70 * Returns the number of {@link Locale} items in this {@link LocaleList}.
71 */
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -080072 @IntRange(from=0)
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -070073 public int size() {
74 return mList.length;
75 }
76
Clara Bayarri66f6bd32016-04-26 12:18:36 +010077 /**
78 * Searches this {@link LocaleList} for the specified {@link Locale} and returns the index of
79 * the first occurrence.
80 *
81 * @param locale The {@link Locale} to search for.
82 * @return The index of the first occurrence of the {@link Locale} or {@code -1} if the item
83 * wasn't found.
84 */
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -080085 @IntRange(from=-1)
86 public int indexOf(Locale locale) {
87 for (int i = 0; i < mList.length; i++) {
88 if (mList[i].equals(locale)) {
89 return i;
90 }
91 }
92 return -1;
93 }
94
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -070095 @Override
96 public boolean equals(Object other) {
97 if (other == this)
98 return true;
99 if (!(other instanceof LocaleList))
100 return false;
101 final Locale[] otherList = ((LocaleList) other).mList;
102 if (mList.length != otherList.length)
103 return false;
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800104 for (int i = 0; i < mList.length; i++) {
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700105 if (!mList[i].equals(otherList[i]))
106 return false;
107 }
108 return true;
109 }
110
111 @Override
112 public int hashCode() {
113 int result = 1;
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800114 for (int i = 0; i < mList.length; i++) {
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700115 result = 31 * result + mList[i].hashCode();
116 }
117 return result;
118 }
119
120 @Override
121 public String toString() {
122 StringBuilder sb = new StringBuilder();
123 sb.append("[");
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800124 for (int i = 0; i < mList.length; i++) {
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700125 sb.append(mList[i]);
126 if (i < mList.length - 1) {
127 sb.append(',');
128 }
129 }
130 sb.append("]");
131 return sb.toString();
132 }
133
Yohei Yukawa789d8fd2015-12-03 11:27:05 -0800134 @Override
135 public int describeContents() {
136 return 0;
137 }
138
139 @Override
140 public void writeToParcel(Parcel dest, int parcelableFlags) {
141 dest.writeString(mStringRepresentation);
142 }
143
Clara Bayarri66f6bd32016-04-26 12:18:36 +0100144 /**
Dianne Hackbornbf5ba6b2018-02-20 10:31:02 -0800145 * Helper to write LocaleList to a protocol buffer output stream. Assumes the parent
146 * protobuf has declared the locale as repeated.
147 *
148 * @param protoOutputStream Stream to write the locale to.
149 * @param fieldId Field Id of the Locale as defined in the parent message.
150 * @hide
151 */
152 public void writeToProto(ProtoOutputStream protoOutputStream, long fieldId) {
153 for (int i = 0; i < mList.length; i++) {
154 final Locale locale = mList[i];
155 final long token = protoOutputStream.start(fieldId);
156 protoOutputStream.write(LocaleProto.LANGUAGE, locale.getLanguage());
157 protoOutputStream.write(LocaleProto.COUNTRY, locale.getCountry());
158 protoOutputStream.write(LocaleProto.VARIANT, locale.getVariant());
159 protoOutputStream.end(token);
160 }
161 }
162
163 /**
Clara Bayarri66f6bd32016-04-26 12:18:36 +0100164 * Retrieves a String representation of the language tags in this list.
165 */
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -0700166 @NonNull
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700167 public String toLanguageTags() {
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -0700168 return mStringRepresentation;
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700169 }
170
171 /**
Clara Bayarri66f6bd32016-04-26 12:18:36 +0100172 * Creates a new {@link LocaleList}.
173 *
174 * <p>For empty lists of {@link Locale} items it is better to use {@link #getEmptyLocaleList()},
175 * which returns a pre-constructed empty list.</p>
176 *
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700177 * @throws NullPointerException if any of the input locales is <code>null</code>.
178 * @throws IllegalArgumentException if any of the input locales repeat.
179 */
Raph Levien10ea92a2016-05-02 12:56:01 -0700180 public LocaleList(@NonNull Locale... list) {
181 if (list.length == 0) {
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700182 mList = sEmptyList;
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -0700183 mStringRepresentation = "";
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700184 } else {
185 final Locale[] localeList = new Locale[list.length];
186 final HashSet<Locale> seenLocales = new HashSet<Locale>();
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -0700187 final StringBuilder sb = new StringBuilder();
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800188 for (int i = 0; i < list.length; i++) {
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700189 final Locale l = list[i];
190 if (l == null) {
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800191 throw new NullPointerException("list[" + i + "] is null");
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700192 } else if (seenLocales.contains(l)) {
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800193 throw new IllegalArgumentException("list[" + i + "] is a repetition");
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700194 } else {
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -0700195 final Locale localeClone = (Locale) l.clone();
196 localeList[i] = localeClone;
197 sb.append(localeClone.toLanguageTag());
198 if (i < list.length - 1) {
199 sb.append(',');
200 }
201 seenLocales.add(localeClone);
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700202 }
203 }
204 mList = localeList;
Roozbeh Pournaderf036ead2015-10-19 16:56:39 -0700205 mStringRepresentation = sb.toString();
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700206 }
207 }
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700208
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800209 /**
210 * Constructs a locale list, with the topLocale moved to the front if it already is
211 * in otherLocales, or added to the front if it isn't.
212 *
213 * {@hide}
214 */
215 public LocaleList(@NonNull Locale topLocale, LocaleList otherLocales) {
216 if (topLocale == null) {
217 throw new NullPointerException("topLocale is null");
218 }
219
220 final int inputLength = (otherLocales == null) ? 0 : otherLocales.mList.length;
221 int topLocaleIndex = -1;
222 for (int i = 0; i < inputLength; i++) {
223 if (topLocale.equals(otherLocales.mList[i])) {
224 topLocaleIndex = i;
225 break;
226 }
227 }
228
229 final int outputLength = inputLength + (topLocaleIndex == -1 ? 1 : 0);
230 final Locale[] localeList = new Locale[outputLength];
231 localeList[0] = (Locale) topLocale.clone();
232 if (topLocaleIndex == -1) {
233 // topLocale was not in otherLocales
234 for (int i = 0; i < inputLength; i++) {
235 localeList[i + 1] = (Locale) otherLocales.mList[i].clone();
236 }
237 } else {
238 for (int i = 0; i < topLocaleIndex; i++) {
239 localeList[i + 1] = (Locale) otherLocales.mList[i].clone();
240 }
241 for (int i = topLocaleIndex + 1; i < inputLength; i++) {
242 localeList[i] = (Locale) otherLocales.mList[i].clone();
243 }
244 }
245
246 final StringBuilder sb = new StringBuilder();
247 for (int i = 0; i < outputLength; i++) {
248 sb.append(localeList[i].toLanguageTag());
249 if (i < outputLength - 1) {
250 sb.append(',');
251 }
252 }
253
254 mList = localeList;
255 mStringRepresentation = sb.toString();
256 }
257
Yohei Yukawa789d8fd2015-12-03 11:27:05 -0800258 public static final Parcelable.Creator<LocaleList> CREATOR
259 = new Parcelable.Creator<LocaleList>() {
260 @Override
261 public LocaleList createFromParcel(Parcel source) {
262 return LocaleList.forLanguageTags(source.readString());
263 }
264
265 @Override
266 public LocaleList[] newArray(int size) {
267 return new LocaleList[size];
268 }
269 };
270
Clara Bayarri66f6bd32016-04-26 12:18:36 +0100271 /**
272 * Retrieve an empty instance of {@link LocaleList}.
273 */
Yohei Yukawa789d8fd2015-12-03 11:27:05 -0800274 @NonNull
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700275 public static LocaleList getEmptyLocaleList() {
276 return sEmptyLocaleList;
277 }
278
Clara Bayarri66f6bd32016-04-26 12:18:36 +0100279 /**
280 * Generates a new LocaleList with the given language tags.
281 *
282 * @param list The language tags to be included as a single {@link String} separated by commas.
283 * @return A new instance with the {@link Locale} items identified by the given tags.
284 */
Yohei Yukawa789d8fd2015-12-03 11:27:05 -0800285 @NonNull
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700286 public static LocaleList forLanguageTags(@Nullable String list) {
287 if (list == null || list.equals("")) {
288 return getEmptyLocaleList();
289 } else {
290 final String[] tags = list.split(",");
291 final Locale[] localeArray = new Locale[tags.length];
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800292 for (int i = 0; i < localeArray.length; i++) {
Roozbeh Pournaderb46fdd42015-08-19 14:56:22 -0700293 localeArray[i] = Locale.forLanguageTag(tags[i]);
294 }
295 return new LocaleList(localeArray);
296 }
297 }
Roozbeh Pournadera23748a2015-08-31 14:30:36 -0700298
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800299 private static String getLikelyScript(Locale locale) {
300 final String script = locale.getScript();
301 if (!script.isEmpty()) {
302 return script;
303 } else {
304 // TODO: Cache the results if this proves to be too slow
305 return ULocale.addLikelySubtags(ULocale.forLocale(locale)).getScript();
306 }
307 }
308
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800309 private static final String STRING_EN_XA = "en-XA";
310 private static final String STRING_AR_XB = "ar-XB";
311 private static final Locale LOCALE_EN_XA = new Locale("en", "XA");
312 private static final Locale LOCALE_AR_XB = new Locale("ar", "XB");
313 private static final int NUM_PSEUDO_LOCALES = 2;
314
315 private static boolean isPseudoLocale(String locale) {
316 return STRING_EN_XA.equals(locale) || STRING_AR_XB.equals(locale);
317 }
318
Igor Viarheichyk025402c2017-10-06 12:46:49 -0700319 /**
320 * Returns true if locale is a pseudo-locale, false otherwise.
321 * {@hide}
322 */
323 public static boolean isPseudoLocale(Locale locale) {
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800324 return LOCALE_EN_XA.equals(locale) || LOCALE_AR_XB.equals(locale);
325 }
326
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800327 @IntRange(from=0, to=1)
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800328 private static int matchScore(Locale supported, Locale desired) {
329 if (supported.equals(desired)) {
330 return 1; // return early so we don't do unnecessary computation
331 }
332 if (!supported.getLanguage().equals(desired.getLanguage())) {
333 return 0;
334 }
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800335 if (isPseudoLocale(supported) || isPseudoLocale(desired)) {
336 // The locales are not the same, but the languages are the same, and one of the locales
337 // is a pseudo-locale. So this is not a match.
338 return 0;
339 }
Roozbeh Pournaderb927c552016-01-15 11:23:42 -0800340 final String supportedScr = getLikelyScript(supported);
341 if (supportedScr.isEmpty()) {
342 // If we can't guess a script, we don't know enough about the locales' language to find
343 // if the locales match. So we fall back to old behavior of matching, which considered
344 // locales with different regions different.
345 final String supportedRegion = supported.getCountry();
346 return (supportedRegion.isEmpty() ||
347 supportedRegion.equals(desired.getCountry()))
348 ? 1 : 0;
349 }
350 final String desiredScr = getLikelyScript(desired);
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800351 // There is no match if the two locales use different scripts. This will most imporantly
352 // take care of traditional vs simplified Chinese.
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800353 return supportedScr.equals(desiredScr) ? 1 : 0;
354 }
355
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800356 private int findFirstMatchIndex(Locale supportedLocale) {
357 for (int idx = 0; idx < mList.length; idx++) {
358 final int score = matchScore(supportedLocale, mList[idx]);
359 if (score > 0) {
360 return idx;
361 }
362 }
363 return Integer.MAX_VALUE;
364 }
365
Roozbeh Pournaderfb9236c2015-12-15 23:56:11 -0800366 private static final Locale EN_LATN = Locale.forLanguageTag("en-Latn");
367
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800368 private int computeFirstMatchIndex(Collection<String> supportedLocales,
369 boolean assumeEnglishIsSupported) {
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800370 if (mList.length == 1) { // just one locale, perhaps the most common scenario
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800371 return 0;
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800372 }
373 if (mList.length == 0) { // empty locale list
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800374 return -1;
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800375 }
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800376
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800377 int bestIndex = Integer.MAX_VALUE;
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800378 // Try English first, so we can return early if it's in the LocaleList
379 if (assumeEnglishIsSupported) {
380 final int idx = findFirstMatchIndex(EN_LATN);
381 if (idx == 0) { // We have a match on the first locale, which is good enough
382 return 0;
383 } else if (idx < bestIndex) {
384 bestIndex = idx;
Roozbeh Pournaderfb9236c2015-12-15 23:56:11 -0800385 }
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800386 }
387 for (String languageTag : supportedLocales) {
388 final Locale supportedLocale = Locale.forLanguageTag(languageTag);
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800389 // We expect the average length of locale lists used for locale resolution to be
390 // smaller than three, so it's OK to do this as an O(mn) algorithm.
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800391 final int idx = findFirstMatchIndex(supportedLocale);
392 if (idx == 0) { // We have a match on the first locale, which is good enough
393 return 0;
394 } else if (idx < bestIndex) {
395 bestIndex = idx;
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800396 }
397 }
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800398 if (bestIndex == Integer.MAX_VALUE) {
399 // no match was found, so we fall back to the first locale in the locale list
400 return 0;
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800401 } else {
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800402 return bestIndex;
Roozbeh Pournader2591cc82015-12-08 22:21:24 -0800403 }
Roozbeh Pournader8bca6982015-11-18 17:41:24 -0800404 }
405
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800406 private Locale computeFirstMatch(Collection<String> supportedLocales,
407 boolean assumeEnglishIsSupported) {
408 int bestIndex = computeFirstMatchIndex(supportedLocales, assumeEnglishIsSupported);
409 return bestIndex == -1 ? null : mList[bestIndex];
410 }
411
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800412 /**
Roozbeh Pournaderfb9236c2015-12-15 23:56:11 -0800413 * Returns the first match in the locale list given an unordered array of supported locales
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800414 * in BCP 47 format.
Roozbeh Pournaderfb9236c2015-12-15 23:56:11 -0800415 *
Clara Bayarri66f6bd32016-04-26 12:18:36 +0100416 * @return The first {@link Locale} from this list that appears in the given array, or
417 * {@code null} if the {@link LocaleList} is empty.
Roozbeh Pournaderfb9236c2015-12-15 23:56:11 -0800418 */
419 @Nullable
420 public Locale getFirstMatch(String[] supportedLocales) {
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800421 return computeFirstMatch(Arrays.asList(supportedLocales),
422 false /* assume English is not supported */);
Roozbeh Pournaderfb9236c2015-12-15 23:56:11 -0800423 }
424
425 /**
Adam Lesinskib61e4052016-05-19 18:23:05 -0700426 * {@hide}
427 */
428 public int getFirstMatchIndex(String[] supportedLocales) {
429 return computeFirstMatchIndex(Arrays.asList(supportedLocales),
430 false /* assume English is not supported */);
431 }
432
433 /**
Roozbeh Pournaderfb9236c2015-12-15 23:56:11 -0800434 * Same as getFirstMatch(), but with English assumed to be supported, even if it's not.
435 * {@hide}
436 */
437 @Nullable
438 public Locale getFirstMatchWithEnglishSupported(String[] supportedLocales) {
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800439 return computeFirstMatch(Arrays.asList(supportedLocales),
440 true /* assume English is supported */);
Roozbeh Pournaderfb9236c2015-12-15 23:56:11 -0800441 }
442
443 /**
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800444 * {@hide}
445 */
446 public int getFirstMatchIndexWithEnglishSupported(Collection<String> supportedLocales) {
447 return computeFirstMatchIndex(supportedLocales, true /* assume English is supported */);
448 }
449
450 /**
451 * {@hide}
452 */
453 public int getFirstMatchIndexWithEnglishSupported(String[] supportedLocales) {
454 return getFirstMatchIndexWithEnglishSupported(Arrays.asList(supportedLocales));
455 }
456
457 /**
458 * Returns true if the collection of locale tags only contains empty locales and pseudolocales.
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800459 * Assumes that there is no repetition in the input.
460 * {@hide}
461 */
Adam Lesinskib61e4052016-05-19 18:23:05 -0700462 public static boolean isPseudoLocalesOnly(@Nullable String[] supportedLocales) {
463 if (supportedLocales == null) {
464 return true;
465 }
466
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800467 if (supportedLocales.length > NUM_PSEUDO_LOCALES + 1) {
468 // This is for optimization. Since there's no repetition in the input, if we have more
469 // than the number of pseudo-locales plus one for the empty string, it's guaranteed
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800470 // that we have some meaninful locale in the collection, so the list is not "practically
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800471 // empty".
472 return false;
473 }
474 for (String locale : supportedLocales) {
475 if (!locale.isEmpty() && !isPseudoLocale(locale)) {
476 return false;
477 }
478 }
479 return true;
480 }
481
Roozbeh Pournadera23748a2015-08-31 14:30:36 -0700482 private final static Object sLock = new Object();
483
484 @GuardedBy("sLock")
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800485 private static LocaleList sLastExplicitlySetLocaleList = null;
486 @GuardedBy("sLock")
487 private static LocaleList sDefaultLocaleList = null;
488 @GuardedBy("sLock")
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800489 private static LocaleList sDefaultAdjustedLocaleList = null;
490 @GuardedBy("sLock")
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800491 private static Locale sLastDefaultLocale = null;
Roozbeh Pournadera23748a2015-08-31 14:30:36 -0700492
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800493 /**
494 * The result is guaranteed to include the default Locale returned by Locale.getDefault(), but
495 * not necessarily at the top of the list. The default locale not being at the top of the list
496 * is an indication that the system has set the default locale to one of the user's other
497 * preferred locales, having concluded that the primary preference is not supported but a
498 * secondary preference is.
499 *
Clara Bayarri66f6bd32016-04-26 12:18:36 +0100500 * <p>Note that the default LocaleList would change if Locale.setDefault() is called. This
501 * method takes that into account by always checking the output of Locale.getDefault() and
502 * recalculating the default LocaleList if needed.</p>
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800503 */
Roozbeh Pournadera23748a2015-08-31 14:30:36 -0700504 @NonNull @Size(min=1)
505 public static LocaleList getDefault() {
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800506 final Locale defaultLocale = Locale.getDefault();
Roozbeh Pournadera23748a2015-08-31 14:30:36 -0700507 synchronized (sLock) {
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800508 if (!defaultLocale.equals(sLastDefaultLocale)) {
509 sLastDefaultLocale = defaultLocale;
510 // It's either the first time someone has asked for the default locale list, or
511 // someone has called Locale.setDefault() since we last set or adjusted the default
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800512 // locale list. So let's recalculate the locale list.
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800513 if (sDefaultLocaleList != null
Roozbeh Pournaderfee44842016-02-04 15:24:24 -0800514 && defaultLocale.equals(sDefaultLocaleList.get(0))) {
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800515 // The default Locale has changed, but it happens to be the first locale in the
516 // default locale list, so we don't need to construct a new locale list.
517 return sDefaultLocaleList;
518 }
519 sDefaultLocaleList = new LocaleList(defaultLocale, sLastExplicitlySetLocaleList);
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800520 sDefaultAdjustedLocaleList = sDefaultLocaleList;
Roozbeh Pournadera23748a2015-08-31 14:30:36 -0700521 }
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800522 // sDefaultLocaleList can't be null, since it can't be set to null by
523 // LocaleList.setDefault(), and if getDefault() is called before a call to
524 // setDefault(), sLastDefaultLocale would be null and the check above would set
525 // sDefaultLocaleList.
526 return sDefaultLocaleList;
Roozbeh Pournadera23748a2015-08-31 14:30:36 -0700527 }
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800528 }
529
530 /**
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800531 * Returns the default locale list, adjusted by moving the default locale to its first
532 * position.
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800533 */
534 @NonNull @Size(min=1)
535 public static LocaleList getAdjustedDefault() {
536 getDefault(); // to recalculate the default locale list, if necessary
537 synchronized (sLock) {
538 return sDefaultAdjustedLocaleList;
539 }
540 }
541
542 /**
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800543 * Also sets the default locale by calling Locale.setDefault() with the first locale in the
544 * list.
545 *
546 * @throws NullPointerException if the input is <code>null</code>.
547 * @throws IllegalArgumentException if the input is empty.
548 */
549 public static void setDefault(@NonNull @Size(min=1) LocaleList locales) {
550 setDefault(locales, 0);
551 }
552
553 /**
554 * This may be used directly by system processes to set the default locale list for apps. For
555 * such uses, the default locale list would always come from the user preferences, but the
556 * default locale may have been chosen to be a locale other than the first locale in the locale
557 * list (based on the locales the app supports).
558 *
559 * {@hide}
560 */
561 public static void setDefault(@NonNull @Size(min=1) LocaleList locales, int localeIndex) {
562 if (locales == null) {
563 throw new NullPointerException("locales is null");
564 }
565 if (locales.isEmpty()) {
566 throw new IllegalArgumentException("locales is empty");
567 }
568 synchronized (sLock) {
569 sLastDefaultLocale = locales.get(localeIndex);
570 Locale.setDefault(sLastDefaultLocale);
571 sLastExplicitlySetLocaleList = locales;
572 sDefaultLocaleList = locales;
Roozbeh Pournader834641b2016-01-23 22:34:57 -0800573 if (localeIndex == 0) {
574 sDefaultAdjustedLocaleList = sDefaultLocaleList;
575 } else {
576 sDefaultAdjustedLocaleList = new LocaleList(
577 sLastDefaultLocale, sDefaultLocaleList);
578 }
Roozbeh Pournader2b5ab182016-01-06 12:22:03 -0800579 }
Roozbeh Pournadera23748a2015-08-31 14:30:36 -0700580 }
Roozbeh Pournader0ba0d6b2015-08-19 11:19:51 -0700581}