summaryrefslogtreecommitdiff
path: root/src/com/android/customization/model/color/ColorSeedOption.java
blob: ed38049e18f147ff9a86e241fe61f1b016888613 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * 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.android.customization.model.color;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.PorterDuff.Mode;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.ColorInt;
import androidx.annotation.VisibleForTesting;

import com.android.customization.model.color.ColorOptionsProvider.ColorSource;
import com.android.systemui.monet.Style;
import com.android.wallpaper.R;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Represents a seed color obtained from WallpaperColors, for the user to chose as their theming
 * option.
 */
public class ColorSeedOption extends ColorOption {

    private final PreviewInfo mPreviewInfo;
    @ColorSource
    private final String mSource;

    @VisibleForTesting
    ColorSeedOption(String title, Map<String, String> overlayPackages, boolean isDefault,
            @ColorSource String source, Style style, int index, PreviewInfo previewInfo) {
        super(title, overlayPackages, isDefault, style, index);
        mSource = source;
        mPreviewInfo = previewInfo;
    }

    @Override
    public PreviewInfo getPreviewInfo() {
        return mPreviewInfo;
    }

    @Override
    public String getSource() {
        return mSource;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.color_option;
    }

    @Override
    public void bindThumbnailTile(View view) {
        Resources res = view.getContext().getResources();
        @ColorInt int[] colors = mPreviewInfo.resolveColors(res);

        for (int i = 0; i < mPreviewColorIds.length; i++) {
            ImageView colorPreviewImageView = view.findViewById(mPreviewColorIds[i]);
            colorPreviewImageView.getDrawable().setColorFilter(colors[i], Mode.SRC);
        }

        view.setContentDescription(getContentDescription(view.getContext()));
    }

    @Override
    public CharSequence getContentDescription(Context context) {
        // Override because we want all options with the same description.
        return context.getString(R.string.wallpaper_color_title);
    }

    /**
     * The preview information of {@link ColorOption}
     */
    public static class PreviewInfo implements ColorOption.PreviewInfo {
        @ColorInt public int[] lightColors;
        @ColorInt public int[] darkColors;

        private PreviewInfo(@ColorInt int[] lightColors, @ColorInt int[] darkColors) {
            this.lightColors = lightColors;
            this.darkColors = darkColors;
        }

        /**
         * Returns the colors to be applied corresponding with the current
         * configuration's UI mode.
         * @param res resources to read to the UI mode configuration from
         * @return one of {@link #lightColors} or {@link #darkColors}
         */
        @ColorInt
        public int[] resolveColors(Resources res) {
            boolean night = (res.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK)
                    == Configuration.UI_MODE_NIGHT_YES;
            return night ? darkColors : lightColors;
        }

        /**
         * Returns the preview colors based on whether dark theme or light theme colors are
         * requested.
         * @param darkTheme if true, returns dark theme colors, otherwise returns light theme colors
         * @return one of {@link #lightColors} or {@link #darkColors}
         */
        @ColorInt
        public int[] resolveColors(boolean darkTheme) {
            return darkTheme ? darkColors : lightColors;
        }
    }

    /**
     * The builder of ColorSeedOption
     */
    public static class Builder {
        protected String mTitle;
        @ColorInt
        private int[] mLightColors;
        @ColorInt
        private int[] mDarkColors;
        @ColorSource
        private String mSource;
        private boolean mIsDefault;
        private Style mStyle = Style.TONAL_SPOT;
        private int mIndex;
        protected Map<String, String> mPackages = new HashMap<>();

        /**
         * Builds the ColorSeedOption
         * @return new {@link ColorOption} object
         */
        public ColorSeedOption build() {
            return new ColorSeedOption(mTitle, mPackages, mIsDefault, mSource, mStyle, mIndex,
                    createPreviewInfo());
        }

        /**
         * Creates preview information
         * @return the {@link PreviewInfo} object
         */
        public PreviewInfo createPreviewInfo() {
            return new PreviewInfo(mLightColors, mDarkColors);
        }

        public Map<String, String> getPackages() {
            return Collections.unmodifiableMap(mPackages);
        }

        /**
         * Gets title of {@link ColorOption} object
         * @return title string
         */
        public String getTitle() {
            return mTitle;
        }

        /**
         * Sets title of bundle
         * @param title specified title
         * @return this of {@link ColorBundle.Builder}
         */
        public Builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        /**
         * Sets the colors for preview in light mode
         * @param lightColors  {@link ColorInt} colors for light mode
         * @return this of {@link Builder}
         */
        public Builder setLightColors(@ColorInt int[] lightColors) {
            mLightColors = lightColors;
            return this;
        }

        /**
         * Sets the colors for preview in light mode
         * @param darkColors  {@link ColorInt} colors for light mode
         * @return this of {@link Builder}
         */
        public Builder setDarkColors(@ColorInt int[] darkColors) {
            mDarkColors = darkColors;
            return this;
        }


        /**
         * Sets overlay package for bundle
         * @param category the category of bundle
         * @param packageName tha name of package in the category
         * @return this of {@link Builder}
         */
        public Builder addOverlayPackage(String category, String packageName) {
            mPackages.put(category, packageName);
            return this;
        }

        /**
         * Sets the source of this color seed
         * @param source typically either {@link ColorOptionsProvider#COLOR_SOURCE_HOME} or
         *              {@link ColorOptionsProvider#COLOR_SOURCE_LOCK}
         * @return this of {@link Builder}
         */
        public Builder setSource(@ColorSource String source) {
            mSource = source;
            return this;
        }

        /**
         * Sets the source of this color seed
         * @param style color style of {@link Style}
         * @return this of {@link Builder}
         */
        public Builder setStyle(Style style) {
            mStyle = style;
            return this;
        }

        /**
         * Sets color option index of seed
         * @param index color option index
         * @return this of {@link ColorBundle.Builder}
         */
        public Builder setIndex(int index) {
            mIndex = index;
            return this;
        }

        /**
         * Sets as default bundle
         * @return this of {@link Builder}
         */
        public Builder asDefault() {
            mIsDefault = true;
            return this;
        }
    }
}