1 /*
2  * Copyright (C) 2023 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  */
17 package com.android.customization.model.color
18 
19 import android.content.Context
20 import android.stats.style.StyleEnums
21 import android.view.View
22 import androidx.annotation.ColorInt
23 import com.android.customization.model.color.ColorOptionsProvider.ColorSource
24 import com.android.customization.picker.color.shared.model.ColorType
25 import com.android.systemui.monet.Style
26 import com.android.themepicker.R
27 
28 /**
29  * Represents a color option in the revamped UI, it can be used for both wallpaper and preset colors
30  */
31 class ColorOptionImpl(
32     title: String?,
33     overlayPackages: Map<String, String?>,
34     isDefault: Boolean,
35     private val source: String?,
36     seedColor: Int,
37     @Style.Type style: Int,
38     index: Int,
39     private val previewInfo: PreviewInfo,
40     val type: ColorType,
41 ) : ColorOption(title, overlayPackages, isDefault, seedColor, style, index) {
42 
43     class PreviewInfo(@ColorInt val lightColors: IntArray, @ColorInt val darkColors: IntArray) :
44         ColorOption.PreviewInfo {
45         @ColorInt
resolveColorsnull46         fun resolveColors(darkTheme: Boolean): IntArray {
47             return if (darkTheme) darkColors else lightColors
48         }
49     }
50 
bindThumbnailTilenull51     override fun bindThumbnailTile(view: View?) {
52         // Do nothing. This function will no longer be used in the Revamped UI
53     }
54 
getLayoutResIdnull55     override fun getLayoutResId(): Int {
56         return R.layout.color_option
57     }
58 
getPreviewInfonull59     override fun getPreviewInfo(): PreviewInfo {
60         return previewInfo
61     }
62 
getContentDescriptionnull63     override fun getContentDescription(context: Context): CharSequence? {
64         return title
65     }
66 
getSourcenull67     override fun getSource(): String? {
68         return source
69     }
70 
getSourceForLoggingnull71     override fun getSourceForLogging(): Int {
72         return when (getSource()) {
73             ColorOptionsProvider.COLOR_SOURCE_PRESET -> StyleEnums.COLOR_SOURCE_PRESET_COLOR
74             ColorOptionsProvider.COLOR_SOURCE_HOME -> StyleEnums.COLOR_SOURCE_HOME_SCREEN_WALLPAPER
75             ColorOptionsProvider.COLOR_SOURCE_LOCK -> StyleEnums.COLOR_SOURCE_LOCK_SCREEN_WALLPAPER
76             else -> StyleEnums.COLOR_SOURCE_UNSPECIFIED
77         }
78     }
79 
getStyleForLoggingnull80     override fun getStyleForLogging(): Int = Style.toString(style).hashCode()
81 
82     class Builder {
83         var title: String? = null
84 
85         @ColorInt var lightColors: IntArray = intArrayOf()
86 
87         @ColorInt var darkColors: IntArray = intArrayOf()
88 
89         @ColorSource var source: String? = null
90         var isDefault = false
91         @ColorInt var seedColor = 0
92         @Style.Type var style = Style.TONAL_SPOT
93         var index = 0
94         var packages: MutableMap<String, String?> = HashMap()
95         var type = ColorType.WALLPAPER_COLOR
96 
97         fun build(): ColorOptionImpl {
98             return ColorOptionImpl(
99                 title,
100                 packages,
101                 isDefault,
102                 source,
103                 seedColor,
104                 style,
105                 index,
106                 createPreviewInfo(),
107                 type,
108             )
109         }
110 
111         private fun createPreviewInfo(): PreviewInfo {
112             return PreviewInfo(lightColors, darkColors)
113         }
114 
115         fun addOverlayPackage(category: String?, packageName: String?): ColorOptionImpl.Builder {
116             category?.let { packages[category] = packageName }
117             return this
118         }
119     }
120 }
121