From 8e8d23214a71d8813ebd3676b192924c530cb913 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Fri, 24 Jun 2016 12:29:16 -0700 Subject: [PATCH] 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 --- core/java/android/app/ResourcesManager.java | 27 +++++++--- core/java/android/content/res/Resources.java | 5 +- .../android/content/res/ResourcesImpl.java | 49 ++++++------------- .../java/android/view/DisplayAdjustments.java | 2 +- .../content/res/ResourcesManagerTest.java | 4 +- 5 files changed, 39 insertions(+), 48 deletions(-) diff --git a/core/java/android/app/ResourcesManager.java b/core/java/android/app/ResourcesManager.java index cc2f62101e6d0..c4673a3b0b4cd 100644 --- a/core/java/android/app/ResourcesManager.java +++ b/core/java/android/app/ResourcesManager.java @@ -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) { diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java index 7820cbedacc70..8d3940c99e0dd 100644 --- a/core/java/android/content/res/Resources.java +++ b/core/java/android/content/res/Resources.java @@ -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()); } /** diff --git a/core/java/android/content/res/ResourcesImpl.java b/core/java/android/content/res/ResourcesImpl.java index 0f140e945f7cb..aa8039025df6e 100644 --- a/core/java/android/content/res/ResourcesImpl.java +++ b/core/java/android/content/res/ResourcesImpl.java @@ -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()); diff --git a/core/java/android/view/DisplayAdjustments.java b/core/java/android/view/DisplayAdjustments.java index 6cc0508b5832f..dd86062f2d1d2 100644 --- a/core/java/android/view/DisplayAdjustments.java +++ b/core/java/android/view/DisplayAdjustments.java @@ -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() { diff --git a/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java b/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java index 39a29074acde3..f088197609a87 100644 --- a/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java +++ b/core/tests/coretests/src/android/content/res/ResourcesManagerTest.java @@ -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; } };