Search in sources :

Example 26 with LocaleList

use of android.os.LocaleList in project android_frameworks_base by AOSPA.

the class InputManagerService method getDefaultKeyboardLayout.

private String getDefaultKeyboardLayout(final InputDevice d) {
    final Locale systemLocale = mContext.getResources().getConfiguration().locale;
    // reasonable default.
    if (TextUtils.isEmpty(systemLocale.getLanguage())) {
        return null;
    }
    final List<KeyboardLayout> layouts = new ArrayList<>();
    visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {

        @Override
        public void visitKeyboardLayout(Resources resources, int keyboardLayoutResId, KeyboardLayout layout) {
            // means its a custom layout for a specific keyboard.
            if (layout.getVendorId() != d.getVendorId() || layout.getProductId() != d.getProductId()) {
                return;
            }
            final LocaleList locales = layout.getLocales();
            final int numLocales = locales.size();
            for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
                if (isCompatibleLocale(systemLocale, locales.get(localeIndex))) {
                    layouts.add(layout);
                    break;
                }
            }
        }
    });
    if (layouts.isEmpty()) {
        return null;
    }
    // First sort so that ones with higher priority are listed at the top
    Collections.sort(layouts);
    // Next we want to try to find an exact match of language, country and variant.
    final int N = layouts.size();
    for (int i = 0; i < N; i++) {
        KeyboardLayout layout = layouts.get(i);
        final LocaleList locales = layout.getLocales();
        final int numLocales = locales.size();
        for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
            final Locale locale = locales.get(localeIndex);
            if (locale.getCountry().equals(systemLocale.getCountry()) && locale.getVariant().equals(systemLocale.getVariant())) {
                return layout.getDescriptor();
            }
        }
    }
    // Then try an exact match of language and country
    for (int i = 0; i < N; i++) {
        KeyboardLayout layout = layouts.get(i);
        final LocaleList locales = layout.getLocales();
        final int numLocales = locales.size();
        for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
            final Locale locale = locales.get(localeIndex);
            if (locale.getCountry().equals(systemLocale.getCountry())) {
                return layout.getDescriptor();
            }
        }
    }
    // Give up and just use the highest priority layout with matching language
    return layouts.get(0).getDescriptor();
}
Also used : Locale(java.util.Locale) LocaleList(android.os.LocaleList) ArrayList(java.util.ArrayList) KeyboardLayout(android.hardware.input.KeyboardLayout) Resources(android.content.res.Resources)

Example 27 with LocaleList

use of android.os.LocaleList in project android_frameworks_base by ResurrectionRemix.

the class LocaleUtilsTest method testFilterByLanguageEmptyLanguageList.

@SmallTest
public void testFilterByLanguageEmptyLanguageList() throws Exception {
    final ArrayList<Locale> availableLocales = new ArrayList<>();
    availableLocales.add(Locale.forLanguageTag("en-US"));
    availableLocales.add(Locale.forLanguageTag("fr-CA"));
    availableLocales.add(Locale.forLanguageTag("in"));
    availableLocales.add(Locale.forLanguageTag("ja"));
    availableLocales.add(Locale.forLanguageTag("fil"));
    final LocaleList preferredLocales = LocaleList.getEmptyLocaleList();
    final ArrayList<Locale> dest = new ArrayList<>();
    LocaleUtils.filterByLanguage(availableLocales, sIdentityMapper, preferredLocales, dest);
    assertEquals(0, dest.size());
}
Also used : Locale(java.util.Locale) LocaleList(android.os.LocaleList) ArrayList(java.util.ArrayList) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 28 with LocaleList

use of android.os.LocaleList in project android_frameworks_base by ResurrectionRemix.

the class InputManagerService method getDefaultKeyboardLayout.

private String getDefaultKeyboardLayout(final InputDevice d) {
    final Locale systemLocale = mContext.getResources().getConfiguration().locale;
    // reasonable default.
    if (TextUtils.isEmpty(systemLocale.getLanguage())) {
        return null;
    }
    final List<KeyboardLayout> layouts = new ArrayList<>();
    visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {

        @Override
        public void visitKeyboardLayout(Resources resources, int keyboardLayoutResId, KeyboardLayout layout) {
            // means its a custom layout for a specific keyboard.
            if (layout.getVendorId() != d.getVendorId() || layout.getProductId() != d.getProductId()) {
                return;
            }
            final LocaleList locales = layout.getLocales();
            final int numLocales = locales.size();
            for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
                if (isCompatibleLocale(systemLocale, locales.get(localeIndex))) {
                    layouts.add(layout);
                    break;
                }
            }
        }
    });
    if (layouts.isEmpty()) {
        return null;
    }
    // First sort so that ones with higher priority are listed at the top
    Collections.sort(layouts);
    // Next we want to try to find an exact match of language, country and variant.
    final int N = layouts.size();
    for (int i = 0; i < N; i++) {
        KeyboardLayout layout = layouts.get(i);
        final LocaleList locales = layout.getLocales();
        final int numLocales = locales.size();
        for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
            final Locale locale = locales.get(localeIndex);
            if (locale.getCountry().equals(systemLocale.getCountry()) && locale.getVariant().equals(systemLocale.getVariant())) {
                return layout.getDescriptor();
            }
        }
    }
    // Then try an exact match of language and country
    for (int i = 0; i < N; i++) {
        KeyboardLayout layout = layouts.get(i);
        final LocaleList locales = layout.getLocales();
        final int numLocales = locales.size();
        for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
            final Locale locale = locales.get(localeIndex);
            if (locale.getCountry().equals(systemLocale.getCountry())) {
                return layout.getDescriptor();
            }
        }
    }
    // Give up and just use the highest priority layout with matching language
    return layouts.get(0).getDescriptor();
}
Also used : Locale(java.util.Locale) LocaleList(android.os.LocaleList) ArrayList(java.util.ArrayList) KeyboardLayout(android.hardware.input.KeyboardLayout) Resources(android.content.res.Resources)

Example 29 with LocaleList

use of android.os.LocaleList in project android_frameworks_base by ResurrectionRemix.

the class ResourcesLocaleTest method testEnglishIsAlwaysConsideredSupported.

@SmallTest
public void testEnglishIsAlwaysConsideredSupported() throws Exception {
    final Resources resources = createResourcesWithApk(R.raw.locales);
    ensureNoLanguage(resources, "en");
    final LocaleList preferredLocales = LocaleList.forLanguageTags("en-US,pl-PL");
    final Configuration config = new Configuration();
    config.setLocales(preferredLocales);
    resources.updateConfiguration(config, null);
    // The APK we loaded has default and Polish languages. If English is first in the list,
    // always take it the default (assumed to be English).
    assertEquals(Locale.forLanguageTag("en-US"), resources.getConfiguration().getLocales().get(0));
}
Also used : LocaleList(android.os.LocaleList) SmallTest(android.support.test.filters.SmallTest)

Example 30 with LocaleList

use of android.os.LocaleList in project android_frameworks_base by ResurrectionRemix.

the class ActivityThread method updateLocaleListFromAppContext.

/**
     * The LocaleList set for the app's resources may have been shuffled so that the preferred
     * Locale is at position 0. We must find the index of this preferred Locale in the
     * original LocaleList.
     */
private void updateLocaleListFromAppContext(Context context, LocaleList newLocaleList) {
    final Locale bestLocale = context.getResources().getConfiguration().getLocales().get(0);
    final int newLocaleListSize = newLocaleList.size();
    for (int i = 0; i < newLocaleListSize; i++) {
        if (bestLocale.equals(newLocaleList.get(i))) {
            LocaleList.setDefault(newLocaleList, i);
            return;
        }
    }
    // The app may have overridden the LocaleList with its own Locale
    // (not present in the available list). Push the chosen Locale
    // to the front of the list.
    LocaleList.setDefault(new LocaleList(bestLocale, newLocaleList));
}
Also used : Locale(java.util.Locale) LocaleList(android.os.LocaleList)

Aggregations

LocaleList (android.os.LocaleList)91 Locale (java.util.Locale)39 ArrayList (java.util.ArrayList)31 SmallTest (android.test.suitebuilder.annotation.SmallTest)15 Resources (android.content.res.Resources)10 KeyboardLayout (android.hardware.input.KeyboardLayout)10 SmallTest (android.support.test.filters.SmallTest)10 InputMethodSubtype (android.view.inputmethod.InputMethodSubtype)10 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)7 RemoteException (android.os.RemoteException)7 FileNotFoundException (java.io.FileNotFoundException)7 IOException (java.io.IOException)7 Context (android.content.Context)5 Config (android.content.pm.ActivityInfo.Config)5 NotFoundException (android.content.res.Resources.NotFoundException)5 TypedArray (android.content.res.TypedArray)5 XmlResourceParser (android.content.res.XmlResourceParser)5 Bundle (android.os.Bundle)5 SettingNotFoundException (android.provider.Settings.SettingNotFoundException)5 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)5