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 static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_COLOR; 19 import static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_SYSTEM_PALETTE; 20 import static com.android.customization.model.color.ColorUtils.toColorString; 21 22 import android.content.Context; 23 import android.content.pm.PackageManager; 24 import android.text.TextUtils; 25 import android.util.Log; 26 27 import androidx.annotation.ColorInt; 28 29 import com.android.systemui.monet.ColorScheme; 30 import com.android.systemui.monet.Style; 31 32 /** 33 * Utility class to read all the details of a color bundle for previewing it 34 * (eg, actual color values) 35 */ 36 class ColorBundlePreviewExtractor { 37 38 private static final String TAG = "ColorBundlePreviewExtractor"; 39 40 private final PackageManager mPackageManager; 41 ColorBundlePreviewExtractor(Context context)42 ColorBundlePreviewExtractor(Context context) { 43 mPackageManager = context.getPackageManager(); 44 } 45 addSecondaryColor(ColorBundle.Builder builder, @ColorInt int color)46 void addSecondaryColor(ColorBundle.Builder builder, @ColorInt int color) { 47 ColorScheme darkColorScheme = new ColorScheme(color, true); 48 ColorScheme lightColorScheme = new ColorScheme(color, false); 49 int lightSecondary = lightColorScheme.getAccentColor(); 50 int darkSecondary = darkColorScheme.getAccentColor(); 51 builder.addOverlayPackage(OVERLAY_CATEGORY_COLOR, toColorString(color)) 52 .setColorSecondaryLight(lightSecondary) 53 .setColorSecondaryDark(darkSecondary); 54 } 55 addPrimaryColor(ColorBundle.Builder builder, @ColorInt int color)56 void addPrimaryColor(ColorBundle.Builder builder, @ColorInt int color) { 57 ColorScheme darkColorScheme = new ColorScheme(color, true); 58 ColorScheme lightColorScheme = new ColorScheme(color, false); 59 int lightPrimary = lightColorScheme.getAccentColor(); 60 int darkPrimary = darkColorScheme.getAccentColor(); 61 builder.addOverlayPackage(OVERLAY_CATEGORY_SYSTEM_PALETTE, toColorString(color)) 62 .setColorPrimaryLight(lightPrimary) 63 .setColorPrimaryDark(darkPrimary); 64 } 65 addColorStyle(ColorBundle.Builder builder, String styleName)66 void addColorStyle(ColorBundle.Builder builder, String styleName) { 67 Style s = Style.TONAL_SPOT; 68 if (!TextUtils.isEmpty(styleName)) { 69 try { 70 s = Style.valueOf(styleName); 71 } catch (IllegalArgumentException e) { 72 Log.i(TAG, "Unknown style : " + styleName + ". Will default to TONAL_SPOT."); 73 } 74 } 75 builder.setStyle(s); 76 } 77 } 78