summaryrefslogtreecommitdiff
path: root/core/java/android/window/ConfigurationHelper.java
blob: 843712f0434a5be4b77e6d568a0139cde98bc876 (plain)
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
/*
 * 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.window;

import static android.view.Display.INVALID_DISPLAY;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ResourcesManager;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.IBinder;
import android.view.Display;
import android.view.WindowManager;

/**
 * A helper class to maintain {@link android.content.res.Configuration} related methods used both
 * in {@link android.app.Activity} and {@link WindowContext}.
 *
 * @hide
 */
public class ConfigurationHelper {
    private ConfigurationHelper() {}

    /** Ask text layout engine to free its caches if there is a locale change. */
    public static void freeTextLayoutCachesIfNeeded(int configDiff) {
        if ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0) {
            Canvas.freeTextLayoutCaches();
        }
    }

    /**
     * Returns {@code true} if the {@link android.content.res.Resources} associated with
     * a {@code token} needs to be updated.
     *
     * @param token A {@link Context#getActivityToken() activity token} or
     * {@link Context#getWindowContextToken() window context token}
     * @param config The original {@link Configuration}
     * @param newConfig The updated Configuration
     * @param displayChanged a flag to indicate there's a display change
     * @param configChanged a flag to indicate there's a Configuration change.
     *
     * @see ResourcesManager#updateResourcesForActivity(IBinder, Configuration, int)
     */
    public static boolean shouldUpdateResources(IBinder token, @Nullable Configuration config,
            @NonNull Configuration newConfig, @NonNull Configuration overrideConfig,
            boolean displayChanged, @Nullable Boolean configChanged) {
        // The configuration has not yet been initialized. We should update it.
        if (config == null) {
            return true;
        }
        // If the token associated context is moved to another display, we should update the
        // ResourcesKey.
        if (displayChanged) {
            return true;
        }
        // If the new config is the same as the config this Activity is already running with and
        // the override config also didn't change, then don't update the Resources
        if (!ResourcesManager.getInstance().isSameResourcesOverrideConfig(token, overrideConfig)) {
            return true;
        }
        // If there's a update on WindowConfiguration#mBounds or maxBounds, we should update the
        // Resources to make WindowMetrics API report the updated result.
        if (shouldUpdateWindowMetricsBounds(config, newConfig)) {
            return true;
        }
        return configChanged == null ? config.diff(newConfig) != 0 : configChanged;
    }

    /**
     * Returns {@code true} if {@code displayId} is different from {@code newDisplayId}.
     * Note that {@link Display#INVALID_DISPLAY} means no difference.
     */
    public static boolean isDifferentDisplay(int displayId, int newDisplayId) {
        return newDisplayId != INVALID_DISPLAY && displayId != newDisplayId;
    }

    // TODO(b/173090263): Remove this method after the improvement of AssetManager and ResourcesImpl
    // constructions.
    /**
     * Returns {@code true} if the metrics reported by {@link android.view.WindowMetrics} APIs
     * should be updated.
     *
     * @see WindowManager#getCurrentWindowMetrics()
     * @see WindowManager#getMaximumWindowMetrics()
     */
    private static boolean shouldUpdateWindowMetricsBounds(@NonNull Configuration currentConfig,
            @NonNull Configuration newConfig) {
        final Rect currentBounds = currentConfig.windowConfiguration.getBounds();
        final Rect newBounds = newConfig.windowConfiguration.getBounds();

        final Rect currentMaxBounds = currentConfig.windowConfiguration.getMaxBounds();
        final Rect newMaxBounds = newConfig.windowConfiguration.getMaxBounds();

        return !currentBounds.equals(newBounds) || !currentMaxBounds.equals(newMaxBounds);
    }
}