1 /*
2  * Copyright (c) 2022 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 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and limitations under the License.
12  */
13 
14 package com.android.systemui.statusbar.notification
15 
16 import com.android.systemui.log.LogBuffer
17 import com.android.systemui.log.core.LogLevel.DEBUG
18 import com.android.systemui.log.dagger.NotificationLockscreenLog
19 import com.android.systemui.statusbar.StatusBarState
20 import javax.inject.Inject
21 
22 class NotificationWakeUpCoordinatorLogger
23 @Inject
24 constructor(@NotificationLockscreenLog private val buffer: LogBuffer) {
25     private var allowThrottle = true
26     private var lastSetDozeAmountLogInputWasFractional = false
27     private var lastSetDozeAmountLogDelayWasFractional = false
28     private var lastSetDozeAmountLogState = -1
29     private var lastSetHardOverride: Float? = null
30     private var lastOnDozeAmountChangedLogWasFractional = false
31     private var lastSetDelayDozeAmountOverrideLogWasFractional = false
32     private var lastSetVisibilityAmountLogWasFractional = false
33     private var lastSetHideAmountLogWasFractional = false
34     private var lastSetHideAmount = -1f
35 
36     fun logUpdateDozeAmount(
37         inputLinear: Float,
38         delayLinear: Float,
39         hardOverride: Float?,
40         outputLinear: Float,
41         state: Int,
42         changed: Boolean,
43     ) {
44         // Avoid logging on every frame of the animation if important values are not changing
45         val isInputFractional = inputLinear != 1f && inputLinear != 0f
46         val isDelayFractional = delayLinear != 1f && delayLinear != 0f
47         if (
48             (isInputFractional || isDelayFractional) &&
49                 lastSetDozeAmountLogInputWasFractional == isInputFractional &&
50                 lastSetDozeAmountLogDelayWasFractional == isDelayFractional &&
51                 lastSetDozeAmountLogState == state &&
52                 lastSetHardOverride == hardOverride &&
53                 allowThrottle
54         ) {
55             return
56         }
57         lastSetDozeAmountLogInputWasFractional = isInputFractional
58         lastSetDozeAmountLogDelayWasFractional = isDelayFractional
59         lastSetDozeAmountLogState = state
60         lastSetHardOverride = hardOverride
61 
62         buffer.log(
63             TAG,
64             DEBUG,
65             {
66                 double1 = inputLinear.toDouble()
67                 str1 = hardOverride.toString()
68                 str2 = outputLinear.toString()
69                 str3 = delayLinear.toString()
70                 int1 = state
71                 bool1 = changed
72             },
73             {
74                 "updateDozeAmount() inputLinear=$double1 delayLinear=$str3" +
75                     " hardOverride=$str1 outputLinear=$str2" +
76                     " state=${StatusBarState.toString(int1)} changed=$bool1"
77             }
78         )
79     }
80 
81     fun logSetDozeAmountOverride(dozing: Boolean, source: String) {
82         buffer.log(
83             TAG,
84             DEBUG,
85             {
86                 bool1 = dozing
87                 str1 = source
88             },
89             { "setDozeAmountOverride(dozing=$bool1, source=\"$str1\")" }
90         )
91     }
92 
93     fun logMaybeClearHardDozeAmountOverrideHidingNotifs(
94         willRemove: Boolean,
95         onKeyguard: Boolean,
96         dozing: Boolean,
97         bypass: Boolean,
98         animating: Boolean,
99     ) {
100         buffer.log(
101             TAG,
102             DEBUG,
103             {
104                 str1 =
105                     "willRemove=$willRemove onKeyguard=$onKeyguard dozing=$dozing" +
106                         " bypass=$bypass animating=$animating"
107             },
108             { "maybeClearHardDozeAmountOverrideHidingNotifs() $str1" }
109         )
110     }
111 
112     fun logOnDozeAmountChanged(linear: Float, eased: Float) {
113         // Avoid logging on every frame of the animation when values are fractional
114         val isFractional = linear != 1f && linear != 0f
115         if (lastOnDozeAmountChangedLogWasFractional && isFractional && allowThrottle) return
116         lastOnDozeAmountChangedLogWasFractional = isFractional
117         buffer.log(
118             TAG,
119             DEBUG,
120             {
121                 double1 = linear.toDouble()
122                 str2 = eased.toString()
123             },
124             { "onDozeAmountChanged(linear=$double1, eased=$str2)" }
125         )
126     }
127 
128     fun logSetDelayDozeAmountOverride(linear: Float) {
129         // Avoid logging on every frame of the animation when values are fractional
130         val isFractional = linear != 1f && linear != 0f
131         if (lastSetDelayDozeAmountOverrideLogWasFractional && isFractional && allowThrottle) return
132         lastSetDelayDozeAmountOverrideLogWasFractional = isFractional
133         buffer.log(
134             TAG,
135             DEBUG,
136             { double1 = linear.toDouble() },
137             { "setDelayDozeAmountOverride($double1)" }
138         )
139     }
140 
141     fun logSetVisibilityAmount(linear: Float) {
142         // Avoid logging on every frame of the animation when values are fractional
143         val isFractional = linear != 1f && linear != 0f
144         if (lastSetVisibilityAmountLogWasFractional && isFractional && allowThrottle) return
145         lastSetVisibilityAmountLogWasFractional = isFractional
146         buffer.log(TAG, DEBUG, { double1 = linear.toDouble() }, { "setVisibilityAmount($double1)" })
147     }
148 
149     fun logSetHideAmount(linear: Float) {
150         // Avoid logging the same value repeatedly
151         if (lastSetHideAmount == linear && allowThrottle) return
152         lastSetHideAmount = linear
153         // Avoid logging on every frame of the animation when values are fractional
154         val isFractional = linear != 1f && linear != 0f
155         if (lastSetHideAmountLogWasFractional && isFractional && allowThrottle) return
156         lastSetHideAmountLogWasFractional = isFractional
157         buffer.log(TAG, DEBUG, { double1 = linear.toDouble() }, { "setHideAmount($double1)" })
158     }
159 
160     fun logStartDelayedDozeAmountAnimation(alreadyRunning: Boolean) {
161         buffer.log(
162             TAG,
163             DEBUG,
164             { bool1 = alreadyRunning },
165             { "startDelayedDozeAmountAnimation() alreadyRunning=$bool1" }
166         )
167     }
168 
169     fun logOnStateChanged(newState: Int, storedState: Int) {
170         buffer.log(
171             TAG,
172             DEBUG,
173             {
174                 int1 = newState
175                 int2 = storedState
176             },
177             {
178                 "onStateChanged(newState=${StatusBarState.toString(int1)})" +
179                     " stored=${StatusBarState.toString(int2)}"
180             }
181         )
182     }
183 
184     fun logSetWakingUp(wakingUp: Boolean, requestDelayedAnimation: Boolean) {
185         buffer.log(
186             TAG,
187             DEBUG,
188             {
189                 bool1 = wakingUp
190                 bool2 = requestDelayedAnimation
191             },
192             { "setWakingUp(wakingUp=$bool1, requestDelayedAnimation=$bool2)" }
193         )
194     }
195 
196     fun logDelayingClockWakeUpAnimation(delayingAnimation: Boolean) {
197         buffer.log(
198             TAG,
199             DEBUG,
200             { bool1 = delayingAnimation },
201             { "logDelayingClockWakeUpAnimation($bool1)" }
202         )
203     }
204 }
205 
206 private const val TAG = "NotificationWakeUpCoordinator"
207