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