Search in sources :

Example 16 with ULocale

use of android.icu.util.ULocale in project platform_frameworks_base by android.

the class ZygoteInit method beginIcuCachePinning.

private static void beginIcuCachePinning() {
    // Pin ICU data in memory from this point that would normally be held by soft references.
    // Without this, any references created immediately below or during class preloading
    // would be collected when the Zygote GC runs in gcAndFinalize().
    Log.i(TAG, "Installing ICU cache reference pinning...");
    CacheValue.setStrength(CacheValue.Strength.STRONG);
    Log.i(TAG, "Preloading ICU data...");
    // Explicitly exercise code to cache data apps are likely to need.
    ULocale[] localesToPin = { ULocale.ROOT, ULocale.US, ULocale.getDefault() };
    for (ULocale uLocale : localesToPin) {
        new DecimalFormatSymbols(uLocale);
    }
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols)

Example 17 with ULocale

use of android.icu.util.ULocale in project android_frameworks_base by DirtyUnicorns.

the class LocaleUtils method filterByLanguage.

/**
     * Filters the given items based on language preferences.
     *
     * <p>For each language found in {@code preferredLanguages}, this method tries to copy at most
     * one best-match item from {@code source} to {@code dest}.  For example, if
     * {@code "en-GB", "ja", "en-AU", "fr-CA", "en-IN"} is specified to {@code preferredLanguages},
     * this method tries to copy at most one English locale, at most one Japanese, and at most one
     * French locale from {@code source} to {@code dest}.  Here the best matching English locale
     * will be searched from {@code source} based on matching score. For the score design, see
     * {@link LocaleUtils#calculateMatchingScore(ULocale, LocaleList, byte[])}</p>
     *
     * @param sources Source items to be filtered.
     * @param extractor Type converter from the source items to {@link Locale} object.
     * @param preferredLanguages Ordered list of locales with which the input items will be
     * filtered.
     * @param dest Destination into which the filtered items will be added.
     * @param <T> Type of the data items.
     */
@VisibleForTesting
public static <T> void filterByLanguage(@NonNull List<T> sources, @NonNull LocaleExtractor<T> extractor, @NonNull LocaleList preferredLanguages, @NonNull ArrayList<T> dest) {
    final HashMap<String, ScoreEntry> scoreboard = new HashMap<>();
    final byte[] score = new byte[preferredLanguages.size()];
    final int sourceSize = sources.size();
    for (int i = 0; i < sourceSize; ++i) {
        final Locale locale = extractor.get(sources.get(i));
        if (locale == null || !calculateMatchingScore(ULocale.addLikelySubtags(ULocale.forLocale(locale)), preferredLanguages, score)) {
            continue;
        }
        final String lang = locale.getLanguage();
        final ScoreEntry bestScore = scoreboard.get(lang);
        if (bestScore == null) {
            scoreboard.put(lang, new ScoreEntry(score, i));
        } else {
            bestScore.updateIfBetter(score, i);
        }
    }
    final ScoreEntry[] result = scoreboard.values().toArray(new ScoreEntry[scoreboard.size()]);
    Arrays.sort(result);
    for (final ScoreEntry entry : result) {
        dest.add(sources.get(entry.mIndex));
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) HashMap(java.util.HashMap) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 18 with ULocale

use of android.icu.util.ULocale in project android_frameworks_base by AOSPA.

the class LocaleHelper method getDisplayName.

/**
     * Returns the locale localized for display in the provided locale.
     *
     * @param locale the locale whose name is to be displayed.
     * @param displayLocale the locale in which to display the name.
     * @param sentenceCase true if the result should be sentence-cased
     * @return the localized name of the locale.
     */
public static String getDisplayName(Locale locale, Locale displayLocale, boolean sentenceCase) {
    final ULocale displayULocale = ULocale.forLocale(displayLocale);
    String result = shouldUseDialectName(locale) ? ULocale.getDisplayNameWithDialect(locale.toLanguageTag(), displayULocale) : ULocale.getDisplayName(locale.toLanguageTag(), displayULocale);
    return sentenceCase ? toSentenceCase(result, displayLocale) : result;
}
Also used : ULocale(android.icu.util.ULocale)

Example 19 with ULocale

use of android.icu.util.ULocale in project android_frameworks_base by AOSPA.

the class ZygoteInit method beginIcuCachePinning.

private static void beginIcuCachePinning() {
    // Pin ICU data in memory from this point that would normally be held by soft references.
    // Without this, any references created immediately below or during class preloading
    // would be collected when the Zygote GC runs in gcAndFinalize().
    Log.i(TAG, "Installing ICU cache reference pinning...");
    CacheValue.setStrength(CacheValue.Strength.STRONG);
    Log.i(TAG, "Preloading ICU data...");
    // Explicitly exercise code to cache data apps are likely to need.
    ULocale[] localesToPin = { ULocale.ROOT, ULocale.US, ULocale.getDefault() };
    for (ULocale uLocale : localesToPin) {
        new DecimalFormatSymbols(uLocale);
    }
}
Also used : ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols)

Example 20 with ULocale

use of android.icu.util.ULocale in project android_frameworks_base by AOSPA.

the class LocaleUtils method filterByLanguage.

/**
     * Filters the given items based on language preferences.
     *
     * <p>For each language found in {@code preferredLanguages}, this method tries to copy at most
     * one best-match item from {@code source} to {@code dest}.  For example, if
     * {@code "en-GB", "ja", "en-AU", "fr-CA", "en-IN"} is specified to {@code preferredLanguages},
     * this method tries to copy at most one English locale, at most one Japanese, and at most one
     * French locale from {@code source} to {@code dest}.  Here the best matching English locale
     * will be searched from {@code source} based on matching score. For the score design, see
     * {@link LocaleUtils#calculateMatchingScore(ULocale, LocaleList, byte[])}</p>
     *
     * @param sources Source items to be filtered.
     * @param extractor Type converter from the source items to {@link Locale} object.
     * @param preferredLanguages Ordered list of locales with which the input items will be
     * filtered.
     * @param dest Destination into which the filtered items will be added.
     * @param <T> Type of the data items.
     */
@VisibleForTesting
public static <T> void filterByLanguage(@NonNull List<T> sources, @NonNull LocaleExtractor<T> extractor, @NonNull LocaleList preferredLanguages, @NonNull ArrayList<T> dest) {
    final HashMap<String, ScoreEntry> scoreboard = new HashMap<>();
    final byte[] score = new byte[preferredLanguages.size()];
    final int sourceSize = sources.size();
    for (int i = 0; i < sourceSize; ++i) {
        final Locale locale = extractor.get(sources.get(i));
        if (locale == null || !calculateMatchingScore(ULocale.addLikelySubtags(ULocale.forLocale(locale)), preferredLanguages, score)) {
            continue;
        }
        final String lang = locale.getLanguage();
        final ScoreEntry bestScore = scoreboard.get(lang);
        if (bestScore == null) {
            scoreboard.put(lang, new ScoreEntry(score, i));
        } else {
            bestScore.updateIfBetter(score, i);
        }
    }
    final ScoreEntry[] result = scoreboard.values().toArray(new ScoreEntry[scoreboard.size()]);
    Arrays.sort(result);
    for (final ScoreEntry entry : result) {
        dest.add(sources.get(entry.mIndex));
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) HashMap(java.util.HashMap) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Aggregations

ULocale (android.icu.util.ULocale)25 Locale (java.util.Locale)10 Paint (android.graphics.Paint)5 BreakIterator (android.icu.text.BreakIterator)5 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)5 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)5 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Segment (javax.swing.text.Segment)5