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

Commit c96354dd authored by Jeremy Meyer's avatar Jeremy Meyer Committed by Android (Google) Code Review
Browse files

Merge "Pass all preferred locales to AssetManager" into main

parents 6519cc19 09158c69
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@@ -195,7 +195,8 @@ public class LocaleConfig implements Parcelable {
        XmlUtils.beginDocument(parser, TAG_LOCALE_CONFIG);
        int outerDepth = parser.getDepth();
        AttributeSet attrs = Xml.asAttributeSet(parser);
        Set<String> localeNames = new HashSet<String>();
        // LinkedHashSet to preserve insertion order
        Set<String> localeNames = new LinkedHashSet<>();
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if (TAG_LOCALE.equals(parser.getName())) {
                final TypedArray attributes = res.obtainAttributes(
+6 −6
Original line number Diff line number Diff line
@@ -1456,8 +1456,8 @@ public class PackageParser {

    private static AssetManager newConfiguredAssetManager() {
        AssetManager assetManager = new AssetManager();
        assetManager.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                Build.VERSION.RESOURCES_SDK_INT);
        assetManager.setConfiguration(0, 0, null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, Build.VERSION.RESOURCES_SDK_INT);
        return assetManager;
    }

@@ -9011,8 +9011,8 @@ public class PackageParser {
            }

            AssetManager assets = new AssetManager();
            assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    Build.VERSION.RESOURCES_SDK_INT);
            assets.setConfiguration(0, 0, null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, Build.VERSION.RESOURCES_SDK_INT);
            assets.setApkAssets(apkAssets, false /*invalidateCaches*/);

            mCachedAssetManager = assets;
@@ -9086,8 +9086,8 @@ public class PackageParser {

        private static AssetManager createAssetManagerWithAssets(ApkAssets[] apkAssets) {
            final AssetManager assets = new AssetManager();
            assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    Build.VERSION.RESOURCES_SDK_INT);
            assets.setConfiguration(0, 0, null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, Build.VERSION.RESOURCES_SDK_INT);
            assets.setApkAssets(apkAssets, false /*invalidateCaches*/);
            return assets;
        }
+31 −7
Original line number Diff line number Diff line
@@ -1480,15 +1480,38 @@ public final class AssetManager implements AutoCloseable {
            int screenWidth, int screenHeight, int smallestScreenWidthDp, int screenWidthDp,
            int screenHeightDp, int screenLayout, int uiMode, int colorMode, int grammaticalGender,
            int majorVersion) {
        synchronized (this) {
            ensureValidLocked();
            nativeSetConfiguration(mObject, mcc, mnc, locale, orientation, touchscreen, density,
        if (locale != null) {
            setConfiguration(mcc, mnc, null, new String[]{locale}, orientation, touchscreen,
                    density, keyboard, keyboardHidden, navigation, screenWidth, screenHeight,
                    smallestScreenWidthDp, screenWidthDp, screenHeightDp, screenLayout, uiMode,
                    colorMode, grammaticalGender, majorVersion);
        } else {
            setConfiguration(mcc, mnc, null, null, orientation, touchscreen, density,
                    keyboard, keyboardHidden, navigation, screenWidth, screenHeight,
                    smallestScreenWidthDp, screenWidthDp, screenHeightDp, screenLayout, uiMode,
                    colorMode, grammaticalGender, majorVersion);
        }
    }

    /**
     * Change the configuration used when retrieving resources.  Not for use by
     * applications.
     * @hide
     */
    public void setConfiguration(int mcc, int mnc, String defaultLocale, String[] locales,
            int orientation, int touchscreen, int density, int keyboard, int keyboardHidden,
            int navigation, int screenWidth, int screenHeight, int smallestScreenWidthDp,
            int screenWidthDp, int screenHeightDp, int screenLayout, int uiMode, int colorMode,
            int grammaticalGender, int majorVersion) {
        synchronized (this) {
            ensureValidLocked();
            nativeSetConfiguration(mObject, mcc, mnc, defaultLocale, locales, orientation,
                    touchscreen, density, keyboard, keyboardHidden, navigation, screenWidth,
                    screenHeight, smallestScreenWidthDp, screenWidthDp, screenHeightDp,
                    screenLayout, uiMode, colorMode, grammaticalGender, majorVersion);
        }
    }

    /**
     * @hide
     */
@@ -1572,10 +1595,11 @@ public final class AssetManager implements AutoCloseable {
    private static native void nativeSetApkAssets(long ptr, @NonNull ApkAssets[] apkAssets,
            boolean invalidateCaches);
    private static native void nativeSetConfiguration(long ptr, int mcc, int mnc,
            @Nullable String locale, int orientation, int touchscreen, int density, int keyboard,
            int keyboardHidden, int navigation, int screenWidth, int screenHeight,
            int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp, int screenLayout,
            int uiMode, int colorMode, int grammaticalGender, int majorVersion);
            @Nullable String defaultLocale, @NonNull String[] locales, int orientation,
            int touchscreen, int density, int keyboard, int keyboardHidden, int navigation,
            int screenWidth, int screenHeight, int smallestScreenWidthDp, int screenWidthDp,
            int screenHeightDp, int screenLayout, int uiMode, int colorMode, int grammaticalGender,
            int majorVersion);
    private static native @NonNull SparseArray<String> nativeGetAssignedPackageIdentifiers(
            long ptr, boolean includeOverlays, boolean includeLoaders);

+27 −12
Original line number Diff line number Diff line
@@ -407,14 +407,12 @@ public class ResourcesImpl {
                    mConfiguration.setLocales(locales);
                }

                String[] selectedLocales = null;
                String defaultLocale = null;
                if ((configChanges & ActivityInfo.CONFIG_LOCALE) != 0) {
                    if (locales.size() > 1) {
                        String[] availableLocales;

                        LocaleList localeList = ResourcesManager.getInstance().getLocaleList();
                        if (!localeList.isEmpty()) {
                            availableLocales = localeList.toLanguageTags().split(",");
                        } else {
                        if (ResourcesManager.getInstance().getLocaleList().isEmpty()) {
                            // The LocaleList has changed. We must query the AssetManager's
                            // available Locales and figure out the best matching Locale in the new
                            // LocaleList.
@@ -426,14 +424,30 @@ public class ResourcesImpl {
                                    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 (bestLocale != null) {
                                    selectedLocales = new String[]{
                                            adjustLanguageTag(bestLocale.toLanguageTag())};
                                    if (!bestLocale.equals(locales.get(0))) {
                                        mConfiguration.setLocales(
                                                new LocaleList(bestLocale, locales));
                                    }
                                }
                            }
                        } else {
                            selectedLocales = locales.getIntersection(
                                    ResourcesManager.getInstance().getLocaleList());
                            defaultLocale = ResourcesManager.getInstance()
                                    .getLocaleList().get(0).toLanguageTag();
                        }
                    }
                }
                if (selectedLocales == null) {
                    selectedLocales = new String[locales.size()];
                    for (int i = 0; i < locales.size(); i++) {
                        selectedLocales[i] = adjustLanguageTag(locales.get(i).toLanguageTag());
                    }
                }

@@ -470,7 +484,8 @@ public class ResourcesImpl {
                }

                mAssets.setConfiguration(mConfiguration.mcc, mConfiguration.mnc,
                        adjustLanguageTag(mConfiguration.getLocales().get(0).toLanguageTag()),
                        defaultLocale,
                        selectedLocales,
                        mConfiguration.orientation,
                        mConfiguration.touchscreen,
                        mConfiguration.densityDpi, mConfiguration.keyboard,
+20 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;

/**
@@ -150,6 +151,25 @@ public final class LocaleList implements Parcelable {
        return mStringRepresentation;
    }

    /**
     * Find the intersection between this LocaleList and another
     * @return a String array of the Locales in both LocaleLists
     * {@hide}
     */
    @NonNull
    public String[] getIntersection(@NonNull LocaleList other) {
        List<String> intersection = new ArrayList<>();
        for (Locale l1 : mList) {
            for (Locale l2 : other.mList) {
                if (matchesLanguageAndScript(l2, l1)) {
                    intersection.add(l1.toLanguageTag());
                    break;
                }
            }
        }
        return intersection.toArray(new String[0]);
    }

    /**
     * Creates a new {@link LocaleList}.
     *
Loading