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

Commit ac3e0e59 authored by Adam Lesinski's avatar Adam Lesinski
Browse files

Update DisplayMetrics when resizing

Previously the DisplayMetrics passed to a new ResourcesImpl
object would be generated from the default DisplayAdjustments.
We now use the correct DisplayAdjustments for the ResourcesImpl
and make sure to update them for things like rotation changes.

Bug:29619314
Change-Id: If2ba0d7670a4554dcd3fde9766e2337f20a191fd
(cherry picked from commit 8e8d2321)
parent c63710ea
Loading
Loading
Loading
Loading
+19 −8
Original line number Diff line number Diff line
@@ -149,17 +149,17 @@ public class ResourcesManager {
    }

    DisplayMetrics getDisplayMetrics() {
        return getDisplayMetrics(Display.DEFAULT_DISPLAY);
        return getDisplayMetrics(Display.DEFAULT_DISPLAY,
                DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS);
    }

    /**
     * Protected so that tests can override and returns something a fixed value.
     */
    @VisibleForTesting
    protected @NonNull DisplayMetrics getDisplayMetrics(int displayId) {
    protected @NonNull DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments da) {
        DisplayMetrics dm = new DisplayMetrics();
        final Display display =
                getAdjustedDisplay(displayId, DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS);
        final Display display = getAdjustedDisplay(displayId, da);
        if (display != null) {
            display.getMetrics(dm);
        } else {
@@ -304,11 +304,13 @@ public class ResourcesManager {
    }

    private @NonNull ResourcesImpl createResourcesImpl(@NonNull ResourcesKey key) {
        final DisplayAdjustments daj = new DisplayAdjustments(key.mOverrideConfiguration);
        daj.setCompatibilityInfo(key.mCompatInfo);

        final AssetManager assets = createAssetManager(key);
        final DisplayMetrics dm = getDisplayMetrics(key.mDisplayId);
        final DisplayMetrics dm = getDisplayMetrics(key.mDisplayId, daj);
        final Configuration config = generateConfig(key, dm);
        final ResourcesImpl impl = new ResourcesImpl(assets, dm, config, key.mCompatInfo,
                key.mOverrideConfiguration);
        final ResourcesImpl impl = new ResourcesImpl(assets, dm, config, daj);
        if (DEBUG) {
            Slog.d(TAG, "- creating impl=" + impl + " with key: " + key);
        }
@@ -805,7 +807,16 @@ public class ResourcesManager {
                        }
                        tmpConfig.setTo(config);
                        if (!isDefaultDisplay) {
                            dm = getDisplayMetrics(displayId);
                            // Get new DisplayMetrics based on the DisplayAdjustments given
                            // to the ResourcesImpl. Udate a copy if the CompatibilityInfo
                            // changed, because the ResourcesImpl object will handle the
                            // update internally.
                            DisplayAdjustments daj = r.getDisplayAdjustments();
                            if (compat != null) {
                                daj = new DisplayAdjustments(daj);
                                daj.setCompatibilityInfo(compat);
                            }
                            dm = getDisplayMetrics(displayId, daj);
                            applyNonDefaultDisplayMetricsToConfiguration(dm, tmpConfig);
                        }
                        if (hasOverrideConfiguration) {
+2 −3
Original line number Diff line number Diff line
@@ -209,8 +209,7 @@ public class Resources {
     */
    public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) {
        this(null);
        mResourcesImpl = new ResourcesImpl(assets, metrics, config,
                CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
        mResourcesImpl = new ResourcesImpl(assets, metrics, config, new DisplayAdjustments());
    }

    /**
@@ -238,7 +237,7 @@ public class Resources {
        config.setToDefaults();

        mResourcesImpl = new ResourcesImpl(AssetManager.getSystem(), metrics, config,
                CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
                new DisplayAdjustments());
    }

    /**
+14 −35
Original line number Diff line number Diff line
@@ -114,12 +114,11 @@ public class ResourcesImpl {

    final AssetManager mAssets;
    private final DisplayMetrics mMetrics = new DisplayMetrics();
    private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments();
    private final DisplayAdjustments mDisplayAdjustments;

    private PluralRules mPluralRule;

    private final Configuration mConfiguration = new Configuration();
    private CompatibilityInfo mCompatibilityInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;

    static {
        sPreloadedDrawables = new LongSparseArray[2];
@@ -135,37 +134,15 @@ public class ResourcesImpl {
     *                selecting/computing resource values.
     * @param config Desired device configuration to consider when
     *               selecting/computing resource values (optional).
     * @param compatInfo this resource's compatibility info. Must not be null.
     * @param displayAdjustments this resource's Display override and compatibility info.
     *                           Must not be null.
     */
    public ResourcesImpl(@NonNull AssetManager assets, @Nullable DisplayMetrics metrics,
            @Nullable Configuration config, @NonNull CompatibilityInfo compatInfo) {
        this(assets, metrics, config, compatInfo, null);
    }

    /**
     * Creates a new ResourcesImpl object with CompatibilityInfo and assigns a static overrideConfig
     * that is reported with getDisplayAdjustments(). This is used for updating the Display
     * when a new ResourcesImpl is created due to multi-window configuration changes.
     *
     * @param assets Previously created AssetManager.
     * @param metrics Current display metrics to consider when selecting/computing resource values.
     * @param fullConfig Desired device configuration to consider when selecting/computing
     * resource values.
     * @param compatInfo this resource's compatibility info. Must not be null.
     * @param overrideConfig the overrides specific to this ResourcesImpl object. They must already
     * be applied to the fullConfig and are mainly maintained in order to return a valid
     * DisplayAdjustments object during configuration changes.
     */
    public ResourcesImpl(@NonNull AssetManager assets, @Nullable DisplayMetrics metrics,
            @Nullable Configuration fullConfig, @NonNull CompatibilityInfo compatInfo,
            @Nullable Configuration overrideConfig) {
            @Nullable Configuration config, @NonNull DisplayAdjustments displayAdjustments) {
        mAssets = assets;
        mMetrics.setToDefaults();
        mDisplayAdjustments.setCompatibilityInfo(compatInfo);
        if (overrideConfig != null) {
            mDisplayAdjustments.setConfiguration(overrideConfig);
        }
        updateConfiguration(fullConfig, metrics, compatInfo);
        mDisplayAdjustments = displayAdjustments;
        updateConfiguration(config, metrics, displayAdjustments.getCompatibilityInfo());
        mAssets.ensureStringBlocks();
    }

@@ -192,7 +169,7 @@ public class ResourcesImpl {
    }

    CompatibilityInfo getCompatibilityInfo() {
        return mCompatibilityInfo;
        return mDisplayAdjustments.getCompatibilityInfo();
    }

    private PluralRules getPluralRule() {
@@ -347,12 +324,13 @@ public class ResourcesImpl {
            synchronized (mAccessLock) {
                if (false) {
                    Slog.i(TAG, "**** Updating config of " + this + ": old config is "
                            + mConfiguration + " old compat is " + mCompatibilityInfo);
                            + mConfiguration + " old compat is "
                            + mDisplayAdjustments.getCompatibilityInfo());
                    Slog.i(TAG, "**** Updating config of " + this + ": new config is "
                            + config + " new compat is " + compat);
                }
                if (compat != null) {
                    mCompatibilityInfo = compat;
                    mDisplayAdjustments.setCompatibilityInfo(compat);
                }
                if (metrics != null) {
                    mMetrics.setTo(metrics);
@@ -366,7 +344,7 @@ public class ResourcesImpl {
                // it would be cleaner and more maintainable to just be
                // consistently dealing with a compatible display everywhere in
                // the framework.
                mCompatibilityInfo.applyToDisplayMetrics(mMetrics);
                mDisplayAdjustments.getCompatibilityInfo().applyToDisplayMetrics(mMetrics);

                final @Config int configChanges = calcConfigChanges(config);

@@ -440,7 +418,8 @@ public class ResourcesImpl {

                if (DEBUG_CONFIG) {
                    Slog.i(TAG, "**** Updating config of " + this + ": final config is "
                            + mConfiguration + " final compat is " + mCompatibilityInfo);
                            + mConfiguration + " final compat is "
                            + mDisplayAdjustments.getCompatibilityInfo());
                }

                mDrawableCache.onConfigurationChange(configChanges);
@@ -480,7 +459,7 @@ public class ResourcesImpl {
            density = mMetrics.noncompatDensityDpi;
        }

        mCompatibilityInfo.applyToConfiguration(density, mTmpConfig);
        mDisplayAdjustments.getCompatibilityInfo().applyToConfiguration(density, mTmpConfig);

        if (mTmpConfig.getLocales().isEmpty()) {
            mTmpConfig.setLocales(LocaleList.getDefault());
+1 −1
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ public class DisplayAdjustments {
            throw new IllegalArgumentException(
                    "setConfiguration: Cannot modify DEFAULT_DISPLAY_ADJUSTMENTS");
        }
        mConfiguration = configuration;
        mConfiguration = configuration != null ? configuration : Configuration.EMPTY;
    }

    public Configuration getConfiguration() {
+3 −1
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import android.support.test.filters.SmallTest;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.DisplayAdjustments;

import junit.framework.TestCase;

public class ResourcesManagerTest extends TestCase {
@@ -58,7 +60,7 @@ public class ResourcesManagerTest extends TestCase {
            }

            @Override
            protected DisplayMetrics getDisplayMetrics(int displayId) {
            protected DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments daj) {
                return mDisplayMetrics;
            }
        };