Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 1 | /* |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 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 | |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 18 | |
Wale Ogunwale | 687b427 | 2017-07-27 02:56:23 -0700 | [diff] [blame] | 19 | import android.app.WindowConfiguration; |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 20 | import 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 Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 26 | public abstract class ConfigurationContainer<E extends ConfigurationContainer> { |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 27 | |
| 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 Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 49 | public Configuration getConfiguration() { |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 50 | return mFullConfiguration; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Notify that parent config changed and we need to update full configuration. |
| 55 | * @see #mFullConfiguration |
| 56 | */ |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 57 | public void onConfigurationChanged(Configuration newParentConfig) { |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 58 | 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 Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 67 | public Configuration getOverrideConfiguration() { |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 68 | return mOverrideConfiguration; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Update override configuration and recalculate full config. |
| 73 | * @see #mOverrideConfiguration |
| 74 | * @see #mFullConfiguration |
| 75 | */ |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 76 | public void onOverrideConfigurationChanged(Configuration overrideConfiguration) { |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 77 | 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 Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 89 | public Configuration getMergedOverrideConfiguration() { |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 90 | 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 Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 99 | void onMergedOverrideConfigurationChanged() { |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 100 | 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 Ogunwale | 687b427 | 2017-07-27 02:56:23 -0700 | [diff] [blame] | 113 | /** Sets the windowing mode for the configuration container. */ |
Wale Ogunwale | 5613fdb | 2017-08-22 11:48:18 -0700 | [diff] [blame] | 114 | void setWindowingMode(/*@WindowConfiguration.WindowingMode TODO: causes build error...why?*/ |
| 115 | int windowingMode) { |
Wale Ogunwale | 687b427 | 2017-07-27 02:56:23 -0700 | [diff] [blame] | 116 | mOverrideConfiguration.windowConfiguration.setWindowingMode(windowingMode); |
| 117 | onOverrideConfigurationChanged(mOverrideConfiguration); |
| 118 | } |
| 119 | |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Must be called when new parent for the container was set. |
| 122 | */ |
Wale Ogunwale | 98d6231 | 2017-07-12 09:24:56 -0700 | [diff] [blame] | 123 | protected void onParentChanged() { |
Andrii Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 124 | final ConfigurationContainer parent = getParent(); |
Andrii Kulian | b94292e | 2016-10-19 13:30:58 -0700 | [diff] [blame] | 125 | // 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 Kulian | 1779e61 | 2016-10-12 21:58:25 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | abstract protected int getChildCount(); |
| 136 | |
| 137 | abstract protected E getChildAt(int index); |
| 138 | |
| 139 | abstract protected ConfigurationContainer getParent(); |
| 140 | } |