Commit 1c7aa4e5 authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Android: Use createConfigurationContext() instead of applyOverrideConfiguration()

Attempting to roll appcompat support library, and it looks like the next version
breaks applyOverrideConfiguration(). Specifically,
AppCompatActivity.attachBaseContext() is now calling getResources(),
which triggers an assert in applyOverrideConfiguration() since it cannot
be called after a call to getResources().

I believe createConfigurationContext() should be functionally equivalent
to applyOverrideConfiguration().

Bug: 1064727
Change-Id: I2cd1f4c348b7b8e3815ae33bc8708f79308d40b6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2157676
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#762128}
parent 7f28d24d
...@@ -29,16 +29,16 @@ public class ChromeBaseAppCompatActivity ...@@ -29,16 +29,16 @@ public class ChromeBaseAppCompatActivity
private @StyleRes int mThemeResId; private @StyleRes int mThemeResId;
@Override @Override
protected void attachBaseContext(Context newBase) { protected void attachBaseContext(Context baseContext) {
super.attachBaseContext(newBase);
mNightModeStateProvider = createNightModeStateProvider(); mNightModeStateProvider = createNightModeStateProvider();
Configuration config = new Configuration();
// Pre-Android O, fontScale gets initialized to 1 in the constructor. Set it to 0 so // Pre-Android O, fontScale gets initialized to 1 in the constructor. Set it to 0 so
// that applyOverrideConfiguration() does not interpret it as an overridden value. // that it is not interpreted as an overridden value.
// https://crbug.com/834191 // https://crbug.com/834191
config.fontScale = 0; Configuration overrideConfig = new Configuration();
if (applyOverrides(newBase, config)) applyOverrideConfiguration(config); overrideConfig.fontScale = 0;
applyConfigurationOverrides(baseContext, overrideConfig);
super.attachBaseContext(baseContext.createConfigurationContext(overrideConfig));
} }
@Override @Override
...@@ -67,18 +67,13 @@ public class ChromeBaseAppCompatActivity ...@@ -67,18 +67,13 @@ public class ChromeBaseAppCompatActivity
} }
/** /**
* Called during {@link #attachBaseContext(Context)} to allow configuration overrides to be * Called during {@link #attachBaseContext(Context)} to allow for configuration overrides.
* applied. If this methods return true, the overrides will be applied using
* {@link #applyOverrideConfiguration(Configuration)}.
* @param baseContext The base {@link Context} attached to this class. * @param baseContext The base {@link Context} attached to this class.
* @param overrideConfig The {@link Configuration} that will be passed to * @return A Configuration object with overrides set.
* @link #applyOverrideConfiguration(Configuration)} if necessary.
* @return True if any configuration overrides were applied, and false otherwise.
*/ */
@CallSuper @CallSuper
protected boolean applyOverrides(Context baseContext, Configuration overrideConfig) { protected void applyConfigurationOverrides(Context baseContext, Configuration overrideConfig) {
return NightModeUtils.applyOverridesForNightMode( NightModeUtils.applyOverridesForNightMode(mNightModeStateProvider, overrideConfig);
getNightModeStateProvider(), overrideConfig);
} }
/** /**
......
...@@ -120,19 +120,22 @@ public abstract class AsyncInitializationActivity extends ChromeBaseAppCompatAct ...@@ -120,19 +120,22 @@ public abstract class AsyncInitializationActivity extends ChromeBaseAppCompatAct
@Override @Override
@CallSuper @CallSuper
protected boolean applyOverrides(Context baseContext, Configuration overrideConfig) { protected void applyConfigurationOverrides(Context baseContext, Configuration overrideConfig) {
super.applyOverrides(baseContext, overrideConfig); super.applyConfigurationOverrides(baseContext, overrideConfig);
// Before Android M, an IllegalStateException is thrown for trying to access DisplayManager
// We override the smallestScreenWidthDp here for two reasons: // before attachBaseContext() has been called. Multi-window wasn't added until N, so it is
// 1. To prevent multi-window from hiding the tabstrip when on a tablet. // unlikely for multi-window to be a large issue on Lollipop.
// 2. To ensure mIsTablet only needs to be set once. Since the override lasts for the life if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// of the activity, it will never change via onConfigurationUpdated(). // We override the smallestScreenWidthDp here for two reasons:
// See crbug.com/588838, crbug.com/662338, crbug.com/780593. // 1. To prevent multi-window from hiding the tabstrip when on a tablet.
DisplayAndroid display = DisplayAndroid.getNonMultiDisplay(baseContext); // 2. To ensure mIsTablet only needs to be set once. Since the override lasts for the
int targetSmallestScreenWidthDp = // life of the activity, it will never change via onConfigurationUpdated().
DisplayUtil.pxToDp(display, DisplayUtil.getSmallestWidth(display)); // See crbug.com/588838, crbug.com/662338, crbug.com/780593.
overrideConfig.smallestScreenWidthDp = targetSmallestScreenWidthDp; DisplayAndroid display = DisplayAndroid.getNonMultiDisplay(baseContext);
return true; int targetSmallestScreenWidthDp =
DisplayUtil.pxToDp(display, DisplayUtil.getSmallestWidth(display));
overrideConfig.smallestScreenWidthDp = targetSmallestScreenWidthDp;
}
} }
@Override @Override
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment