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 * @return one of {@link #lightColors} or {@link #darkColors} 104 */ 105 @ColorInt resolveColors(Resources res)106 public int[] resolveColors(Resources res) { 107 boolean night = (res.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) 108 == Configuration.UI_MODE_NIGHT_YES; 109 return night ? darkColors : lightColors; 110 } 111 } 112 113 /** 114 * The builder of ColorSeedOption 115 */ 116 public static class Builder { 117 protected String mTitle; 118 @ColorInt 119 private int[] mLightColors; 120 @ColorInt 121 private int[] mDarkColors; 122 @ColorSource 123 private String mSource; 124 private boolean mIsDefault; 125 private Style mStyle = Style.TONAL_SPOT; 126 private int mIndex; 127 protected Map<String, String> mPackages = new HashMap<>(); 128 129 /** 130 * Builds the ColorSeedOption 131 * @return new {@link ColorOption} object 132 */ build()133 public ColorSeedOption build() { 134 return new ColorSeedOption(mTitle, mPackages, mIsDefault, mSource, mStyle, mIndex, 135 createPreviewInfo()); 136 } 137 138 /** 139 * Creates preview information 140 * @return the {@link PreviewInfo} object 141 */ createPreviewInfo()142 public PreviewInfo createPreviewInfo() { 143 return new PreviewInfo(mLightColors, mDarkColors); 144 } 145 getPackages()146 public Map<String, String> getPackages() { 147 return Collections.unmodifiableMap(mPackages); 148 } 149 150 /** 151 * Gets title of {@link ColorOption} object 152 * @return title string 153 */ getTitle()154 public String getTitle() { 155 return mTitle; 156 } 157 158 /** 159 * Sets title of bundle 160 * @param title specified title 161 * @return this of {@link ColorBundle.Builder} 162 */ setTitle(String title)163 public Builder setTitle(String title) { 164 mTitle = title; 165 return this; 166 } 167 168 /** 169 * Sets the colors for preview in light mode 170 * @param lightColors {@link ColorInt} colors for light mode 171 * @return this of {@link Builder} 172 */ setLightColors(@olorInt int[] lightColors)173 public Builder setLightColors(@ColorInt int[] lightColors) { 174 mLightColors = lightColors; 175 return this; 176 } 177 178 /** 179 * Sets the colors for preview in light mode 180 * @param darkColors {@link ColorInt} colors for light mode 181 * @return this of {@link Builder} 182 */ setDarkColors(@olorInt int[] darkColors)183 public Builder setDarkColors(@ColorInt int[] darkColors) { 184 mDarkColors = darkColors; 185 return this; 186 } 187 188 189 /** 190 * Sets overlay package for bundle 191 * @param category the category of bundle 192 * @param packageName tha name of package in the category 193 * @return this of {@link Builder} 194 */ addOverlayPackage(String category, String packageName)195 public Builder addOverlayPackage(String category, String packageName) { 196 mPackages.put(category, packageName); 197 return this; 198 } 199 200 /** 201 * Sets the source of this color seed 202 * @param source typically either {@link ColorOptionsProvider#COLOR_SOURCE_HOME} or 203 * {@link ColorOptionsProvider#COLOR_SOURCE_LOCK} 204 * @return this of {@link Builder} 205 */ setSource(@olorSource String source)206 public Builder setSource(@ColorSource String source) { 207 mSource = source; 208 return this; 209 } 210 211 /** 212 * Sets the source of this color seed 213 * @param style color style of {@link Style} 214 * @return this of {@link Builder} 215 */ setStyle(Style style)216 public Builder setStyle(Style style) { 217 mStyle = style; 218 return this; 219 } 220 221 /** 222 * Sets color option index of seed 223 * @param index color option index 224 * @return this of {@link ColorBundle.Builder} 225 */ setIndex(int index)226 public Builder setIndex(int index) { 227 mIndex = index; 228 return this; 229 } 230 231 /** 232 * Sets as default bundle 233 * @return this of {@link Builder} 234 */ asDefault()235 public Builder asDefault() { 236 mIsDefault = true; 237 return this; 238 } 239 } 240 } 241