| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.Locale; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class LocaleList implements Parcelable { |
| 40 |
private final Locale[] mList; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
@NonNull |
| 45 |
private final String mStringRepresentation; |
| 46 |
|
| 47 |
private static final Locale[] sEmptyList = new Locale[0]; |
| 48 |
private static final LocaleList sEmptyLocaleList = new LocaleList(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public Locale get(int index) { |
| 57 |
return (0 <= index && index < mList.length) ? mList[index] : null; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public boolean isEmpty() { |
| 67 |
return mList.length == 0; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
@IntRange(from=0) |
| 74 |
public int size() { |
| 75 |
return mList.length; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 96 |
@Override |
| 97 |
public boolean equals(@Nullable 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; |
| 105 |
for (int i = 0; i < mList.length; i++) { |
| 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; |
| 115 |
for (int i = 0; i < mList.length; i++) { |
| 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("["); |
| 125 |
for (int i = 0; i < mList.length; i++) { |
| 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 |
|
| 135 |
@Override |
| 136 |
public int describeContents() { |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public void writeToParcel(Parcel dest, int parcelableFlags) { |
| 142 |
dest.writeString8(mStringRepresentation); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
@NonNull |
| 149 |
public String toLanguageTags() { |
| 150 |
return mStringRepresentation; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public LocaleList(@NonNull Locale... list) { |
| 163 |
if (list.length == 0) { |
| 164 |
mList = sEmptyList; |
| 165 |
mStringRepresentation = ""; |
| 166 |
} else { |
| 167 |
final ArrayList<Locale> localeList = new ArrayList<>(); |
| 168 |
final HashSet<Locale> seenLocales = new HashSet<Locale>(); |
| 169 |
final StringBuilder sb = new StringBuilder(); |
| 170 |
for (int i = 0; i < list.length; i++) { |
| 171 |
final Locale l = list[i]; |
| 172 |
if (l == null) { |
| 173 |
throw new NullPointerException("list[" + i + "] is null"); |
| 174 |
} else if (seenLocales.contains(l)) { |
| 175 |
|
| 176 |
} else { |
| 177 |
final Locale localeClone = (Locale) l.clone(); |
| 178 |
localeList.add(localeClone); |
| 179 |
sb.append(localeClone.toLanguageTag()); |
| 180 |
if (i < list.length - 1) { |
| 181 |
sb.append(','); |
| 182 |
} |
| 183 |
seenLocales.add(localeClone); |
| 184 |
} |
| 185 |
} |
| 186 |
mList = localeList.toArray(new Locale[localeList.size()]); |
| 187 |
mStringRepresentation = sb.toString(); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public LocaleList(@NonNull Locale topLocale, LocaleList otherLocales) { |
| 198 |
if (topLocale == null) { |
| 199 |
throw new NullPointerException("topLocale is null"); |
| 200 |
} |
| 201 |
|
| 202 |
final int inputLength = (otherLocales == null) ? 0 : otherLocales.mList.length; |
| 203 |
int topLocaleIndex = -1; |
| 204 |
for (int i = 0; i < inputLength; i++) { |
| 205 |
if (topLocale.equals(otherLocales.mList[i])) { |
| 206 |
topLocaleIndex = i; |
| 207 |
break; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
final int outputLength = inputLength + (topLocaleIndex == -1 ? 1 : 0); |
| 212 |
final Locale[] localeList = new Locale[outputLength]; |
| 213 |
localeList[0] = (Locale) topLocale.clone(); |
| 214 |
if (topLocaleIndex == -1) { |
| 215 |
|
| 216 |
for (int i = 0; i < inputLength; i++) { |
| 217 |
localeList[i + 1] = (Locale) otherLocales.mList[i].clone(); |
| 218 |
} |
| 219 |
} else { |
| 220 |
for (int i = 0; i < topLocaleIndex; i++) { |
| 221 |
localeList[i + 1] = (Locale) otherLocales.mList[i].clone(); |
| 222 |
} |
| 223 |
for (int i = topLocaleIndex + 1; i < inputLength; i++) { |
| 224 |
localeList[i] = (Locale) otherLocales.mList[i].clone(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
final StringBuilder sb = new StringBuilder(); |
| 229 |
for (int i = 0; i < outputLength; i++) { |
| 230 |
sb.append(localeList[i].toLanguageTag()); |
| 231 |
if (i < outputLength - 1) { |
| 232 |
sb.append(','); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
mList = localeList; |
| 237 |
mStringRepresentation = sb.toString(); |
| 238 |
} |
| 239 |
|
| 240 |
public static final @android.annotation.NonNull Parcelable.Creator<LocaleList> CREATOR |
| 241 |
= new Parcelable.Creator<LocaleList>() { |
| 242 |
@Override |
| 243 |
public LocaleList createFromParcel(Parcel source) { |
| 244 |
return LocaleList.forLanguageTags(source.readString8()); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public LocaleList[] newArray(int size) { |
| 249 |
return new LocaleList[size]; |
| 250 |
} |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
@NonNull |
| 257 |
public static LocaleList getEmptyLocaleList() { |
| 258 |
return sEmptyLocaleList; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
@NonNull |
| 268 |
public static LocaleList forLanguageTags(@Nullable String list) { |
| 269 |
if (list == null || list.equals("")) { |
| 270 |
return getEmptyLocaleList(); |
| 271 |
} else { |
| 272 |
final String[] tags = list.split(","); |
| 273 |
final Locale[] localeArray = new Locale[tags.length]; |
| 274 |
for (int i = 0; i < localeArray.length; i++) { |
| 275 |
localeArray[i] = Locale.forLanguageTag(tags[i]); |
| 276 |
} |
| 277 |
return new LocaleList(localeArray); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
private static String getLikelyScript(Locale locale) { |
| 282 |
final String script = locale.getScript(); |
| 283 |
if (!script.isEmpty()) { |
| 284 |
return script; |
| 285 |
} else { |
| 286 |
|
| 287 |
return ULocale.addLikelySubtags(ULocale.forLocale(locale)).getScript(); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
private static final String STRING_EN_XA = "en-XA"; |
| 292 |
private static final String STRING_AR_XB = "ar-XB"; |
| 293 |
private static final Locale LOCALE_EN_XA = new Locale("en", "XA"); |
| 294 |
private static final Locale LOCALE_AR_XB = new Locale("ar", "XB"); |
| 295 |
private static final int NUM_PSEUDO_LOCALES = 2; |
| 296 |
|
| 297 |
private static boolean isPseudoLocale(String locale) { |
| 298 |
return STRING_EN_XA.equals(locale) || STRING_AR_XB.equals(locale); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public static boolean isPseudoLocale(Locale locale) { |
| 306 |
return LOCALE_EN_XA.equals(locale) || LOCALE_AR_XB.equals(locale); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public static boolean isPseudoLocale(@Nullable ULocale locale) { |
| 313 |
return isPseudoLocale(locale != null ? locale.toLocale() : null); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public static boolean matchesLanguageAndScript(@SuppressLint("UseIcu") @NonNull |
| 329 |
Locale supported, @SuppressLint("UseIcu") @NonNull Locale desired) { |
| 330 |
if (supported.equals(desired)) { |
| 331 |
return true; |
| 332 |
} |
| 333 |
if (!supported.getLanguage().equals(desired.getLanguage())) { |
| 334 |
return false; |
| 335 |
} |
| 336 |
if (isPseudoLocale(supported) || isPseudoLocale(desired)) { |
| 337 |
|
| 338 |
|
| 339 |
return false; |
| 340 |
} |
| 341 |
final String supportedScr = getLikelyScript(supported); |
| 342 |
if (supportedScr.isEmpty()) { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
final String supportedRegion = supported.getCountry(); |
| 347 |
return supportedRegion.isEmpty() || supportedRegion.equals(desired.getCountry()); |
| 348 |
} |
| 349 |
final String desiredScr = getLikelyScript(desired); |
| 350 |
|
| 351 |
|
| 352 |
return supportedScr.equals(desiredScr); |
| 353 |
} |
| 354 |
|
| 355 |
private int findFirstMatchIndex(Locale supportedLocale) { |
| 356 |
for (int idx = 0; idx < mList.length; idx++) { |
| 357 |
if (matchesLanguageAndScript(supportedLocale, mList[idx])) { |
| 358 |
return idx; |
| 359 |
} |
| 360 |
} |
| 361 |
return Integer.MAX_VALUE; |
| 362 |
} |
| 363 |
|
| 364 |
private static final Locale EN_LATN = Locale.forLanguageTag("en-Latn"); |
| 365 |
|
| 366 |
private int computeFirstMatchIndex(Collection<String> supportedLocales, |
| 367 |
boolean assumeEnglishIsSupported) { |
| 368 |
if (mList.length == 1) { |
| 369 |
return 0; |
| 370 |
} |
| 371 |
if (mList.length == 0) { |
| 372 |
return -1; |
| 373 |
} |
| 374 |
|
| 375 |
int bestIndex = Integer.MAX_VALUE; |
| 376 |
|
| 377 |
if (assumeEnglishIsSupported) { |
| 378 |
final int idx = findFirstMatchIndex(EN_LATN); |
| 379 |
if (idx == 0) { |
| 380 |
return 0; |
| 381 |
} else if (idx < bestIndex) { |
| 382 |
bestIndex = idx; |
| 383 |
} |
| 384 |
} |
| 385 |
for (String languageTag : supportedLocales) { |
| 386 |
final Locale supportedLocale = Locale.forLanguageTag(languageTag); |
| 387 |
|
| 388 |
|
| 389 |
final int idx = findFirstMatchIndex(supportedLocale); |
| 390 |
if (idx == 0) { |
| 391 |
return 0; |
| 392 |
} else if (idx < bestIndex) { |
| 393 |
bestIndex = idx; |
| 394 |
} |
| 395 |
} |
| 396 |
if (bestIndex == Integer.MAX_VALUE) { |
| 397 |
|
| 398 |
return 0; |
| 399 |
} else { |
| 400 |
return bestIndex; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
private Locale computeFirstMatch(Collection<String> supportedLocales, |
| 405 |
boolean assumeEnglishIsSupported) { |
| 406 |
int bestIndex = computeFirstMatchIndex(supportedLocales, assumeEnglishIsSupported); |
| 407 |
return bestIndex == -1 ? null : mList[bestIndex]; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
@Nullable |
| 418 |
public Locale getFirstMatch(String[] supportedLocales) { |
| 419 |
return computeFirstMatch(Arrays.asList(supportedLocales), |
| 420 |
false ); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public int getFirstMatchIndex(String[] supportedLocales) { |
| 427 |
return computeFirstMatchIndex(Arrays.asList(supportedLocales), |
| 428 |
false ); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
@Nullable |
| 436 |
public Locale getFirstMatchWithEnglishSupported(String[] supportedLocales) { |
| 437 |
return computeFirstMatch(Arrays.asList(supportedLocales), |
| 438 |
true ); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public int getFirstMatchIndexWithEnglishSupported(Collection<String> supportedLocales) { |
| 445 |
return computeFirstMatchIndex(supportedLocales, true ); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public int getFirstMatchIndexWithEnglishSupported(String[] supportedLocales) { |
| 452 |
return getFirstMatchIndexWithEnglishSupported(Arrays.asList(supportedLocales)); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public static boolean isPseudoLocalesOnly(@Nullable String[] supportedLocales) { |
| 461 |
if (supportedLocales == null) { |
| 462 |
return true; |
| 463 |
} |
| 464 |
|
| 465 |
if (supportedLocales.length > NUM_PSEUDO_LOCALES + 1) { |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
return false; |
| 471 |
} |
| 472 |
for (String locale : supportedLocales) { |
| 473 |
if (!locale.isEmpty() && !isPseudoLocale(locale)) { |
| 474 |
return false; |
| 475 |
} |
| 476 |
} |
| 477 |
return true; |
| 478 |
} |
| 479 |
|
| 480 |
private final static Object sLock = new Object(); |
| 481 |
|
| 482 |
@GuardedBy("sLock") |
| 483 |
private static LocaleList sLastExplicitlySetLocaleList = null; |
| 484 |
@GuardedBy("sLock") |
| 485 |
private static LocaleList sDefaultLocaleList = null; |
| 486 |
@GuardedBy("sLock") |
| 487 |
private static LocaleList sDefaultAdjustedLocaleList = null; |
| 488 |
@GuardedBy("sLock") |
| 489 |
private static Locale sLastDefaultLocale = null; |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
@NonNull @Size(min=1) |
| 503 |
public static LocaleList getDefault() { |
| 504 |
final Locale defaultLocale = Locale.getDefault(); |
| 505 |
synchronized (sLock) { |
| 506 |
if (!defaultLocale.equals(sLastDefaultLocale)) { |
| 507 |
sLastDefaultLocale = defaultLocale; |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
if (sDefaultLocaleList != null |
| 512 |
&& defaultLocale.equals(sDefaultLocaleList.get(0))) { |
| 513 |
|
| 514 |
|
| 515 |
return sDefaultLocaleList; |
| 516 |
} |
| 517 |
sDefaultLocaleList = new LocaleList(defaultLocale, sLastExplicitlySetLocaleList); |
| 518 |
sDefaultAdjustedLocaleList = sDefaultLocaleList; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
return sDefaultLocaleList; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
@NonNull @Size(min=1) |
| 533 |
public static LocaleList getAdjustedDefault() { |
| 534 |
getDefault(); |
| 535 |
synchronized (sLock) { |
| 536 |
return sDefaultAdjustedLocaleList; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
public static void setDefault(@NonNull @Size(min=1) LocaleList locales) { |
| 548 |
setDefault(locales, 0); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 560 |
public static void setDefault(@NonNull @Size(min=1) LocaleList locales, int localeIndex) { |
| 561 |
if (locales == null) { |
| 562 |
throw new NullPointerException("locales is null"); |
| 563 |
} |
| 564 |
if (locales.isEmpty()) { |
| 565 |
throw new IllegalArgumentException("locales is empty"); |
| 566 |
} |
| 567 |
synchronized (sLock) { |
| 568 |
sLastDefaultLocale = locales.get(localeIndex); |
| 569 |
Locale.setDefault(sLastDefaultLocale); |
| 570 |
sLastExplicitlySetLocaleList = locales; |
| 571 |
sDefaultLocaleList = locales; |
| 572 |
if (localeIndex == 0) { |
| 573 |
sDefaultAdjustedLocaleList = sDefaultLocaleList; |
| 574 |
} else { |
| 575 |
sDefaultAdjustedLocaleList = new LocaleList( |
| 576 |
sLastDefaultLocale, sDefaultLocaleList); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
} |