Search in sources :

Example 41 with LocaleList

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

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)

Example 42 with LocaleList

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

the class AutoReinflateContainer method onConfigurationChanged.

@Override
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    boolean shouldInflateLayout = false;
    final int density = newConfig.densityDpi;
    if (density != mDensity) {
        mDensity = density;
        shouldInflateLayout = true;
    }
    final LocaleList localeList = newConfig.getLocales();
    if (localeList != mLocaleList) {
        mLocaleList = localeList;
        shouldInflateLayout = true;
    }
    if (shouldInflateLayout) {
        inflateLayout();
    }
}
Also used : LocaleList(android.os.LocaleList)

Example 43 with LocaleList

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

the class ResourcesImpl method updateConfiguration.

public void updateConfiguration(Configuration config, DisplayMetrics metrics, CompatibilityInfo compat) {
    Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "ResourcesImpl#updateConfiguration");
    try {
        synchronized (mAccessLock) {
            if (false) {
                Slog.i(TAG, "**** Updating config of " + this + ": old config is " + mConfiguration + " old compat is " + mDisplayAdjustments.getCompatibilityInfo());
                Slog.i(TAG, "**** Updating config of " + this + ": new config is " + config + " new compat is " + compat);
            }
            if (compat != null) {
                mDisplayAdjustments.setCompatibilityInfo(compat);
            }
            if (metrics != null) {
                mMetrics.setTo(metrics);
            }
            // NOTE: We should re-arrange this code to create a Display
            // with the CompatibilityInfo that is used everywhere we deal
            // with the display in relation to this app, rather than
            // doing the conversion here.  This impl should be okay because
            // we make sure to return a compatible display in the places
            // where there are public APIs to retrieve the display...  but
            // it would be cleaner and more maintainable to just be
            // consistently dealing with a compatible display everywhere in
            // the framework.
            mDisplayAdjustments.getCompatibilityInfo().applyToDisplayMetrics(mMetrics);
            @Config final int configChanges = calcConfigChanges(config);
            // If even after the update there are no Locales set, grab the default locales.
            LocaleList locales = mConfiguration.getLocales();
            if (locales.isEmpty()) {
                locales = LocaleList.getDefault();
                mConfiguration.setLocales(locales);
            }
            if ((configChanges & ActivityInfo.CONFIG_LOCALE) != 0) {
                if (locales.size() > 1) {
                    // The LocaleList has changed. We must query the AssetManager's available
                    // Locales and figure out the best matching Locale in the new LocaleList.
                    String[] availableLocales = mAssets.getNonSystemLocales();
                    if (LocaleList.isPseudoLocalesOnly(availableLocales)) {
                        // No app defined locales, so grab the system locales.
                        availableLocales = mAssets.getLocales();
                        if (LocaleList.isPseudoLocalesOnly(availableLocales)) {
                            availableLocales = null;
                        }
                    }
                    if (availableLocales != null) {
                        final Locale bestLocale = locales.getFirstMatchWithEnglishSupported(availableLocales);
                        if (bestLocale != null && bestLocale != locales.get(0)) {
                            mConfiguration.setLocales(new LocaleList(bestLocale, locales));
                        }
                    }
                }
            }
            if (mConfiguration.densityDpi != Configuration.DENSITY_DPI_UNDEFINED) {
                mMetrics.densityDpi = mConfiguration.densityDpi;
                mMetrics.density = mConfiguration.densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
            }
            mMetrics.scaledDensity = mMetrics.density * mConfiguration.fontScale;
            final int width, height;
            if (mMetrics.widthPixels >= mMetrics.heightPixels) {
                width = mMetrics.widthPixels;
                height = mMetrics.heightPixels;
            } else {
                //noinspection SuspiciousNameCombination
                width = mMetrics.heightPixels;
                //noinspection SuspiciousNameCombination
                height = mMetrics.widthPixels;
            }
            final int keyboardHidden;
            if (mConfiguration.keyboardHidden == Configuration.KEYBOARDHIDDEN_NO && mConfiguration.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
                keyboardHidden = Configuration.KEYBOARDHIDDEN_SOFT;
            } else {
                keyboardHidden = mConfiguration.keyboardHidden;
            }
            mAssets.setConfiguration(mConfiguration.mcc, mConfiguration.mnc, adjustLanguageTag(mConfiguration.getLocales().get(0).toLanguageTag()), mConfiguration.orientation, mConfiguration.touchscreen, mConfiguration.densityDpi, mConfiguration.keyboard, keyboardHidden, mConfiguration.navigation, width, height, mConfiguration.smallestScreenWidthDp, mConfiguration.screenWidthDp, mConfiguration.screenHeightDp, mConfiguration.screenLayout, mConfiguration.uiMode, Build.VERSION.RESOURCES_SDK_INT);
            if (DEBUG_CONFIG) {
                Slog.i(TAG, "**** Updating config of " + this + ": final config is " + mConfiguration + " final compat is " + mDisplayAdjustments.getCompatibilityInfo());
            }
            mDrawableCache.onConfigurationChange(configChanges);
            mColorDrawableCache.onConfigurationChange(configChanges);
            mComplexColorCache.onConfigurationChange(configChanges);
            mAnimatorCache.onConfigurationChange(configChanges);
            mStateListAnimatorCache.onConfigurationChange(configChanges);
            flushLayoutCache();
        }
        synchronized (sSync) {
            if (mPluralRule != null) {
                mPluralRule = PluralRules.forLocale(mConfiguration.getLocales().get(0));
            }
        }
    } finally {
        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    }
}
Also used : Locale(java.util.Locale) LocaleList(android.os.LocaleList) Config(android.content.pm.ActivityInfo.Config)

Example 44 with LocaleList

use of android.os.LocaleList in project platform_frameworks_base by android.

the class InputManagerService method visitKeyboardLayoutsInPackage.

private void visitKeyboardLayoutsInPackage(PackageManager pm, ActivityInfo receiver, String keyboardName, int requestedPriority, KeyboardLayoutVisitor visitor) {
    Bundle metaData = receiver.metaData;
    if (metaData == null) {
        return;
    }
    int configResId = metaData.getInt(InputManager.META_DATA_KEYBOARD_LAYOUTS);
    if (configResId == 0) {
        Slog.w(TAG, "Missing meta-data '" + InputManager.META_DATA_KEYBOARD_LAYOUTS + "' on receiver " + receiver.packageName + "/" + receiver.name);
        return;
    }
    CharSequence receiverLabel = receiver.loadLabel(pm);
    String collection = receiverLabel != null ? receiverLabel.toString() : "";
    int priority;
    if ((receiver.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
        priority = requestedPriority;
    } else {
        priority = 0;
    }
    try {
        Resources resources = pm.getResourcesForApplication(receiver.applicationInfo);
        XmlResourceParser parser = resources.getXml(configResId);
        try {
            XmlUtils.beginDocument(parser, "keyboard-layouts");
            for (; ; ) {
                XmlUtils.nextElement(parser);
                String element = parser.getName();
                if (element == null) {
                    break;
                }
                if (element.equals("keyboard-layout")) {
                    TypedArray a = resources.obtainAttributes(parser, com.android.internal.R.styleable.KeyboardLayout);
                    try {
                        String name = a.getString(com.android.internal.R.styleable.KeyboardLayout_name);
                        String label = a.getString(com.android.internal.R.styleable.KeyboardLayout_label);
                        int keyboardLayoutResId = a.getResourceId(com.android.internal.R.styleable.KeyboardLayout_keyboardLayout, 0);
                        String languageTags = a.getString(com.android.internal.R.styleable.KeyboardLayout_locale);
                        LocaleList locales = getLocalesFromLanguageTags(languageTags);
                        int vid = a.getInt(com.android.internal.R.styleable.KeyboardLayout_vendorId, -1);
                        int pid = a.getInt(com.android.internal.R.styleable.KeyboardLayout_productId, -1);
                        if (name == null || label == null || keyboardLayoutResId == 0) {
                            Slog.w(TAG, "Missing required 'name', 'label' or 'keyboardLayout' " + "attributes in keyboard layout " + "resource from receiver " + receiver.packageName + "/" + receiver.name);
                        } else {
                            String descriptor = KeyboardLayoutDescriptor.format(receiver.packageName, receiver.name, name);
                            if (keyboardName == null || name.equals(keyboardName)) {
                                KeyboardLayout layout = new KeyboardLayout(descriptor, label, collection, priority, locales, vid, pid);
                                visitor.visitKeyboardLayout(resources, keyboardLayoutResId, layout);
                            }
                        }
                    } finally {
                        a.recycle();
                    }
                } else {
                    Slog.w(TAG, "Skipping unrecognized element '" + element + "' in keyboard layout resource from receiver " + receiver.packageName + "/" + receiver.name);
                }
            }
        } finally {
            parser.close();
        }
    } catch (Exception ex) {
        Slog.w(TAG, "Could not parse keyboard layout resource from receiver " + receiver.packageName + "/" + receiver.name, ex);
    }
}
Also used : LocaleList(android.os.LocaleList) XmlResourceParser(android.content.res.XmlResourceParser) Bundle(android.os.Bundle) TypedArray(android.content.res.TypedArray) Resources(android.content.res.Resources) KeyboardLayout(android.hardware.input.KeyboardLayout) SettingNotFoundException(android.provider.Settings.SettingNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(android.content.res.Resources.NotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Example 45 with LocaleList

use of android.os.LocaleList in project platform_frameworks_base by android.

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)

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