1 /* 2 * Copyright (C) 2024 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.systemui.communal.util 18 19 import android.content.Context 20 import android.graphics.Color 21 import com.android.settingslib.Utils 22 import com.android.systemui.common.ui.domain.interactor.ConfigurationInteractor 23 import com.android.systemui.dagger.SysUISingleton 24 import com.android.systemui.dagger.qualifiers.Application 25 import javax.inject.Inject 26 import kotlinx.coroutines.CoroutineScope 27 import kotlinx.coroutines.flow.SharingStarted 28 import kotlinx.coroutines.flow.StateFlow 29 import kotlinx.coroutines.flow.map 30 import kotlinx.coroutines.flow.stateIn 31 32 /** Wrapper around colors used for the communal UI. */ 33 interface CommunalColors { 34 /** The background color of the glanceable hub. */ 35 val backgroundColor: StateFlow<Color> 36 } 37 38 @SysUISingleton 39 class CommunalColorsImpl 40 @Inject 41 constructor( 42 @Application applicationScope: CoroutineScope, 43 private val context: Context, 44 configurationInteractor: ConfigurationInteractor, 45 ) : CommunalColors { 46 override val backgroundColor: StateFlow<Color> = 47 configurationInteractor.onAnyConfigurationChange <lambda>null48 .map { loadBackgroundColor() } 49 .stateIn( 50 scope = applicationScope, 51 started = SharingStarted.WhileSubscribed(), 52 initialValue = loadBackgroundColor() 53 ) 54 loadBackgroundColornull55 private fun loadBackgroundColor(): Color = 56 Color.valueOf( 57 Utils.getColorAttrDefaultColor( 58 context, 59 com.android.internal.R.attr.materialColorOutlineVariant 60 ) 61 ) 62 } 63