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