• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 package com.android.customization.model.color;
17 
18 import android.content.Context;
19 import android.content.res.Configuration;
20 import android.content.res.Resources;
21 import android.graphics.PorterDuff.Mode;
22 import android.view.View;
23 import android.widget.ImageView;
24 
25 import androidx.annotation.ColorInt;
26 import androidx.annotation.VisibleForTesting;
27 
28 import com.android.customization.model.color.ColorOptionsProvider.ColorSource;
29 import com.android.systemui.monet.Style;
30 import com.android.wallpaper.R;
31 
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Map;
35 
36 /**
37  * Represents a seed color obtained from WallpaperColors, for the user to chose as their theming
38  * option.
39  */
40 public class ColorSeedOption extends ColorOption {
41 
42     private final PreviewInfo mPreviewInfo;
43     @ColorSource
44     private final String mSource;
45 
46     @VisibleForTesting
ColorSeedOption(String title, Map<String, String> overlayPackages, boolean isDefault, @ColorSource String source, Style style, int index, PreviewInfo previewInfo)47     ColorSeedOption(String title, Map<String, String> overlayPackages, boolean isDefault,
48             @ColorSource String source, Style style, int index, PreviewInfo previewInfo) {
49         super(title, overlayPackages, isDefault, style, index);
50         mSource = source;
51         mPreviewInfo = previewInfo;
52     }
53 
54     @Override
getPreviewInfo()55     public PreviewInfo getPreviewInfo() {
56         return mPreviewInfo;
57     }
58 
59     @Override
getSource()60     public String getSource() {
61         return mSource;
62     }
63 
64     @Override
getLayoutResId()65     public int getLayoutResId() {
66         return R.layout.color_option;
67     }
68 
69     @Override
bindThumbnailTile(View view)70     public void bindThumbnailTile(View view) {
71         Resources res = view.getContext().getResources();
72         @ColorInt int[] colors = mPreviewInfo.resolveColors(res);
73 
74         for (int i = 0; i < mPreviewColorIds.length; i++) {
75             ImageView colorPreviewImageView = view.findViewById(mPreviewColorIds[i]);
76             colorPreviewImageView.getDrawable().setColorFilter(colors[i], Mode.SRC);
77         }
78 
79         view.setContentDescription(getContentDescription(view.getContext()));
80     }
81 
82     @Override
getContentDescription(Context context)83     public CharSequence getContentDescription(Context context) {
84         // Override because we want all options with the same description.
85         return context.getString(R.string.wallpaper_color_title);
86     }
87 
88     /**
89      * The preview information of {@link ColorOption}
90      */
91     public static class PreviewInfo implements ColorOption.PreviewInfo {
92         @ColorInt public int[] lightColors;
93         @ColorInt public int[] darkColors;
94 
PreviewInfo(@olorInt int[] lightColors, @ColorInt int[] darkColors)95         private PreviewInfo(@ColorInt int[] lightColors, @ColorInt int[] darkColors) {
96             this.lightColors = lightColors;
97             this.darkColors = darkColors;
98         }
99 
100         /**
101          * Returns the colors to be applied corresponding with the current
102          * configuration's UI mode.
103          * @param res resources to read to the UI mode configuration from
104          * @return one of {@link #lightColors} or {@link #darkColors}
105          */
106         @ColorInt
resolveColors(Resources res)107         public int[] resolveColors(Resources res) {
108             boolean night = (res.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK)
109                     == Configuration.UI_MODE_NIGHT_YES;
110             return night ? darkColors : lightColors;
111         }
112 
113         /**
114          * Returns the preview colors based on whether dark theme or light theme colors are
115          * requested.
116          * @param darkTheme if true, returns dark theme colors, otherwise returns light theme colors
117          * @return one of {@link #lightColors} or {@link #darkColors}
118          */
119         @ColorInt
resolveColors(boolean darkTheme)120         public int[] resolveColors(boolean darkTheme) {
121             return darkTheme ? darkColors : lightColors;
122         }
123     }
124 
125     /**
126      * The builder of ColorSeedOption
127      */
128     public static class Builder {
129         protected String mTitle;
130         @ColorInt
131         private int[] mLightColors;
132         @ColorInt
133         private int[] mDarkColors;
134         @ColorSource
135         private String mSource;
136         private boolean mIsDefault;
137         private Style mStyle = Style.TONAL_SPOT;
138         private int mIndex;
139         protected Map<String, String> mPackages = new HashMap<>();
140 
141         /**
142          * Builds the ColorSeedOption
143          * @return new {@link ColorOption} object
144          */
build()145         public ColorSeedOption build() {
146             return new ColorSeedOption(mTitle, mPackages, mIsDefault, mSource, mStyle, mIndex,
147                     createPreviewInfo());
148         }
149 
150         /**
151          * Creates preview information
152          * @return the {@link PreviewInfo} object
153          */
createPreviewInfo()154         public PreviewInfo createPreviewInfo() {
155             return new PreviewInfo(mLightColors, mDarkColors);
156         }
157 
getPackages()158         public Map<String, String> getPackages() {
159             return Collections.unmodifiableMap(mPackages);
160         }
161 
162         /**
163          * Gets title of {@link ColorOption} object
164          * @return title string
165          */
getTitle()166         public String getTitle() {
167             return mTitle;
168         }
169 
170         /**
171          * Sets title of bundle
172          * @param title specified title
173          * @return this of {@link ColorBundle.Builder}
174          */
setTitle(String title)175         public Builder setTitle(String title) {
176             mTitle = title;
177             return this;
178         }
179 
180         /**
181          * Sets the colors for preview in light mode
182          * @param lightColors  {@link ColorInt} colors for light mode
183          * @return this of {@link Builder}
184          */
setLightColors(@olorInt int[] lightColors)185         public Builder setLightColors(@ColorInt int[] lightColors) {
186             mLightColors = lightColors;
187             return this;
188         }
189 
190         /**
191          * Sets the colors for preview in light mode
192          * @param darkColors  {@link ColorInt} colors for light mode
193          * @return this of {@link Builder}
194          */
setDarkColors(@olorInt int[] darkColors)195         public Builder setDarkColors(@ColorInt int[] darkColors) {
196             mDarkColors = darkColors;
197             return this;
198         }
199 
200 
201         /**
202          * Sets overlay package for bundle
203          * @param category the category of bundle
204          * @param packageName tha name of package in the category
205          * @return this of {@link Builder}
206          */
addOverlayPackage(String category, String packageName)207         public Builder addOverlayPackage(String category, String packageName) {
208             mPackages.put(category, packageName);
209             return this;
210         }
211 
212         /**
213          * Sets the source of this color seed
214          * @param source typically either {@link ColorOptionsProvider#COLOR_SOURCE_HOME} or
215          *              {@link ColorOptionsProvider#COLOR_SOURCE_LOCK}
216          * @return this of {@link Builder}
217          */
setSource(@olorSource String source)218         public Builder setSource(@ColorSource String source) {
219             mSource = source;
220             return this;
221         }
222 
223         /**
224          * Sets the source of this color seed
225          * @param style color style of {@link Style}
226          * @return this of {@link Builder}
227          */
setStyle(Style style)228         public Builder setStyle(Style style) {
229             mStyle = style;
230             return this;
231         }
232 
233         /**
234          * Sets color option index of seed
235          * @param index color option index
236          * @return this of {@link ColorBundle.Builder}
237          */
setIndex(int index)238         public Builder setIndex(int index) {
239             mIndex = index;
240             return this;
241         }
242 
243         /**
244          * Sets as default bundle
245          * @return this of {@link Builder}
246          */
asDefault()247         public Builder asDefault() {
248             mIsDefault = true;
249             return this;
250         }
251     }
252 }
253