1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.app;
import static android.app.ActivityThread.DEBUG_CONFIGURATION;
import static android.window.ConfigurationHelper.freeTextLayoutCachesIfNeeded;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.HardwareRenderer;
import android.os.LocaleList;
import android.os.Trace;
import android.util.DisplayMetrics;
import android.util.Slog;
import android.view.ContextThemeWrapper;
import android.view.WindowManagerGlobal;
import com.android.internal.annotations.GuardedBy;
import java.util.ArrayList;
import java.util.Locale;
/**
* A client side controller to handle process level configuration changes.
* @hide
*/
class ConfigurationController {
private static final String TAG = "ConfigurationController";
private final ActivityThreadInternal mActivityThread;
private final ResourcesManager mResourcesManager = ResourcesManager.getInstance();
@GuardedBy("mResourcesManager")
private @Nullable Configuration mPendingConfiguration;
private @Nullable Configuration mCompatConfiguration;
private @Nullable Configuration mConfiguration;
ConfigurationController(@NonNull ActivityThreadInternal activityThread) {
mActivityThread = activityThread;
}
/** Update the pending configuration. */
Configuration updatePendingConfiguration(@NonNull Configuration config) {
synchronized (mResourcesManager) {
if (mPendingConfiguration == null || mPendingConfiguration.isOtherSeqNewer(config)) {
mPendingConfiguration = config;
return mPendingConfiguration;
}
}
return null;
}
/** Get the pending configuration. */
Configuration getPendingConfiguration(boolean clearPending) {
Configuration outConfig = null;
synchronized (mResourcesManager) {
if (mPendingConfiguration != null) {
outConfig = mPendingConfiguration;
if (clearPending) {
mPendingConfiguration = null;
}
}
}
return outConfig;
}
/** Set the compatibility configuration. */
void setCompatConfiguration(@NonNull Configuration config) {
mCompatConfiguration = new Configuration(config);
}
/** Get the compatibility configuration. */
Configuration getCompatConfiguration() {
return mCompatConfiguration;
}
/** Apply the global compatibility configuration. */
final Configuration applyCompatConfiguration() {
Configuration config = mConfiguration;
final int displayDensity = config.densityDpi;
if (mCompatConfiguration == null) {
mCompatConfiguration = new Configuration();
}
mCompatConfiguration.setTo(mConfiguration);
if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
config = mCompatConfiguration;
}
return config;
}
/** Set the configuration. */
void setConfiguration(@NonNull Configuration config) {
mConfiguration = new Configuration(config);
}
/** Get current configuration. */
Configuration getConfiguration() {
return mConfiguration;
}
/**
* Update the configuration to latest.
* @param config The new configuration.
*/
void handleConfigurationChanged(@NonNull Configuration config) {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
handleConfigurationChanged(config, null /* compat */);
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
}
/**
* Update the configuration to latest.
* @param compat The new compatibility information.
*/
void handleConfigurationChanged(@NonNull CompatibilityInfo compat) {
handleConfigurationChanged(mConfiguration, compat);
WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
}
/**
* Update the configuration to latest.
* @param config The new configuration.
* @param compat The new compatibility information.
*/
void handleConfigurationChanged(@Nullable Configuration config,
@Nullable CompatibilityInfo compat) {
int configDiff;
boolean equivalent;
// Get theme outside of synchronization to avoid nested lock.
final Resources.Theme systemTheme = mActivityThread.getSystemContext().getTheme();
final ContextImpl systemUiContext = mActivityThread.getSystemUiContextNoCreate();
final Resources.Theme systemUiTheme =
systemUiContext != null ? systemUiContext.getTheme() : null;
synchronized (mResourcesManager) {
if (mPendingConfiguration != null) {
if (!mPendingConfiguration.isOtherSeqNewer(config)) {
config = mPendingConfiguration;
updateDefaultDensity(config.densityDpi);
}
mPendingConfiguration = null;
}
if (config == null) {
return;
}
// This flag tracks whether the new configuration is fundamentally equivalent to the
// existing configuration. This is necessary to determine whether non-activity callbacks
// should receive notice when the only changes are related to non-public fields.
// We do not gate calling {@link #performActivityConfigurationChanged} based on this
// flag as that method uses the same check on the activity config override as well.
equivalent = mConfiguration != null && (0 == mConfiguration.diffPublicOnly(config));
if (DEBUG_CONFIGURATION) {
Slog.v(TAG, "Handle configuration changed: " + config);
}
final Application app = mActivityThread.getApplication();
final Resources appResources = app.getResources();
mResourcesManager.applyConfigurationToResources(config, compat);
updateLocaleListFromAppContext(app.getApplicationContext());
if (mConfiguration == null) {
mConfiguration = new Configuration();
}
if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
return;
}
configDiff = mConfiguration.updateFrom(config);
config = applyCompatConfiguration();
HardwareRenderer.sendDeviceConfigurationForDebugging(config);
if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
systemTheme.rebase();
}
if (systemUiTheme != null
&& (systemUiTheme.getChangingConfigurations() & configDiff) != 0) {
systemUiTheme.rebase();
}
}
final ArrayList<ComponentCallbacks2> callbacks =
mActivityThread.collectComponentCallbacks(false /* includeUiContexts */);
freeTextLayoutCachesIfNeeded(configDiff);
if (callbacks != null) {
final int size = callbacks.size();
for (int i = 0; i < size; i++) {
ComponentCallbacks2 cb = callbacks.get(i);
if (!equivalent) {
performConfigurationChanged(cb, config);
}
}
}
}
/**
* Decides whether to update a component's configuration and whether to inform it.
* @param cb The component callback to notify of configuration change.
* @param newConfig The new configuration.
*/
void performConfigurationChanged(@NonNull ComponentCallbacks2 cb,
@NonNull Configuration newConfig) {
// ContextThemeWrappers may override the configuration for that context. We must check and
// apply any overrides defined.
Configuration contextThemeWrapperOverrideConfig = null;
if (cb instanceof ContextThemeWrapper) {
final ContextThemeWrapper contextThemeWrapper = (ContextThemeWrapper) cb;
contextThemeWrapperOverrideConfig = contextThemeWrapper.getOverrideConfiguration();
}
// Apply the ContextThemeWrapper override if necessary.
// NOTE: Make sure the configurations are not modified, as they are treated as immutable
// in many places.
final Configuration configToReport = createNewConfigAndUpdateIfNotNull(
newConfig, contextThemeWrapperOverrideConfig);
cb.onConfigurationChanged(configToReport);
}
/** Update default density. */
void updateDefaultDensity(int densityDpi) {
if (!mActivityThread.isInDensityCompatMode()
&& densityDpi != Configuration.DENSITY_DPI_UNDEFINED
&& densityDpi != DisplayMetrics.DENSITY_DEVICE) {
DisplayMetrics.DENSITY_DEVICE = densityDpi;
Bitmap.setDefaultDensity(densityDpi);
}
}
/** Get current default display dpi. This is only done to maintain @UnsupportedAppUsage. */
int getCurDefaultDisplayDpi() {
return mConfiguration.densityDpi;
}
/**
* The LocaleList set for the app's resources may have been shuffled so that the preferred
* Locale is at position 0. We must find the index of this preferred Locale in the
* original LocaleList.
*/
void updateLocaleListFromAppContext(@NonNull Context context) {
final Locale bestLocale = context.getResources().getConfiguration().getLocales().get(0);
final LocaleList newLocaleList = mResourcesManager.getConfiguration().getLocales();
final int newLocaleListSize = newLocaleList.size();
for (int i = 0; i < newLocaleListSize; i++) {
if (bestLocale.equals(newLocaleList.get(i))) {
LocaleList.setDefault(newLocaleList, i);
return;
}
}
// The app may have overridden the LocaleList with its own Locale
// (not present in the available list). Push the chosen Locale
// to the front of the list.
LocaleList.setDefault(new LocaleList(bestLocale, newLocaleList));
}
/**
* Creates a new Configuration only if override would modify base. Otherwise returns base.
* @param base The base configuration.
* @param override The update to apply to the base configuration. Can be null.
* @return A Configuration representing base with override applied.
*/
static Configuration createNewConfigAndUpdateIfNotNull(@NonNull Configuration base,
@Nullable Configuration override) {
if (override == null) {
return base;
}
Configuration newConfig = new Configuration(base);
newConfig.updateFrom(override);
return newConfig;
}
}
|