blob: 8afdf1aeb0238b8f5c98f966d0d3e34f7b05e45b [file] [log] [blame]
John Spurlock813552c2014-09-19 08:30:21 -04001/*
2 * Copyright (C) 2014 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
17package com.android.systemui.doze;
18
Ned Burns30d67702020-01-28 12:58:45 -050019import android.annotation.IntDef;
John Spurlock813552c2014-09-19 08:30:21 -040020import android.util.TimeUtils;
21
Ned Burns30d67702020-01-28 12:58:45 -050022import androidx.annotation.NonNull;
23
John Spurlock813552c2014-09-19 08:30:21 -040024import com.android.keyguard.KeyguardUpdateMonitor;
25import com.android.keyguard.KeyguardUpdateMonitorCallback;
Beverlycc4a62f2019-09-26 14:55:28 -040026import com.android.systemui.DumpController;
Ned Burns30d67702020-01-28 12:58:45 -050027import com.android.systemui.Dumpable;
John Spurlock813552c2014-09-19 08:30:21 -040028
Ned Burns30d67702020-01-28 12:58:45 -050029import java.io.FileDescriptor;
John Spurlock813552c2014-09-19 08:30:21 -040030import java.io.PrintWriter;
Ned Burns30d67702020-01-28 12:58:45 -050031import java.lang.annotation.Retention;
32import java.lang.annotation.RetentionPolicy;
John Spurlock813552c2014-09-19 08:30:21 -040033
Beverlycc4a62f2019-09-26 14:55:28 -040034import javax.inject.Inject;
35import javax.inject.Singleton;
36
37/**
38 * Logs doze events for debugging and triaging purposes. Logs are dumped in bugreports or on demand:
39 * adb shell dumpsys activity service com.android.systemui/.SystemUIService \
Ned Burns30d67702020-01-28 12:58:45 -050040 * dependency DumpController DozeLog,DozeStats
Beverlycc4a62f2019-09-26 14:55:28 -040041 */
42@Singleton
Ned Burns30d67702020-01-28 12:58:45 -050043public class DozeLog implements Dumpable {
44 private final DozeLogger mLogger;
Beverly3ae892b2019-12-05 14:33:09 -050045
Beverlycc4a62f2019-09-26 14:55:28 -040046 private boolean mPulsing;
47 private long mSince;
48 private SummaryStats mPickupPulseNearVibrationStats;
49 private SummaryStats mPickupPulseNotNearVibrationStats;
50 private SummaryStats mNotificationPulseStats;
51 private SummaryStats mScreenOnPulsingStats;
52 private SummaryStats mScreenOnNotPulsingStats;
53 private SummaryStats mEmergencyCallStats;
54 private SummaryStats[][] mProxStats; // [reason][near/far]
John Spurlockeab28e62014-11-29 11:33:49 -050055
Beverlycc4a62f2019-09-26 14:55:28 -040056 @Inject
Ned Burns30d67702020-01-28 12:58:45 -050057 public DozeLog(
58 KeyguardUpdateMonitor keyguardUpdateMonitor,
59 DumpController dumpController,
60 DozeLogger logger) {
61 mLogger = logger;
Beverlycc4a62f2019-09-26 14:55:28 -040062 mSince = System.currentTimeMillis();
63 mPickupPulseNearVibrationStats = new SummaryStats();
64 mPickupPulseNotNearVibrationStats = new SummaryStats();
65 mNotificationPulseStats = new SummaryStats();
66 mScreenOnPulsingStats = new SummaryStats();
67 mScreenOnNotPulsingStats = new SummaryStats();
68 mEmergencyCallStats = new SummaryStats();
Ned Burns30d67702020-01-28 12:58:45 -050069 mProxStats = new SummaryStats[TOTAL_REASONS][2];
70 for (int i = 0; i < TOTAL_REASONS; i++) {
Beverlycc4a62f2019-09-26 14:55:28 -040071 mProxStats[i][0] = new SummaryStats();
72 mProxStats[i][1] = new SummaryStats();
73 }
John Spurlockeab28e62014-11-29 11:33:49 -050074
Beverlycc4a62f2019-09-26 14:55:28 -040075 if (keyguardUpdateMonitor != null) {
76 keyguardUpdateMonitor.registerCallback(mKeyguardCallback);
John Spurlock813552c2014-09-19 08:30:21 -040077 }
Ned Burns30d67702020-01-28 12:58:45 -050078
79 dumpController.registerDumpable("DumpStats", this);
Jorim Jaggi83969702015-06-05 14:59:24 -070080 }
81
Beverlycc4a62f2019-09-26 14:55:28 -040082 /**
83 * Appends pickup wakeup event to the logs
84 */
85 public void tracePickupWakeUp(boolean withinVibrationThreshold) {
Ned Burns30d67702020-01-28 12:58:45 -050086 mLogger.logPickupWakeup(withinVibrationThreshold);
87 (withinVibrationThreshold ? mPickupPulseNearVibrationStats
88 : mPickupPulseNotNearVibrationStats).append();
John Spurlock813552c2014-09-19 08:30:21 -040089 }
90
Beverlycc4a62f2019-09-26 14:55:28 -040091 /**
92 * Appends pulse started event to the logs.
93 * @param reason why the pulse started
94 */
Ned Burns30d67702020-01-28 12:58:45 -050095 public void tracePulseStart(@Reason int reason) {
96 mLogger.logPulseStart(reason);
97 mPulsing = true;
Beverlycc4a62f2019-09-26 14:55:28 -040098 }
99
100 /**
101 * Appends pulse finished event to the logs
102 */
103 public void tracePulseFinish() {
Ned Burns30d67702020-01-28 12:58:45 -0500104 mLogger.logPulseFinish();
105 mPulsing = false;
Beverlycc4a62f2019-09-26 14:55:28 -0400106 }
107
108 /**
109 * Appends pulse event to the logs
110 */
111 public void traceNotificationPulse() {
Ned Burns30d67702020-01-28 12:58:45 -0500112 mLogger.logNotificationPulse();
113 mNotificationPulseStats.append();
Beverlycc4a62f2019-09-26 14:55:28 -0400114 }
115
116 /**
117 * Appends dozing event to the logs
118 * @param dozing true if dozing, else false
119 */
120 public void traceDozing(boolean dozing) {
Ned Burns30d67702020-01-28 12:58:45 -0500121 mLogger.logDozing(dozing);
122 mPulsing = false;
Beverlycc4a62f2019-09-26 14:55:28 -0400123 }
124
125 /**
126 * Appends fling event to the logs
127 */
128 public void traceFling(boolean expand, boolean aboveThreshold, boolean thresholdNeeded,
John Spurlockee98df72014-09-30 09:56:05 -0400129 boolean screenOnFromTouch) {
Ned Burns30d67702020-01-28 12:58:45 -0500130 mLogger.logFling(expand, aboveThreshold, thresholdNeeded, screenOnFromTouch);
John Spurlock813552c2014-09-19 08:30:21 -0400131 }
132
Beverlycc4a62f2019-09-26 14:55:28 -0400133 /**
134 * Appends emergency call event to the logs
135 */
136 public void traceEmergencyCall() {
Ned Burns30d67702020-01-28 12:58:45 -0500137 mLogger.logEmergencyCall();
138 mEmergencyCallStats.append();
John Spurlock813552c2014-09-19 08:30:21 -0400139 }
140
Beverlycc4a62f2019-09-26 14:55:28 -0400141 /**
142 * Appends keyguard bouncer changed event to the logs
143 * @param showing true if the keyguard bouncer is showing, else false
144 */
145 public void traceKeyguardBouncerChanged(boolean showing) {
Ned Burns30d67702020-01-28 12:58:45 -0500146 mLogger.logKeyguardBouncerChanged(showing);
Beverlycc4a62f2019-09-26 14:55:28 -0400147 }
148
149 /**
150 * Appends screen-on event to the logs
151 */
152 public void traceScreenOn() {
Ned Burns30d67702020-01-28 12:58:45 -0500153 mLogger.logScreenOn(mPulsing);
154 (mPulsing ? mScreenOnPulsingStats : mScreenOnNotPulsingStats).append();
155 mPulsing = false;
Beverlycc4a62f2019-09-26 14:55:28 -0400156 }
157
158 /**
159 * Appends screen-off event to the logs
160 * @param why reason the screen is off
161 */
162 public void traceScreenOff(int why) {
Ned Burns30d67702020-01-28 12:58:45 -0500163 mLogger.logScreenOff(why);
Beverlycc4a62f2019-09-26 14:55:28 -0400164 }
165
166 /**
167 * Appends missed tick event to the logs
168 * @param delay of the missed tick
169 */
170 public void traceMissedTick(String delay) {
Ned Burns30d67702020-01-28 12:58:45 -0500171 mLogger.logMissedTick(delay);
Beverlycc4a62f2019-09-26 14:55:28 -0400172 }
173
174 /**
175 * Appends time tick scheduled event to the logs
176 * @param when time tick scheduled at
177 * @param triggerAt time tick trigger at
178 */
179 public void traceTimeTickScheduled(long when, long triggerAt) {
Ned Burns30d67702020-01-28 12:58:45 -0500180 mLogger.logTimeTickScheduled(when, triggerAt);
Beverlycc4a62f2019-09-26 14:55:28 -0400181 }
182
183 /**
184 * Appends keyguard visibility change event to the logs
185 * @param showing whether the keyguard is now showing
186 */
187 public void traceKeyguard(boolean showing) {
Ned Burns30d67702020-01-28 12:58:45 -0500188 mLogger.logKeyguardVisibilityChange(showing);
189 if (!showing) mPulsing = false;
Beverlycc4a62f2019-09-26 14:55:28 -0400190 }
191
192 /**
193 * Appends doze state changed event to the logs
194 * @param state new DozeMachine state
195 */
196 public void traceState(DozeMachine.State state) {
Ned Burns30d67702020-01-28 12:58:45 -0500197 mLogger.logDozeStateChanged(state);
Adrian Roosa6c03f82017-07-26 16:20:30 +0200198 }
199
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800200 /**
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800201 * Appends wake-display event to the logs.
202 * @param wake if we're waking up or sleeping.
203 */
Beverlycc4a62f2019-09-26 14:55:28 -0400204 public void traceWakeDisplay(boolean wake) {
Ned Burns30d67702020-01-28 12:58:45 -0500205 mLogger.logWakeDisplay(wake);
Lucas Dupinc81702e2018-08-09 15:41:55 -0700206 }
207
Beverlycc4a62f2019-09-26 14:55:28 -0400208 /**
209 * Appends proximity result event to the logs
210 * @param near true if near, else false
Beverlycc4a62f2019-09-26 14:55:28 -0400211 * @param reason why proximity result was triggered
212 */
Ned Burns30d67702020-01-28 12:58:45 -0500213 public void traceProximityResult(boolean near, long millis, @Reason int reason) {
214 mLogger.logProximityResult(near, millis, reason);
215 mProxStats[reason][near ? 0 : 1].append();
John Spurlock92b8d412014-10-03 17:12:40 -0400216 }
217
Ned Burns30d67702020-01-28 12:58:45 -0500218 @Override
219 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
John Spurlock813552c2014-09-19 08:30:21 -0400220 synchronized (DozeLog.class) {
John Spurlock813552c2014-09-19 08:30:21 -0400221 pw.print(" Doze summary stats (for ");
Beverlycc4a62f2019-09-26 14:55:28 -0400222 TimeUtils.formatDuration(System.currentTimeMillis() - mSince, pw);
John Spurlock813552c2014-09-19 08:30:21 -0400223 pw.println("):");
Beverlycc4a62f2019-09-26 14:55:28 -0400224 mPickupPulseNearVibrationStats.dump(pw, "Pickup pulse (near vibration)");
225 mPickupPulseNotNearVibrationStats.dump(pw, "Pickup pulse (not near vibration)");
226 mNotificationPulseStats.dump(pw, "Notification pulse");
227 mScreenOnPulsingStats.dump(pw, "Screen on (pulsing)");
228 mScreenOnNotPulsingStats.dump(pw, "Screen on (not pulsing)");
229 mEmergencyCallStats.dump(pw, "Emergency call");
Ned Burns30d67702020-01-28 12:58:45 -0500230 for (int i = 0; i < TOTAL_REASONS; i++) {
231 final String reason = reasonToString(i);
Beverlycc4a62f2019-09-26 14:55:28 -0400232 mProxStats[i][0].dump(pw, "Proximity near (" + reason + ")");
233 mProxStats[i][1].dump(pw, "Proximity far (" + reason + ")");
John Spurlockeab28e62014-11-29 11:33:49 -0500234 }
John Spurlock813552c2014-09-19 08:30:21 -0400235 }
236 }
237
Beverlycc4a62f2019-09-26 14:55:28 -0400238 /**
239 * Appends pulse dropped event to logs
240 */
241 public void tracePulseDropped(boolean pulsePending, DozeMachine.State state, boolean blocked) {
Ned Burns30d67702020-01-28 12:58:45 -0500242 mLogger.logPulseDropped(pulsePending, state, blocked);
Lucas Dupinc0b81112019-07-25 18:56:12 -0700243 }
244
Beverlycc4a62f2019-09-26 14:55:28 -0400245 /**
246 * Appends pulse dropped event to logs
247 * @param reason why the pulse was dropped
248 */
249 public void tracePulseDropped(String reason) {
Ned Burns30d67702020-01-28 12:58:45 -0500250 mLogger.logPulseDropped(reason);
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700251 }
252
Beverlycc4a62f2019-09-26 14:55:28 -0400253 /**
254 * Appends pulse touch displayed by prox sensor event to logs
255 * @param disabled
256 */
257 public void tracePulseTouchDisabledByProx(boolean disabled) {
Ned Burns30d67702020-01-28 12:58:45 -0500258 mLogger.logPulseTouchDisabledByProx(disabled);
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700259 }
260
Beverlycc4a62f2019-09-26 14:55:28 -0400261 /**
262 * Appends sensor triggered event to logs
263 * @param reason why the sensor was triggered
264 */
Ned Burns30d67702020-01-28 12:58:45 -0500265 public void traceSensor(@Reason int reason) {
266 mLogger.logSensorTriggered(reason);
Adrian Roos2ca9cd72017-07-03 15:14:37 +0200267 }
268
Beverlycc4a62f2019-09-26 14:55:28 -0400269 private class SummaryStats {
John Spurlock813552c2014-09-19 08:30:21 -0400270 private int mCount;
271
272 public void append() {
273 mCount++;
274 }
275
276 public void dump(PrintWriter pw, String type) {
John Spurlockeab28e62014-11-29 11:33:49 -0500277 if (mCount == 0) return;
John Spurlock813552c2014-09-19 08:30:21 -0400278 pw.print(" ");
279 pw.print(type);
280 pw.print(": n=");
281 pw.print(mCount);
282 pw.print(" (");
Beverlycc4a62f2019-09-26 14:55:28 -0400283 final double perHr = (double) mCount / (System.currentTimeMillis() - mSince)
John Spurlock813552c2014-09-19 08:30:21 -0400284 * 1000 * 60 * 60;
285 pw.print(perHr);
286 pw.print("/hr)");
287 pw.println();
288 }
289 }
John Spurlockbee3b0a2014-09-29 11:30:12 -0400290
Beverlycc4a62f2019-09-26 14:55:28 -0400291 private final KeyguardUpdateMonitorCallback mKeyguardCallback =
John Spurlockbee3b0a2014-09-29 11:30:12 -0400292 new KeyguardUpdateMonitorCallback() {
293 @Override
294 public void onEmergencyCallAction() {
295 traceEmergencyCall();
296 }
297
298 @Override
299 public void onKeyguardBouncerChanged(boolean bouncer) {
300 traceKeyguardBouncerChanged(bouncer);
301 }
302
303 @Override
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700304 public void onStartedWakingUp() {
John Spurlockbee3b0a2014-09-29 11:30:12 -0400305 traceScreenOn();
306 }
307
308 @Override
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700309 public void onFinishedGoingToSleep(int why) {
John Spurlockbee3b0a2014-09-29 11:30:12 -0400310 traceScreenOff(why);
311 }
312
313 @Override
314 public void onKeyguardVisibilityChanged(boolean showing) {
315 traceKeyguard(showing);
316 }
317 };
Beverlycc4a62f2019-09-26 14:55:28 -0400318
Ned Burns30d67702020-01-28 12:58:45 -0500319 /**
320 * Converts the reason (integer) to a user-readable string
321 */
322 public static String reasonToString(@Reason int pulseReason) {
323 switch (pulseReason) {
324 case PULSE_REASON_INTENT: return "intent";
325 case PULSE_REASON_NOTIFICATION: return "notification";
326 case PULSE_REASON_SENSOR_SIGMOTION: return "sigmotion";
327 case REASON_SENSOR_PICKUP: return "pickup";
328 case REASON_SENSOR_DOUBLE_TAP: return "doubletap";
329 case PULSE_REASON_SENSOR_LONG_PRESS: return "longpress";
330 case PULSE_REASON_DOCKING: return "docking";
331 case PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN: return "wakelockscreen";
332 case REASON_SENSOR_WAKE_UP: return "wakeup";
333 case REASON_SENSOR_TAP: return "tap";
334 default: throw new IllegalArgumentException("invalid reason: " + pulseReason);
335 }
336 }
337
338 @Retention(RetentionPolicy.SOURCE)
339 @IntDef({PULSE_REASON_NONE, PULSE_REASON_INTENT, PULSE_REASON_NOTIFICATION,
340 PULSE_REASON_SENSOR_SIGMOTION, REASON_SENSOR_PICKUP, REASON_SENSOR_DOUBLE_TAP,
341 PULSE_REASON_SENSOR_LONG_PRESS, PULSE_REASON_DOCKING, REASON_SENSOR_WAKE_UP,
342 PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN, REASON_SENSOR_TAP})
343 public @interface Reason {}
344 public static final int PULSE_REASON_NONE = -1;
345 public static final int PULSE_REASON_INTENT = 0;
346 public static final int PULSE_REASON_NOTIFICATION = 1;
347 public static final int PULSE_REASON_SENSOR_SIGMOTION = 2;
348 public static final int REASON_SENSOR_PICKUP = 3;
349 public static final int REASON_SENSOR_DOUBLE_TAP = 4;
350 public static final int PULSE_REASON_SENSOR_LONG_PRESS = 5;
351 public static final int PULSE_REASON_DOCKING = 6;
352 public static final int REASON_SENSOR_WAKE_UP = 7;
353 public static final int PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN = 8;
354 public static final int REASON_SENSOR_TAP = 9;
355
356 public static final int TOTAL_REASONS = 10;
John Spurlock813552c2014-09-19 08:30:21 -0400357}