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
17 package com.android.systemui.statusbar.notification.icon.ui.viewbinder
18
19 import com.android.app.tracing.traceSection
20 import com.android.internal.util.ContrastColorUtil
21 import com.android.systemui.res.R
22 import com.android.systemui.statusbar.StatusBarIconView
23 import com.android.systemui.statusbar.StatusBarIconView.NO_COLOR
24 import com.android.systemui.statusbar.notification.NotificationUtils
25 import com.android.systemui.statusbar.notification.icon.ui.viewmodel.NotificationIconColors
26 import com.android.systemui.util.view.viewBoundsOnScreen
27 import kotlinx.coroutines.flow.Flow
28
29 object StatusBarIconViewBinder {
30
31 // TODO(b/305739416): Once StatusBarIconView has its own Recommended Architecture stack, these
32 // methods can become private and we can have a single bind() method for SBIV and its
33 // view-model (which, at the time of this writing, does not yet exist).
34
35 suspend fun bindColor(view: StatusBarIconView, color: Flow<Int>) {
36 color.collectTracingEach("SBIV#bindColor") { color ->
37 // Set the color for the icons
38 view.staticDrawableColor = color
39 // Set the color for the overflow dot
40 view.setDecorColor(color)
41 }
42 }
43
44 suspend fun bindTintAlpha(view: StatusBarIconView, tintAlpha: Flow<Float>) {
45 tintAlpha.collectTracingEach("SBIV#bindTintAlpha") { amt -> view.setTintAlpha(amt) }
46 }
47
48 suspend fun bindAnimationsEnabled(view: StatusBarIconView, allowAnimation: Flow<Boolean>) {
49 allowAnimation.collectTracingEach("SBIV#bindAnimationsEnabled", view::setAllowAnimation)
50 }
51
52 suspend fun bindIconColors(
53 view: StatusBarIconView,
54 iconColors: Flow<NotificationIconColors>,
55 contrastColorUtil: ContrastColorUtil,
56 ) {
57 iconColors.collectTracingEach("SBIV#bindIconColors") { colors ->
58 // Set the icon color
59 val isPreL = java.lang.Boolean.TRUE == view.getTag(R.id.icon_is_pre_L)
60 val isColorized = !isPreL || NotificationUtils.isGrayscale(view, contrastColorUtil)
61 view.staticDrawableColor =
62 if (isColorized) colors.staticDrawableColor(view.viewBoundsOnScreen()) else NO_COLOR
63 // Set the color for the overflow dot
64 view.setDecorColor(colors.tint)
65 }
66 }
67 }
68
collectTracingEachnull69 private suspend inline fun <T> Flow<T>.collectTracingEach(
70 tag: String,
71 crossinline collector: (T) -> Unit,
72 ) = collect { traceSection(tag) { collector(it) } }
73