1 /*
2  * Copyright (C) 2020 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.doze
18 
19 import android.view.Display
20 import com.android.systemui.doze.DozeLog.Reason
21 import com.android.systemui.doze.DozeLog.reasonToString
22 import com.android.systemui.log.LogBuffer
23 import com.android.systemui.log.LogLevel.DEBUG
24 import com.android.systemui.log.LogLevel.ERROR
25 import com.android.systemui.log.LogLevel.INFO
26 import com.android.systemui.log.dagger.DozeLog
27 import com.android.systemui.statusbar.policy.DevicePostureController
28 import java.text.SimpleDateFormat
29 import java.util.Date
30 import java.util.Locale
31 import javax.inject.Inject
32 
33 /** Interface for logging messages to the [DozeLog]. */
34 class DozeLogger @Inject constructor(
35     @DozeLog private val buffer: LogBuffer
36 ) {
37     fun logPickupWakeup(isWithinVibrationThreshold: Boolean) {
38         buffer.log(TAG, DEBUG, {
39             bool1 = isWithinVibrationThreshold
40         }, {
41             "PickupWakeup withinVibrationThreshold=$bool1"
42         })
43     }
44 
45     fun logPulseStart(@Reason reason: Int) {
46         buffer.log(TAG, INFO, {
47             int1 = reason
48         }, {
49             "Pulse start, reason=${reasonToString(int1)}"
50         })
51     }
52 
53     fun logPulseFinish() {
54         buffer.log(TAG, INFO, {}, { "Pulse finish" })
55     }
56 
57     fun logNotificationPulse() {
58         buffer.log(TAG, INFO, {}, { "Notification pulse" })
59     }
60 
61     fun logDozing(isDozing: Boolean) {
62         buffer.log(TAG, INFO, {
63             bool1 = isDozing
64         }, {
65             "Dozing=$bool1"
66         })
67     }
68 
69     fun logDozingChanged(isDozing: Boolean) {
70         buffer.log(TAG, INFO, {
71             bool1 = isDozing
72         }, {
73             "Dozing changed dozing=$bool1"
74         })
75     }
76 
77     fun logDozingSuppressed(isDozingSuppressed: Boolean) {
78         buffer.log(TAG, INFO, {
79             bool1 = isDozingSuppressed
80         }, {
81             "DozingSuppressed=$bool1"
82         })
83     }
84 
85     fun logFling(
86         expand: Boolean,
87         aboveThreshold: Boolean,
88         thresholdNeeded: Boolean,
89         screenOnFromTouch: Boolean
90     ) {
91         buffer.log(TAG, DEBUG, {
92             bool1 = expand
93             bool2 = aboveThreshold
94             bool3 = thresholdNeeded
95             bool4 = screenOnFromTouch
96         }, {
97             "Fling expand=$bool1 aboveThreshold=$bool2 thresholdNeeded=$bool3 " +
98                     "screenOnFromTouch=$bool4"
99         })
100     }
101 
102     fun logEmergencyCall() {
103         buffer.log(TAG, INFO, {}, { "Emergency call" })
104     }
105 
106     fun logKeyguardBouncerChanged(isShowing: Boolean) {
107         buffer.log(TAG, INFO, {
108             bool1 = isShowing
109         }, {
110             "Keyguard bouncer changed, showing=$bool1"
111         })
112     }
113 
114     fun logScreenOn(isPulsing: Boolean) {
115         buffer.log(TAG, INFO, {
116             bool1 = isPulsing
117         }, {
118             "Screen on, pulsing=$bool1"
119         })
120     }
121 
122     fun logScreenOff(why: Int) {
123         buffer.log(TAG, INFO, {
124             int1 = why
125         }, {
126             "Screen off, why=$int1"
127         })
128     }
129 
130     fun logMissedTick(delay: String) {
131         buffer.log(TAG, ERROR, {
132             str1 = delay
133         }, {
134             "Missed AOD time tick by $str1"
135         })
136     }
137 
138     fun logTimeTickScheduled(whenAt: Long, triggerAt: Long) {
139         buffer.log(TAG, DEBUG, {
140             long1 = whenAt
141             long2 = triggerAt
142         }, {
143             "Time tick scheduledAt=${DATE_FORMAT.format(Date(long1))} " +
144                     "triggerAt=${DATE_FORMAT.format(Date(long2))}"
145         })
146     }
147 
148     fun logKeyguardVisibilityChange(isShowing: Boolean) {
149         buffer.log(TAG, INFO, {
150             bool1 = isShowing
151         }, {
152             "Keyguard visibility change, isShowing=$bool1"
153         })
154     }
155 
156     fun logDozeStateChanged(state: DozeMachine.State) {
157         buffer.log(TAG, INFO, {
158             str1 = state.name
159         }, {
160             "Doze state changed to $str1"
161         })
162     }
163 
164     fun logStateChangedSent(state: DozeMachine.State) {
165         buffer.log(TAG, INFO, {
166             str1 = state.name
167         }, {
168             "Doze state sent to all DozeMachineParts stateSent=$str1"
169         })
170     }
171 
172     fun logDisplayStateDelayedByUdfps(delayedDisplayState: Int) {
173         buffer.log(TAG, INFO, {
174             str1 = Display.stateToString(delayedDisplayState)
175         }, {
176             "Delaying display state change to: $str1 due to UDFPS activity"
177         })
178     }
179 
180     fun logDisplayStateChanged(displayState: Int) {
181         buffer.log(TAG, INFO, {
182             str1 = Display.stateToString(displayState)
183         }, {
184             "Display state changed to $str1"
185         })
186     }
187 
188     fun logWakeDisplay(isAwake: Boolean, @Reason reason: Int) {
189         buffer.log(TAG, DEBUG, {
190             bool1 = isAwake
191             int1 = reason
192         }, {
193             "Display wakefulness changed, isAwake=$bool1, reason=${reasonToString(int1)}"
194         })
195     }
196 
197     fun logProximityResult(isNear: Boolean, millis: Long, @Reason reason: Int) {
198         buffer.log(TAG, DEBUG, {
199             bool1 = isNear
200             long1 = millis
201             int1 = reason
202         }, {
203             "Proximity result reason=${reasonToString(int1)} near=$bool1 millis=$long1"
204         })
205     }
206 
207     fun logPostureChanged(posture: Int, partUpdated: String) {
208         buffer.log(TAG, INFO, {
209             int1 = posture
210             str1 = partUpdated
211         }, {
212             "Posture changed, posture=${DevicePostureController.devicePostureToString(int1)}" +
213                     " partUpdated=$str1"
214         })
215     }
216 
217     fun logPulseDropped(pulsePending: Boolean, state: DozeMachine.State, blocked: Boolean) {
218         buffer.log(TAG, INFO, {
219             bool1 = pulsePending
220             str1 = state.name
221             bool2 = blocked
222         }, {
223             "Pulse dropped, pulsePending=$bool1 state=$str1 blocked=$bool2"
224         })
225     }
226 
227     fun logSensorEventDropped(sensorEvent: Int, reason: String) {
228         buffer.log(TAG, INFO, {
229             int1 = sensorEvent
230             str1 = reason
231         }, {
232             "SensorEvent [$int1] dropped, reason=$str1"
233         })
234     }
235 
236     fun logPulseDropped(reason: String) {
237         buffer.log(TAG, INFO, {
238             str1 = reason
239         }, {
240             "Pulse dropped, why=$str1"
241         })
242     }
243 
244     fun logPulseTouchDisabledByProx(disabled: Boolean) {
245         buffer.log(TAG, DEBUG, {
246             bool1 = disabled
247         }, {
248             "Pulse touch modified by prox, disabled=$bool1"
249         })
250     }
251 
252     fun logSensorTriggered(@Reason reason: Int) {
253         buffer.log(TAG, DEBUG, {
254             int1 = reason
255         }, {
256             "Sensor triggered, type=${reasonToString(int1)}"
257         })
258     }
259 
260     fun logDozeSuppressed(state: DozeMachine.State) {
261         buffer.log(TAG, INFO, {
262             str1 = state.name
263         }, {
264             "Doze state suppressed, state=$str1"
265         })
266     }
267 
268     fun logDozeScreenBrightness(brightness: Int) {
269         buffer.log(TAG, INFO, {
270             int1 = brightness
271         }, {
272             "Doze screen brightness set, brightness=$int1"
273         })
274     }
275 
276     fun logSetAodDimmingScrim(scrimOpacity: Long) {
277         buffer.log(TAG, INFO, {
278             long1 = scrimOpacity
279         }, {
280             "Doze aod dimming scrim opacity set, opacity=$long1"
281         })
282     }
283 }
284 
285 private const val TAG = "DozeLog"
286 
287 val DATE_FORMAT = SimpleDateFormat("MM-dd HH:mm:ss.S", Locale.US)
288