1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.window;
18 
19 import static android.view.Display.INVALID_DISPLAY;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.app.ResourcesManager;
24 import android.app.WindowConfiguration;
25 import android.content.Context;
26 import android.content.pm.ActivityInfo;
27 import android.content.res.Configuration;
28 import android.graphics.Canvas;
29 import android.graphics.Rect;
30 import android.os.IBinder;
31 import android.view.Display;
32 import android.view.WindowManager;
33 
34 /**
35  * A helper class to maintain {@link android.content.res.Configuration} related methods used both
36  * in {@link android.app.Activity} and {@link WindowContext}.
37  *
38  * @hide
39  */
40 public class ConfigurationHelper {
ConfigurationHelper()41     private ConfigurationHelper() {}
42 
43     /** Ask text layout engine to free its caches if there is a locale change. */
freeTextLayoutCachesIfNeeded(int configDiff)44     public static void freeTextLayoutCachesIfNeeded(int configDiff) {
45         if ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0) {
46             Canvas.freeTextLayoutCaches();
47         }
48     }
49 
50     /**
51      * Returns {@code true} if the {@link android.content.res.Resources} associated with
52      * a {@code token} needs to be updated.
53      *
54      * @param token A {@link Context#getActivityToken() activity token} or
55      * {@link Context#getWindowContextToken() window context token}
56      * @param config The original {@link Configuration}
57      * @param newConfig The updated Configuration
58      * @param displayChanged a flag to indicate there's a display change
59      * @param configChanged a flag to indicate there's a Configuration change.
60      *
61      * @see ResourcesManager#updateResourcesForActivity(IBinder, Configuration, int)
62      */
shouldUpdateResources(IBinder token, @Nullable Configuration config, @NonNull Configuration newConfig, @NonNull Configuration overrideConfig, boolean displayChanged, @Nullable Boolean configChanged)63     public static boolean shouldUpdateResources(IBinder token, @Nullable Configuration config,
64             @NonNull Configuration newConfig, @NonNull Configuration overrideConfig,
65             boolean displayChanged, @Nullable Boolean configChanged) {
66         // The configuration has not yet been initialized. We should update it.
67         if (config == null) {
68             return true;
69         }
70         // If the token associated context is moved to another display, we should update the
71         // ResourcesKey.
72         if (displayChanged) {
73             return true;
74         }
75         // If the new config is the same as the config this Activity is already running with and
76         // the override config also didn't change, then don't update the Resources
77         if (!ResourcesManager.getInstance().isSameResourcesOverrideConfig(token, overrideConfig)) {
78             return true;
79         }
80         // If there's a update on WindowConfiguration#mBounds or maxBounds, we should update the
81         // Resources to make WindowMetrics API report the updated result.
82         if (shouldUpdateWindowMetricsBounds(config, newConfig)) {
83             return true;
84         }
85         // If the display rotation has changed, we also need to update resources.
86         if (isDisplayRotationChanged(config, newConfig)) {
87             return true;
88         }
89         return configChanged == null ? config.diff(newConfig) != 0 : configChanged;
90     }
91 
92     /**
93      * Returns {@code true} if {@code displayId} is different from {@code newDisplayId}.
94      * Note that {@link Display#INVALID_DISPLAY} means no difference.
95      */
isDifferentDisplay(int displayId, int newDisplayId)96     public static boolean isDifferentDisplay(int displayId, int newDisplayId) {
97         return newDisplayId != INVALID_DISPLAY && displayId != newDisplayId;
98     }
99 
100     // TODO(b/173090263): Remove this method after the improvement of AssetManager and ResourcesImpl
101     // constructions.
102     /**
103      * Returns {@code true} if the metrics reported by {@link android.view.WindowMetrics} APIs
104      * should be updated.
105      *
106      * @see WindowManager#getCurrentWindowMetrics()
107      * @see WindowManager#getMaximumWindowMetrics()
108      */
shouldUpdateWindowMetricsBounds(@onNull Configuration currentConfig, @NonNull Configuration newConfig)109     private static boolean shouldUpdateWindowMetricsBounds(@NonNull Configuration currentConfig,
110             @NonNull Configuration newConfig) {
111         final Rect currentBounds = currentConfig.windowConfiguration.getBounds();
112         final Rect newBounds = newConfig.windowConfiguration.getBounds();
113 
114         final Rect currentMaxBounds = currentConfig.windowConfiguration.getMaxBounds();
115         final Rect newMaxBounds = newConfig.windowConfiguration.getMaxBounds();
116 
117         return !currentBounds.equals(newBounds) || !currentMaxBounds.equals(newMaxBounds);
118     }
119 
isDisplayRotationChanged(@onNull Configuration config, @NonNull Configuration newConfig)120     private static boolean isDisplayRotationChanged(@NonNull Configuration config,
121             @NonNull Configuration newConfig) {
122         final int origRot = config.windowConfiguration.getDisplayRotation();
123         final int newRot = newConfig.windowConfiguration.getDisplayRotation();
124         if (newRot == WindowConfiguration.ROTATION_UNDEFINED
125                 || origRot == WindowConfiguration.ROTATION_UNDEFINED) {
126             return false;
127         }
128         return origRot != newRot;
129     }
130 }
131