1 /*
<lambda>null2 * 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 package com.android.systemui.statusbar.phone.domain.interactor
17
18 import android.graphics.Rect
19 import com.android.systemui.dagger.SysUISingleton
20 import com.android.systemui.plugins.DarkIconDispatcher
21 import com.android.systemui.statusbar.phone.SysuiDarkIconDispatcher.DarkChange
22 import com.android.systemui.statusbar.phone.data.repository.DarkIconRepository
23 import com.android.systemui.statusbar.phone.domain.model.DarkState
24 import javax.inject.Inject
25 import kotlinx.coroutines.flow.Flow
26 import kotlinx.coroutines.flow.conflate
27 import kotlinx.coroutines.flow.distinctUntilChanged
28 import kotlinx.coroutines.flow.map
29
30 /** States pertaining to calculating colors for icons in dark mode. */
31 @SysUISingleton
32 class DarkIconInteractor @Inject constructor(private val repository: DarkIconRepository) {
33 /** Dark-mode state for tinting icons. */
34 fun darkState(displayId: Int): Flow<DarkState> =
35 repository.darkState(displayId).map { DarkState(it.areas, it.tint, it.darkIntensity) }
36
37 /**
38 * Given a display id: returns a flow of [IsAreaDark], a function that can tell you if a given
39 * [Rect] should be tinted dark or not. This flow ignores [DarkChange.tint] and
40 * [DarkChange.darkIntensity]
41 */
42 fun isAreaDark(displayId: Int): Flow<IsAreaDark> {
43 return repository.darkState(displayId).toIsAreaDark()
44 }
45
46 companion object {
47 /**
48 * Convenience function to convert between the repository's [darkState] into [IsAreaDark]
49 * type flows.
50 */
51 @JvmStatic
52 fun Flow<DarkChange>.toIsAreaDark(): Flow<IsAreaDark> =
53 map { darkChange ->
54 DarkStateWithoutIntensity(darkChange.areas, darkChange.darkIntensity < 0.5f)
55 }
56 .distinctUntilChanged()
57 .map { darkState ->
58 IsAreaDark { viewBounds: Rect ->
59 if (DarkIconDispatcher.isInAreas(darkState.areas, viewBounds)) {
60 darkState.isDark
61 } else {
62 false
63 }
64 }
65 }
66 .conflate()
67 .distinctUntilChanged()
68 }
69 }
70
71 /** So we can map between [DarkState] and a single boolean, but based on intensity */
72 private data class DarkStateWithoutIntensity(val areas: Collection<Rect>, val isDark: Boolean)
73
74 /** Given a region on screen, determine if the foreground should be dark or light */
interfacenull75 fun interface IsAreaDark {
76 fun isDark(viewBounds: Rect): Boolean
77 }
78