• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.util;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.res.Configuration;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.io.PrintWriter;
26 
27 /**
28  * Container that holds global and override config and their merge product.
29  * Merged configuration updates automatically whenever global or override configs are updated via
30  * setters.
31  *
32  * {@hide}
33  */
34 public class MergedConfiguration implements Parcelable {
35 
36     private final Configuration mGlobalConfig = new Configuration();
37     private final Configuration mOverrideConfig = new Configuration();
38     private final Configuration mMergedConfig = new Configuration();
39 
MergedConfiguration()40     public MergedConfiguration() {
41     }
42 
MergedConfiguration(Configuration globalConfig, Configuration overrideConfig)43     public MergedConfiguration(Configuration globalConfig, Configuration overrideConfig) {
44         setConfiguration(globalConfig, overrideConfig);
45     }
46 
MergedConfiguration(Configuration globalConfig)47     public MergedConfiguration(Configuration globalConfig) {
48         setGlobalConfiguration(globalConfig);
49     }
50 
MergedConfiguration(MergedConfiguration mergedConfiguration)51     public MergedConfiguration(MergedConfiguration mergedConfiguration) {
52         setConfiguration(mergedConfiguration.getGlobalConfiguration(),
53                 mergedConfiguration.getOverrideConfiguration());
54     }
55 
MergedConfiguration(Parcel in)56     private MergedConfiguration(Parcel in) {
57         readFromParcel(in);
58     }
59 
60     @Override
writeToParcel(Parcel dest, int flags)61     public void writeToParcel(Parcel dest, int flags) {
62         mGlobalConfig.writeToParcel(dest, flags);
63         mOverrideConfig.writeToParcel(dest, flags);
64         mMergedConfig.writeToParcel(dest, flags);
65     }
66 
readFromParcel(Parcel source)67     public void readFromParcel(Parcel source) {
68         mGlobalConfig.readFromParcel(source);
69         mOverrideConfig.readFromParcel(source);
70         mMergedConfig.readFromParcel(source);
71     }
72 
73     @Override
describeContents()74     public int describeContents() {
75         return 0;
76     }
77 
78     public static final @android.annotation.NonNull Creator<MergedConfiguration> CREATOR = new Creator<MergedConfiguration>() {
79         @Override
80         public MergedConfiguration createFromParcel(Parcel in) {
81             return new MergedConfiguration(in);
82         }
83 
84         @Override
85         public MergedConfiguration[] newArray(int size) {
86             return new MergedConfiguration[size];
87         }
88     };
89 
90     /**
91      * Update global and override configurations.
92      * Merged configuration will automatically be updated.
93      * @param globalConfig New global configuration.
94      * @param overrideConfig New override configuration.
95      */
setConfiguration(Configuration globalConfig, Configuration overrideConfig)96     public void setConfiguration(Configuration globalConfig, Configuration overrideConfig) {
97         mGlobalConfig.setTo(globalConfig);
98         mOverrideConfig.setTo(overrideConfig);
99         updateMergedConfig();
100     }
101 
102     /**
103      * Update global configurations.
104      * Merged configuration will automatically be updated.
105      * @param globalConfig New global configuration.
106      */
setGlobalConfiguration(Configuration globalConfig)107     public void setGlobalConfiguration(Configuration globalConfig) {
108         mGlobalConfig.setTo(globalConfig);
109         updateMergedConfig();
110     }
111 
112     /**
113      * Update override configurations.
114      * Merged configuration will automatically be updated.
115      * @param overrideConfig New override configuration.
116      */
setOverrideConfiguration(Configuration overrideConfig)117     public void setOverrideConfiguration(Configuration overrideConfig) {
118         mOverrideConfig.setTo(overrideConfig);
119         updateMergedConfig();
120     }
121 
setTo(MergedConfiguration config)122     public void setTo(MergedConfiguration config) {
123         setConfiguration(config.mGlobalConfig, config.mOverrideConfig);
124     }
125 
unset()126     public void unset() {
127         mGlobalConfig.unset();
128         mOverrideConfig.unset();
129         updateMergedConfig();
130     }
131 
132     /**
133      * @return Stored global configuration value.
134      */
135     @NonNull
getGlobalConfiguration()136     public Configuration getGlobalConfiguration() {
137         return mGlobalConfig;
138     }
139 
140     /**
141      * @return Stored override configuration value.
142      */
getOverrideConfiguration()143     public Configuration getOverrideConfiguration() {
144         return mOverrideConfig;
145     }
146 
147     /**
148      * @return Stored merged configuration value.
149      */
getMergedConfiguration()150     public Configuration getMergedConfiguration() {
151         return mMergedConfig;
152     }
153 
154     /** Update merged config when global or override config changes. */
updateMergedConfig()155     private void updateMergedConfig() {
156         mMergedConfig.setTo(mGlobalConfig);
157         mMergedConfig.updateFrom(mOverrideConfig);
158     }
159 
160     @Override
toString()161     public String toString() {
162         return "{mGlobalConfig=" + mGlobalConfig + " mOverrideConfig=" + mOverrideConfig + "}";
163     }
164 
165     @Override
hashCode()166     public int hashCode() {
167         return mMergedConfig.hashCode();
168     }
169 
170     @Override
equals(@ullable Object that)171     public boolean equals(@Nullable Object that) {
172         if (!(that instanceof MergedConfiguration)) {
173             return false;
174         }
175 
176         if (that == this) return true;
177         return mMergedConfig.equals(((MergedConfiguration) that).mMergedConfig);
178     }
179 
dump(PrintWriter pw, String prefix)180     public void dump(PrintWriter pw, String prefix) {
181         pw.println(prefix + "mGlobalConfig=" + mGlobalConfig);
182         pw.println(prefix + "mOverrideConfig=" + mOverrideConfig);
183     }
184 }
185