Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ad4dccf authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Support ellipsizing LocaleHelper.getDisplayLocaleList()

Add an extra parameter to LocaleHelper.getDisplayLocaleList()
specifying the maximum number of locales to output, as callers
probably won't need the whole list.

Bug: 28872122
Change-Id: Ief136bc1af2841e76ed4d8e65932a9a30821eae3
parent f0dae2c5
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -67,7 +67,8 @@ public class TextUtils {
    private static final String TAG = "TextUtils";

    /* package */ static final char[] ELLIPSIS_NORMAL = { '\u2026' }; // this is "..."
    private static final String ELLIPSIS_STRING = new String(ELLIPSIS_NORMAL);
    /** {@hide} */
    public static final String ELLIPSIS_STRING = new String(ELLIPSIS_NORMAL);

    /* package */ static final char[] ELLIPSIS_TWO_DOTS = { '\u2025' }; // this is ".."
    private static final String ELLIPSIS_TWO_DOTS_STRING = new String(ELLIPSIS_TWO_DOTS);
+23 −3
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package com.android.internal.app;

import android.annotation.IntRange;
import android.icu.text.ListFormatter;
import android.icu.util.ULocale;
import android.os.LocaleList;
import android.text.TextUtils;

import java.text.Collator;
import java.util.Comparator;
@@ -153,16 +155,34 @@ public class LocaleHelper {
     * @param locales the list of locales whose names is to be displayed.
     * @param displayLocale the locale in which to display the names.
     *                      If this is null, it will use the default locale.
     * @param maxLocales maximum number of locales to display. Generates ellipsis after that.
     * @return the locale aware list of locale names
     */
    public static String getDisplayLocaleList(LocaleList locales, Locale displayLocale) {
    public static String getDisplayLocaleList(
            LocaleList locales, Locale displayLocale, @IntRange(from=1) int maxLocales) {

        final Locale dispLocale = displayLocale == null ? Locale.getDefault() : displayLocale;

        int localeCount = locales.size();
        final String[] localeNames = new String[localeCount];
        final boolean ellipsisNeeded = locales.size() > maxLocales;
        final int localeCount, listCount;
        if (ellipsisNeeded) {
            localeCount = maxLocales;
            listCount = maxLocales + 1;  // One extra slot for the ellipsis
        } else {
            listCount = localeCount = locales.size();
        }
        final String[] localeNames = new String[listCount];
        for (int i = 0; i < localeCount; i++) {
            localeNames[i] = LocaleHelper.getDisplayName(locales.get(i), dispLocale, false);
        }
        if (ellipsisNeeded) {
            // Theoretically, we want to extract this from ICU's Resource Bundle for
            // "Ellipsis/final", which seems to have different strings than the normal ellipsis for
            // Hong Kong Traditional Chinese (zh_Hant_HK) and Dzongkha (dz). But that has two
            // problems: it's expensive to extract it, and in case the output string becomes
            // automatically ellipsized, it can result in weird output.
            localeNames[maxLocales] = TextUtils.ELLIPSIS_STRING;
        }

        ListFormatter lfn = ListFormatter.getInstance(dispLocale);
        return lfn.format((Object[]) localeNames);