blob: 67112577b977b76a6f2853793800a99de1909df5 [file] [log] [blame]
Andrii Kulian1779e612016-10-12 21:58:25 -07001/*
Wale Ogunwale98d62312017-07-12 09:24:56 -07002 * Copyright (C) 2017 The Android Open Source Project
Andrii Kulian1779e612016-10-12 21:58:25 -07003 *
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
Wale Ogunwale98d62312017-07-12 09:24:56 -070017package com.android.server.wm;
Andrii Kulian1779e612016-10-12 21:58:25 -070018
Wale Ogunwale687b4272017-07-27 02:56:23 -070019import android.app.WindowConfiguration;
Andrii Kulian1779e612016-10-12 21:58:25 -070020import android.content.res.Configuration;
21
22/**
23 * Contains common logic for classes that have override configurations and are organized in a
24 * hierarchy.
25 */
Wale Ogunwale98d62312017-07-12 09:24:56 -070026public abstract class ConfigurationContainer<E extends ConfigurationContainer> {
Andrii Kulian1779e612016-10-12 21:58:25 -070027
28 /** Contains override configuration settings applied to this configuration container. */
29 private Configuration mOverrideConfiguration = new Configuration();
30
31 /**
32 * Contains full configuration applied to this configuration container. Corresponds to full
33 * parent's config with applied {@link #mOverrideConfiguration}.
34 */
35 private Configuration mFullConfiguration = new Configuration();
36
37 /**
38 * Contains merged override configuration settings from the top of the hierarchy down to this
39 * particular instance. It is different from {@link #mFullConfiguration} because it starts from
40 * topmost container's override config instead of global config.
41 */
42 private Configuration mMergedOverrideConfiguration = new Configuration();
43
44 /**
45 * Returns full configuration applied to this configuration container.
46 * This method should be used for getting settings applied in each particular level of the
47 * hierarchy.
48 */
Wale Ogunwale98d62312017-07-12 09:24:56 -070049 public Configuration getConfiguration() {
Andrii Kulian1779e612016-10-12 21:58:25 -070050 return mFullConfiguration;
51 }
52
53 /**
54 * Notify that parent config changed and we need to update full configuration.
55 * @see #mFullConfiguration
56 */
Wale Ogunwale98d62312017-07-12 09:24:56 -070057 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian1779e612016-10-12 21:58:25 -070058 mFullConfiguration.setTo(newParentConfig);
59 mFullConfiguration.updateFrom(mOverrideConfiguration);
60 for (int i = getChildCount() - 1; i >= 0; --i) {
61 final ConfigurationContainer child = getChildAt(i);
62 child.onConfigurationChanged(mFullConfiguration);
63 }
64 }
65
66 /** Returns override configuration applied to this configuration container. */
Wale Ogunwale98d62312017-07-12 09:24:56 -070067 public Configuration getOverrideConfiguration() {
Andrii Kulian1779e612016-10-12 21:58:25 -070068 return mOverrideConfiguration;
69 }
70
71 /**
72 * Update override configuration and recalculate full config.
73 * @see #mOverrideConfiguration
74 * @see #mFullConfiguration
75 */
Wale Ogunwale98d62312017-07-12 09:24:56 -070076 public void onOverrideConfigurationChanged(Configuration overrideConfiguration) {
Andrii Kulian1779e612016-10-12 21:58:25 -070077 mOverrideConfiguration.setTo(overrideConfiguration);
78 // Update full configuration of this container and all its children.
79 final ConfigurationContainer parent = getParent();
80 onConfigurationChanged(parent != null ? parent.getConfiguration() : Configuration.EMPTY);
81 // Update merged override config of this container and all its children.
82 onMergedOverrideConfigurationChanged();
83 }
84
85 /**
86 * Get merged override configuration from the top of the hierarchy down to this particular
87 * instance. This should be reported to client as override config.
88 */
Wale Ogunwale98d62312017-07-12 09:24:56 -070089 public Configuration getMergedOverrideConfiguration() {
Andrii Kulian1779e612016-10-12 21:58:25 -070090 return mMergedOverrideConfiguration;
91 }
92
93 /**
94 * Update merged override configuration based on corresponding parent's config and notify all
95 * its children. If there is no parent, merged override configuration will set equal to current
96 * override config.
97 * @see #mMergedOverrideConfiguration
98 */
Wale Ogunwale98d62312017-07-12 09:24:56 -070099 void onMergedOverrideConfigurationChanged() {
Andrii Kulian1779e612016-10-12 21:58:25 -0700100 final ConfigurationContainer parent = getParent();
101 if (parent != null) {
102 mMergedOverrideConfiguration.setTo(parent.getMergedOverrideConfiguration());
103 mMergedOverrideConfiguration.updateFrom(mOverrideConfiguration);
104 } else {
105 mMergedOverrideConfiguration.setTo(mOverrideConfiguration);
106 }
107 for (int i = getChildCount() - 1; i >= 0; --i) {
108 final ConfigurationContainer child = getChildAt(i);
109 child.onMergedOverrideConfigurationChanged();
110 }
111 }
112
Wale Ogunwale687b4272017-07-27 02:56:23 -0700113 /** Sets the windowing mode for the configuration container. */
Wale Ogunwale5613fdb2017-08-22 11:48:18 -0700114 void setWindowingMode(/*@WindowConfiguration.WindowingMode TODO: causes build error...why?*/
115 int windowingMode) {
Wale Ogunwale687b4272017-07-27 02:56:23 -0700116 mOverrideConfiguration.windowConfiguration.setWindowingMode(windowingMode);
117 onOverrideConfigurationChanged(mOverrideConfiguration);
118 }
119
Andrii Kulian1779e612016-10-12 21:58:25 -0700120 /**
121 * Must be called when new parent for the container was set.
122 */
Wale Ogunwale98d62312017-07-12 09:24:56 -0700123 protected void onParentChanged() {
Andrii Kulian1779e612016-10-12 21:58:25 -0700124 final ConfigurationContainer parent = getParent();
Andrii Kulianb94292e2016-10-19 13:30:58 -0700125 // Removing parent usually means that we've detached this entity to destroy it or to attach
126 // to another parent. In both cases we don't need to update the configuration now.
127 if (parent != null) {
128 // Update full configuration of this container and all its children.
129 onConfigurationChanged(parent.mFullConfiguration);
130 // Update merged override configuration of this container and all its children.
131 onMergedOverrideConfigurationChanged();
132 }
Andrii Kulian1779e612016-10-12 21:58:25 -0700133 }
134
135 abstract protected int getChildCount();
136
137 abstract protected E getChildAt(int index);
138
139 abstract protected ConfigurationContainer getParent();
140}