/
HarmonizedColors.java
212 lines (193 loc) · 8.84 KB
/
HarmonizedColors.java
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (C) 2022 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 com.google.android.material.color;
import com.google.android.material.R;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.content.res.loader.ResourcesLoader;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import androidx.annotation.ChecksSdkIntAtLeast;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import java.util.HashMap;
import java.util.Map;
/**
* A class for harmonizing color resources and attributes.
*
* <p>This class is used for harmonizing color resources/attributes defined in xml at runtime. The
* values of harmonized resources/attributes will be overridden, and afterwards if you retrieve the
* value from the associated context, or inflate resources like layouts that are using those
* harmonized resources/attributes, the overridden values will be used instead.
*
* <p>If you need to harmonize color resources at runtime, see:
* {@link #applyToContextIfAvailable(Context, HarmonizedColorsOptions)}, and
* {@link #wrapContextIfAvailable(Context, HarmonizedColorsOptions)}
*/
public class HarmonizedColors {
private HarmonizedColors() {}
private static final String TAG = HarmonizedColors.class.getSimpleName();
/**
* Harmonizes the specified color resources, attributes, and theme overlay in the {@link
* Context} provided.
*
* <p>If harmonization is not available, provided color resources and attributes in {@link
* HarmonizedColorsOptions} will not be harmonized.
*
* @param context The target context.
* @param options The {@link HarmonizedColorsOptions} object that specifies the resource ids,
* color attributes to be harmonized and the color attribute to harmonize with.
*/
@NonNull
public static void applyToContextIfAvailable(
@NonNull Context context, @NonNull HarmonizedColorsOptions options) {
if (!isHarmonizedColorAvailable()) {
return;
}
Map<Integer, Integer> colorReplacementMap =
createHarmonizedColorReplacementMap(context, options);
int themeOverlay = options.getThemeOverlayResourceId(/* defaultThemeOverlay= */ 0);
if (addResourcesLoaderToContext(context, colorReplacementMap) && themeOverlay != 0) {
ThemeUtils.applyThemeOverlay(context, themeOverlay);
}
}
/**
* Wraps the given Context from HarmonizedColorsOptions with the color resources being harmonized.
*
* <p>If harmonization is not available, provided color resources and attributes in {@link
* HarmonizedColorsOptions} will not be harmonized.
*
* @param context The target context.
* @param options The {@link HarmonizedColorsOptions} object that specifies the resource ids,
* color attributes to be harmonized and the color attribute to harmonize with.
* @return the new context with resources being harmonized. If resources are not harmonized
* successfully, the context passed in will be returned.
*/
@NonNull
public static Context wrapContextIfAvailable(
@NonNull Context context, @NonNull HarmonizedColorsOptions options) {
if (!isHarmonizedColorAvailable()) {
return context;
}
// Retrieve colors from original context passed in before the resources are overridden below.
Map<Integer, Integer> colorReplacementMap =
createHarmonizedColorReplacementMap(context, options);
// Empty themeOverlay is used as default to prevent ContextThemeWrapper uses the default theme
// of the application to wrap Context.
int themeOverlay =
options.getThemeOverlayResourceId(R.style.ThemeOverlay_Material3_HarmonizedColors_Empty);
ContextThemeWrapper themeWrapper = new ContextThemeWrapper(context, themeOverlay);
// Because ContextThemeWrapper does not provide a new set of resources, override config to
// retrieve the new set of resources and to keep the original context's resources intact.
themeWrapper.applyOverrideConfiguration(new Configuration());
return addResourcesLoaderToContext(themeWrapper, colorReplacementMap) ? themeWrapper : context;
}
/**
* <p>If harmonization is not available, color will not be harmonized.
*
* @see #applyToContextIfAvailable(Context, HarmonizedColorsOptions)
* @see #wrapContextIfAvailable(Context, HarmonizedColorsOptions)
* @return {@code true} if harmonized colors are available on the current SDK level, otherwise
* {@code false} will be returned.
*/
@ChecksSdkIntAtLeast(api = VERSION_CODES.R)
public static boolean isHarmonizedColorAvailable() {
return VERSION.SDK_INT >= VERSION_CODES.R;
}
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
private static Map<Integer, Integer> createHarmonizedColorReplacementMap(
Context originalContext, HarmonizedColorsOptions options) {
Map<Integer, Integer> colorReplacementMap = new HashMap<>();
int colorToHarmonizeWith =
MaterialColors.getColor(originalContext, options.getColorAttributeToHarmonizeWith(), TAG);
// Harmonize color resources.
for (int colorResourceId : options.getColorResourceIds()) {
int harmonizedColor =
MaterialColors.harmonize(
ContextCompat.getColor(originalContext, colorResourceId), colorToHarmonizeWith);
colorReplacementMap.put(colorResourceId, harmonizedColor);
}
HarmonizedColorAttributes colorAttributes = options.getColorAttributes();
if (colorAttributes != null) {
int[] attributes = colorAttributes.getAttributes();
if (attributes.length > 0) {
// Harmonize theme overlay attributes in the custom theme overlay. If custom theme overlay
// is not provided, look up resources value the theme attributes point to and
// harmonize directly.
int themeOverlay = colorAttributes.getThemeOverlay();
TypedArray themeAttributesTypedArray = originalContext.obtainStyledAttributes(attributes);
TypedArray themeOverlayAttributesTypedArray =
themeOverlay != 0
? new ContextThemeWrapper(originalContext, themeOverlay)
.obtainStyledAttributes(attributes)
: null;
addHarmonizedColorAttributesToReplacementMap(
colorReplacementMap,
themeAttributesTypedArray,
themeOverlayAttributesTypedArray,
colorToHarmonizeWith);
themeAttributesTypedArray.recycle();
if (themeOverlayAttributesTypedArray != null) {
themeOverlayAttributesTypedArray.recycle();
}
}
}
return colorReplacementMap;
}
@RequiresApi(api = VERSION_CODES.R)
private static boolean addResourcesLoaderToContext(
Context context, Map<Integer, Integer> colorReplacementMap) {
ResourcesLoader resourcesLoader =
ColorResourcesLoaderCreator.create(context, colorReplacementMap);
if (resourcesLoader != null) {
context.getResources().addLoaders(resourcesLoader);
return true;
}
return false;
}
// TypedArray.getType() requires API >= 21.
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
private static void addHarmonizedColorAttributesToReplacementMap(
@NonNull Map<Integer, Integer> colorReplacementMap,
@NonNull TypedArray themeAttributesTypedArray,
@Nullable TypedArray themeOverlayAttributesTypedArray,
@ColorInt int colorToHarmonizeWith) {
TypedArray resourceIdTypedArray =
themeOverlayAttributesTypedArray != null
? themeOverlayAttributesTypedArray
: themeAttributesTypedArray;
for (int i = 0; i < themeAttributesTypedArray.getIndexCount(); i++) {
int resourceId = resourceIdTypedArray.getResourceId(i, 0);
if (resourceId != 0
&& themeAttributesTypedArray.hasValue(i)
&& isColorResource(themeAttributesTypedArray.getType(i))) {
int colorToHarmonize = themeAttributesTypedArray.getColor(i, 0);
colorReplacementMap.put(
resourceId, MaterialColors.harmonize(colorToHarmonize, colorToHarmonizeWith));
}
}
}
private static boolean isColorResource(int attrType) {
return (TypedValue.TYPE_FIRST_COLOR_INT <= attrType)
&& (attrType <= TypedValue.TYPE_LAST_COLOR_INT);
}
}