Commit 6048d77c authored by Trevor Perrier's avatar Trevor Perrier Committed by Commit Bot

[Android] only use updateConfiguration for locale override

A previous CL (https://crrev.com/c/2446991) made all activity overrides
ues Resources#updateConfiguration.  This caused several bugs with
NightMode rendering and tablet view.

This CL splits the locale overrides from the other overrides and *only*
the locale overrides use the deprecated method #updateConfiguration.
All other overrides use ContextThemeWrapper#applyOverrideConfiguration.

Bug: 1139760, 1139811
Change-Id: I608cee113207987719216be317d01bc88ed07752
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2490872
Commit-Queue: Trevor  Perrier <perrier@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819938}
parent 39f8cc4a
......@@ -17,6 +17,7 @@ import org.chromium.base.ApplicationStatus;
import org.chromium.base.BuildInfo;
import org.chromium.base.CommandLineInitUtil;
import org.chromium.base.ContextUtils;
import org.chromium.base.LocaleUtils;
import org.chromium.base.PathUtils;
import org.chromium.base.TraceEvent;
import org.chromium.base.annotations.MainDex;
......@@ -80,8 +81,10 @@ public class ChromeApplication extends SplitCompatApplication {
// context to use as the base context for the application.
// Must be initialized early to override Application level localizations.
if (GlobalAppLocaleController.getInstance().init(context)) {
context = context.createConfigurationContext(
GlobalAppLocaleController.getInstance().getOverrideConfig(context));
Configuration config =
GlobalAppLocaleController.getInstance().getOverrideConfig(context);
LocaleUtils.setDefaultLocalesFromConfiguration(config);
context = context.createConfigurationContext(config);
}
}
......
......@@ -7,7 +7,6 @@ package org.chromium.chrome.browser;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.annotation.CallSuper;
......@@ -16,7 +15,6 @@ import androidx.annotation.StyleRes;
import androidx.appcompat.app.AppCompatActivity;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.browser.language.AppLocaleUtils;
import org.chromium.chrome.browser.language.GlobalAppLocaleController;
import org.chromium.chrome.browser.night_mode.GlobalNightModeStateProviderHolder;
import org.chromium.chrome.browser.night_mode.NightModeStateProvider;
......@@ -35,20 +33,15 @@ public class ChromeBaseAppCompatActivity
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
mNightModeStateProvider = createNightModeStateProvider();
AppLocaleUtils.maybeInstallActivitySplitCompat(this);
}
/**
* Update a context's configuration to match new a configuration.
* Because of an Android bug with {@link Context#createConfigurationContext} this method uses
* the deprecated method {@link Resources#updateConfiguration}. (crbug.com/1075390#c20).
* @param base Context to update configuration on.
* @param config Configuration to update context with.
*/
private void updateConfiguration(Context base, Configuration config) {
// TODO(crbug.com/1136096): Use #createConfigurationContext once that method is fixed.
Resources resources = base.getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
Configuration config = new Configuration();
// 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.
// https://crbug.com/834191
config.fontScale = 0;
// NightMode and other applyOverrides must be done before onCreate in attachBaseContext.
// https://crbug.com/1139760
if (applyOverrides(newBase, config)) applyOverrideConfiguration(config);
}
@Override
......@@ -57,12 +50,8 @@ public class ChromeBaseAppCompatActivity
mNightModeStateProvider.addObserver(this);
super.onCreate(savedInstanceState);
Configuration config = new Configuration(getResources().getConfiguration());
// 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.
// https://crbug.com/834191
config.fontScale = 0;
if (applyOverrides(this, config)) updateConfiguration(this, config);
// Activity level locale overrides must be done in onCreate.
GlobalAppLocaleController.getInstance().maybeOverrideContextConfig(this);
}
@Override
......@@ -94,11 +83,8 @@ public class ChromeBaseAppCompatActivity
*/
@CallSuper
protected boolean applyOverrides(Context baseContext, Configuration overrideConfig) {
boolean applied = NightModeUtils.applyOverridesForNightMode(
return NightModeUtils.applyOverridesForNightMode(
getNightModeStateProvider(), overrideConfig);
applied |= GlobalAppLocaleController.getInstance().applyActivityOverrides(
baseContext, overrideConfig);
return applied;
}
/**
......
......@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.language;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.TextUtils;
import org.chromium.base.LocaleUtils;
......@@ -42,8 +43,8 @@ public class GlobalAppLocaleController {
}
/**
* If the application should be overridden returns the override Configuration and updates the
* default LocaleList/Locale. Called early in {@link ChromeActivity#attachBaseContext}.
* If the application locale should be overridden returns an updated override Configuration.
* Called early in {@link ChromeActivity#attachBaseContext}.
* @param base The base Context for the application and has the system locales.
* @return Configuration to override application context with or null.
*/
......@@ -57,24 +58,24 @@ public class GlobalAppLocaleController {
config.fontScale = 0;
LocaleUtils.updateConfig(base, config, mOverrideLanguage);
LocaleUtils.setDefaultLocalesFromConfiguration(config);
return config;
}
/**
* Do the Activity level locale override if app locale preference is set.
* Should be called from {@link Activity#attachBaseContext()}.
* @param base The base Context for the Activity which has the system locales.
* @param config The Configuration that will be used to update the Activity.
* @return boolean Whether or not config was modified.
* If the locale should be overridden update the context configuration to use new locale.
* @param base Context to update.
*/
public boolean applyActivityOverrides(Context base, Configuration config) {
public void maybeOverrideContextConfig(Context base) {
if (!mIsOverridden) {
return false;
return;
}
LocaleUtils.updateConfig(base, config, mOverrideLanguage);
return true;
Configuration config = getOverrideConfig(base);
Resources resources = base.getResources();
// Because of an Android bug with {@link Context#createConfigurationContext} the deprecated
// method {@link Resources#updateConfiguration} is used. (crbug.com/1075390#c20).
// TODO(crbug.com/1136096): Use #createConfigurationContext once that method is fixed.
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
/**
......
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