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 android.graphics.Rect
20 import android.view.View
21 import com.android.app.tracing.traceSection
22 import com.android.internal.util.ContrastColorUtil
23 import com.android.systemui.Flags
24 import com.android.systemui.res.R
25 import com.android.systemui.statusbar.StatusBarIconView
26 import com.android.systemui.statusbar.StatusBarIconView.NO_COLOR
27 import com.android.systemui.statusbar.notification.NotificationUtils
28 import com.android.systemui.statusbar.notification.icon.ui.viewmodel.NotificationIconColors
29 import kotlinx.coroutines.flow.Flow
30 
31 object StatusBarIconViewBinder {
32 
33     // TODO(b/305739416): Once StatusBarIconView has its own Recommended Architecture stack, these
34     //  methods can become private and we can have a single bind() method for SBIV and its
35     //  view-model (which, at the time of this writing, does not yet exist).
36 
37     suspend fun bindColor(view: StatusBarIconView, color: Flow<Int>) {
38         color.collectTracingEach("SBIV#bindColor") { color ->
39             // Don't change the icon color if an app icon experiment is enabled.
40             if (!android.app.Flags.notificationsUseAppIcon()) {
41                 view.staticDrawableColor = color
42             }
43             // Continue changing the overflow dot color
44             view.setDecorColor(color)
45         }
46     }
47 
48     suspend fun bindTintAlpha(view: StatusBarIconView, tintAlpha: Flow<Float>) {
49         tintAlpha.collectTracingEach("SBIV#bindTintAlpha") { amt -> view.setTintAlpha(amt) }
50     }
51 
52     suspend fun bindAnimationsEnabled(view: StatusBarIconView, allowAnimation: Flow<Boolean>) {
53         allowAnimation.collectTracingEach("SBIV#bindAnimationsEnabled", view::setAllowAnimation)
54     }
55 
56     suspend fun bindIconColors(
57         view: StatusBarIconView,
58         iconColors: Flow<NotificationIconColors>,
59         contrastColorUtil: ContrastColorUtil,
60     ) {
61         iconColors.collectTracingEach("SBIV#bindIconColors") { colors ->
62             // Don't change the icon color if an app icon experiment is enabled.
63             if (!android.app.Flags.notificationsUseAppIcon()) {
64                 val isPreL = java.lang.Boolean.TRUE == view.getTag(R.id.icon_is_pre_L)
65                 val isColorized = !isPreL || NotificationUtils.isGrayscale(view, contrastColorUtil)
66                 view.staticDrawableColor =
67                     if (isColorized) colors.staticDrawableColor(view.viewBounds) else NO_COLOR
68             }
69             // Continue changing the overflow dot color
70             view.setDecorColor(colors.tint)
71         }
72     }
73 }
74 
75 private val View.viewBounds: Rect
76     get() {
77         val tmpArray = intArrayOf(0, 0)
78         getLocationOnScreen(tmpArray)
79         return Rect(
80             /* left = */ tmpArray[0],
81             /* top = */ tmpArray[1],
82             /* right = */ left + width,
83             /* bottom = */ top + height,
84         )
85     }
86 
collectTracingEachnull87 private suspend inline fun <T> Flow<T>.collectTracingEach(
88     tag: String,
89     crossinline collector: (T) -> Unit,
90 ) = collect { traceSection(tag) { collector(it) } }
91