blob: 6a5501445a4d7775d9b11016b9caa9ebc38bd612 [file] [log] [blame]
Adrian Roosff2c4562016-11-03 12:13:36 -07001/*
2 * Copyright (C) 2016 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
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -070019import android.annotation.Nullable;
Adrian Roos6023ccb2017-06-28 16:22:02 +020020import android.app.AlarmManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070021import android.app.UiModeManager;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.res.Configuration;
Issei Suzukica19e6e2019-02-26 12:39:11 +010027import android.hardware.display.AmbientDisplayConfiguration;
Steven Wude353052019-03-12 13:49:23 -040028import android.metrics.LogMaker;
Adrian Roosff2c4562016-11-03 12:13:36 -070029import android.os.SystemClock;
30import android.os.UserHandle;
31import android.text.format.Formatter;
32import android.util.Log;
33
lpeter8a5f4702019-01-18 16:53:07 +080034import com.android.internal.annotations.VisibleForTesting;
Steven Wude353052019-03-12 13:49:23 -040035import com.android.internal.logging.MetricsLogger;
thiruram70bc5832020-04-29 15:57:45 -070036import com.android.internal.logging.UiEvent;
37import com.android.internal.logging.UiEventLogger;
38import com.android.internal.logging.UiEventLoggerImpl;
Steven Wude353052019-03-12 13:49:23 -040039import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steven Wude353052019-03-12 13:49:23 -040040import com.android.systemui.Dependency;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000041import com.android.systemui.broadcast.BroadcastDispatcher;
lpeter8a5f4702019-01-18 16:53:07 +080042import com.android.systemui.dock.DockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070043import com.android.systemui.statusbar.phone.DozeParameters;
44import com.android.systemui.util.Assert;
Dave Mankoffdddcae92020-05-13 12:46:18 -040045import com.android.systemui.util.concurrency.DelayableExecutor;
Dave Mankoff63a12822019-09-16 14:38:06 -040046import com.android.systemui.util.sensors.AsyncSensorManager;
47import com.android.systemui.util.sensors.ProximitySensor;
Adrian Roosc1b50322017-02-27 21:07:58 +010048import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070049
50import java.io.PrintWriter;
thiruram70bc5832020-04-29 15:57:45 -070051import java.util.Optional;
Dave Mankoff63a12822019-09-16 14:38:06 -040052import java.util.function.Consumer;
Adrian Roosff2c4562016-11-03 12:13:36 -070053
54/**
55 * Handles triggers for ambient state changes.
56 */
57public class DozeTriggers implements DozeMachine.Part {
58
59 private static final String TAG = "DozeTriggers";
Adrian Roos67cca742017-04-13 16:52:51 -070060 private static final boolean DEBUG = DozeService.DEBUG;
Adrian Roosff2c4562016-11-03 12:13:36 -070061
62 /** adb shell am broadcast -a com.android.systemui.doze.pulse com.android.systemui */
63 private static final String PULSE_ACTION = "com.android.systemui.doze.pulse";
64
thiruram70bc5832020-04-29 15:57:45 -070065 private static final UiEventLogger UI_EVENT_LOGGER = new UiEventLoggerImpl();
66
Lucas Dupin1ae6cf92018-12-14 18:06:38 -080067 /**
68 * Last value sent by the wake-display sensor.
69 * Assuming that the screen should start on.
70 */
71 private static boolean sWakeDisplaySensorState = true;
72
Dave Mankoff63a12822019-09-16 14:38:06 -040073 private static final int PROXIMITY_TIMEOUT_DELAY_MS = 500;
74
Adrian Roosff2c4562016-11-03 12:13:36 -070075 private final Context mContext;
76 private final DozeMachine mMachine;
Beverlycc4a62f2019-09-26 14:55:28 -040077 private final DozeLog mDozeLog;
Adrian Roosff2c4562016-11-03 12:13:36 -070078 private final DozeSensors mDozeSensors;
79 private final DozeHost mDozeHost;
80 private final AmbientDisplayConfiguration mConfig;
81 private final DozeParameters mDozeParameters;
Dave Mankoff63a12822019-09-16 14:38:06 -040082 private final AsyncSensorManager mSensorManager;
Adrian Roosc1b50322017-02-27 21:07:58 +010083 private final WakeLock mWakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -080084 private final boolean mAllowPulseTriggers;
Adrian Roosff2c4562016-11-03 12:13:36 -070085 private final UiModeManager mUiModeManager;
86 private final TriggerReceiver mBroadcastReceiver = new TriggerReceiver();
lpeter8a5f4702019-01-18 16:53:07 +080087 private final DockEventListener mDockEventListener = new DockEventListener();
88 private final DockManager mDockManager;
Dave Mankoff63a12822019-09-16 14:38:06 -040089 private final ProximitySensor.ProximityCheck mProxCheck;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000090 private final BroadcastDispatcher mBroadcastDispatcher;
Adrian Roosff2c4562016-11-03 12:13:36 -070091
92 private long mNotificationPulseTime;
93 private boolean mPulsePending;
94
Steven Wude353052019-03-12 13:49:23 -040095 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070096
thiruram70bc5832020-04-29 15:57:45 -070097 @VisibleForTesting
98 public enum DozingUpdateUiEvent implements UiEventLogger.UiEventEnum {
99 @UiEvent(doc = "Dozing updated due to notification.")
100 DOZING_UPDATE_NOTIFICATION(433),
101
102 @UiEvent(doc = "Dozing updated due to sigmotion.")
103 DOZING_UPDATE_SIGMOTION(434),
104
105 @UiEvent(doc = "Dozing updated because sensor was picked up.")
106 DOZING_UPDATE_SENSOR_PICKUP(435),
107
108 @UiEvent(doc = "Dozing updated because sensor was double tapped.")
109 DOZING_UPDATE_SENSOR_DOUBLE_TAP(436),
110
111 @UiEvent(doc = "Dozing updated because sensor was long squeezed.")
112 DOZING_UPDATE_SENSOR_LONG_SQUEEZE(437),
113
114 @UiEvent(doc = "Dozing updated due to docking.")
115 DOZING_UPDATE_DOCKING(438),
116
117 @UiEvent(doc = "Dozing updated because sensor woke up.")
118 DOZING_UPDATE_SENSOR_WAKEUP(439),
119
120 @UiEvent(doc = "Dozing updated because sensor woke up the lockscreen.")
121 DOZING_UPDATE_SENSOR_WAKE_LOCKSCREEN(440),
122
123 @UiEvent(doc = "Dozing updated because sensor was tapped.")
124 DOZING_UPDATE_SENSOR_TAP(441);
125
126 private final int mId;
127
128 DozingUpdateUiEvent(int id) {
129 mId = id;
130 }
131
132 @Override
133 public int getId() {
134 return mId;
135 }
136
137 static DozingUpdateUiEvent fromReason(int reason) {
138 switch (reason) {
139 case 1: return DOZING_UPDATE_NOTIFICATION;
140 case 2: return DOZING_UPDATE_SIGMOTION;
141 case 3: return DOZING_UPDATE_SENSOR_PICKUP;
142 case 4: return DOZING_UPDATE_SENSOR_DOUBLE_TAP;
143 case 5: return DOZING_UPDATE_SENSOR_LONG_SQUEEZE;
144 case 6: return DOZING_UPDATE_DOCKING;
145 case 7: return DOZING_UPDATE_SENSOR_WAKEUP;
146 case 8: return DOZING_UPDATE_SENSOR_WAKE_LOCKSCREEN;
147 case 9: return DOZING_UPDATE_SENSOR_TAP;
148 default: return null;
149 }
150 }
151 }
152
Adrian Roosff2c4562016-11-03 12:13:36 -0700153 public DozeTriggers(Context context, DozeMachine machine, DozeHost dozeHost,
Adrian Roos6023ccb2017-06-28 16:22:02 +0200154 AlarmManager alarmManager, AmbientDisplayConfiguration config,
Dave Mankoffdddcae92020-05-13 12:46:18 -0400155 DozeParameters dozeParameters, AsyncSensorManager sensorManager,
156 DelayableExecutor delayableExecutor, WakeLock wakeLock, boolean allowPulseTriggers,
157 DockManager dockManager, ProximitySensor proximitySensor,
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000158 DozeLog dozeLog, BroadcastDispatcher broadcastDispatcher) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700159 mContext = context;
160 mMachine = machine;
161 mDozeHost = dozeHost;
162 mConfig = config;
163 mDozeParameters = dozeParameters;
164 mSensorManager = sensorManager;
Adrian Roosff2c4562016-11-03 12:13:36 -0700165 mWakeLock = wakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -0800166 mAllowPulseTriggers = allowPulseTriggers;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200167 mDozeSensors = new DozeSensors(context, alarmManager, mSensorManager, dozeParameters,
Dave Mankoffdddcae92020-05-13 12:46:18 -0400168 config, wakeLock, this::onSensor, this::onProximityFar, dozeLog, proximitySensor);
Adrian Roosff2c4562016-11-03 12:13:36 -0700169 mUiModeManager = mContext.getSystemService(UiModeManager.class);
lpeter8a5f4702019-01-18 16:53:07 +0800170 mDockManager = dockManager;
Dave Mankoffdddcae92020-05-13 12:46:18 -0400171 mProxCheck = new ProximitySensor.ProximityCheck(proximitySensor, delayableExecutor);
Beverlycc4a62f2019-09-26 14:55:28 -0400172 mDozeLog = dozeLog;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000173 mBroadcastDispatcher = broadcastDispatcher;
Adrian Roosff2c4562016-11-03 12:13:36 -0700174 }
175
Dave Mankoff52229d82020-04-27 16:55:50 -0400176 @Override
177 public void destroy() {
178 mDozeSensors.destroy();
179 }
180
Selim Cinek65c96f22019-07-25 20:09:04 -0700181 private void onNotification(Runnable onPulseSuppressedListener) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400182 if (DozeMachine.DEBUG) {
183 Log.d(TAG, "requestNotificationPulse");
184 }
185 if (!sWakeDisplaySensorState) {
186 Log.d(TAG, "Wake display false. Pulse denied.");
Selim Cinek65c96f22019-07-25 20:09:04 -0700187 runIfNotNull(onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400188 mDozeLog.tracePulseDropped("wakeDisplaySensor");
Dave Mankoff15bbec62019-06-27 15:17:42 -0400189 return;
190 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700191 mNotificationPulseTime = SystemClock.elapsedRealtime();
Dave Mankoff15bbec62019-06-27 15:17:42 -0400192 if (!mConfig.pulseOnNotificationEnabled(UserHandle.USER_CURRENT)) {
Selim Cinek65c96f22019-07-25 20:09:04 -0700193 runIfNotNull(onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400194 mDozeLog.tracePulseDropped("pulseOnNotificationsDisabled");
Dave Mankoff15bbec62019-06-27 15:17:42 -0400195 return;
196 }
Yogisha Dixit65f26b72020-02-07 17:30:49 +0000197 if (mDozeHost.isDozeSuppressed()) {
Yogisha Dixitb6594de2020-01-15 19:30:23 +0000198 runIfNotNull(onPulseSuppressedListener);
199 mDozeLog.tracePulseDropped("dozeSuppressed");
200 return;
201 }
Ned Burns30d67702020-01-28 12:58:45 -0500202 requestPulse(DozeLog.PULSE_REASON_NOTIFICATION, false /* performedProxCheck */,
Selim Cinek65c96f22019-07-25 20:09:04 -0700203 onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400204 mDozeLog.traceNotificationPulse();
Adrian Roosff2c4562016-11-03 12:13:36 -0700205 }
206
Selim Cinek65c96f22019-07-25 20:09:04 -0700207 private static void runIfNotNull(Runnable runnable) {
208 if (runnable != null) {
209 runnable.run();
210 }
211 }
212
Dave Mankoff63a12822019-09-16 14:38:06 -0400213 private void proximityCheckThenCall(Consumer<Boolean> callback,
Adrian Roosd32b3662017-06-27 14:48:50 +0200214 boolean alreadyPerformedProxCheck,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700215 int reason) {
Dave Mankoff63a12822019-09-16 14:38:06 -0400216 Boolean cachedProxNear = mDozeSensors.isProximityCurrentlyNear();
Adrian Roosd32b3662017-06-27 14:48:50 +0200217 if (alreadyPerformedProxCheck) {
Dave Mankoff63a12822019-09-16 14:38:06 -0400218 callback.accept(null);
219 } else if (cachedProxNear != null) {
220 callback.accept(cachedProxNear);
Adrian Roosd32b3662017-06-27 14:48:50 +0200221 } else {
222 final long start = SystemClock.uptimeMillis();
Dave Mankoff63a12822019-09-16 14:38:06 -0400223 mProxCheck.check(PROXIMITY_TIMEOUT_DELAY_MS, near -> {
224 final long end = SystemClock.uptimeMillis();
Beverlycc4a62f2019-09-26 14:55:28 -0400225 mDozeLog.traceProximityResult(
Dave Mankoff63a12822019-09-16 14:38:06 -0400226 near == null ? false : near,
227 end - start,
228 reason);
229 callback.accept(near);
230 mWakeLock.release(TAG);
231 });
232 mWakeLock.acquire(TAG);
Adrian Roosd32b3662017-06-27 14:48:50 +0200233 }
234 }
235
lpeter8a5f4702019-01-18 16:53:07 +0800236 @VisibleForTesting
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700237 void onSensor(int pulseReason, float screenX, float screenY, float[] rawValues) {
Ned Burns30d67702020-01-28 12:58:45 -0500238 boolean isDoubleTap = pulseReason == DozeLog.REASON_SENSOR_DOUBLE_TAP;
239 boolean isTap = pulseReason == DozeLog.REASON_SENSOR_TAP;
240 boolean isPickup = pulseReason == DozeLog.REASON_SENSOR_PICKUP;
241 boolean isLongPress = pulseReason == DozeLog.PULSE_REASON_SENSOR_LONG_PRESS;
242 boolean isWakeDisplay = pulseReason == DozeLog.REASON_SENSOR_WAKE_UP;
243 boolean isWakeLockScreen = pulseReason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800244 boolean wakeEvent = rawValues != null && rawValues.length > 0 && rawValues[0] != 0;
Adrian Roosff2c4562016-11-03 12:13:36 -0700245
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800246 if (isWakeDisplay) {
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700247 onWakeScreen(wakeEvent, mMachine.isExecutingTransition() ? null : mMachine.getState());
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800248 } else if (isLongPress) {
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700249 requestPulse(pulseReason, true /* alreadyPerformedProxCheck */,
250 null /* onPulseSupressedListener */);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800251 } else if (isWakeLockScreen) {
252 if (wakeEvent) {
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700253 requestPulse(pulseReason, true /* alreadyPerformedProxCheck */,
Selim Cinek65c96f22019-07-25 20:09:04 -0700254 null /* onPulseSupressedListener */);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800255 }
Lucas Dupin4359b552018-08-09 15:07:54 -0700256 } else {
Adrian Roosd32b3662017-06-27 14:48:50 +0200257 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400258 if (result != null && result) {
Adrian Roosd32b3662017-06-27 14:48:50 +0200259 // In pocket, drop event.
260 return;
261 }
Lucas Dupind43bf702019-01-15 13:40:42 -0800262 if (isDoubleTap || isTap) {
263 if (screenX != -1 && screenY != -1) {
264 mDozeHost.onSlpiTap(screenX, screenY);
265 }
Lucas Dupined5b7a92019-03-20 11:00:27 -0700266 gentleWakeUp(pulseReason);
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800267 } else if (isPickup) {
Lucas Dupined5b7a92019-03-20 11:00:27 -0700268 gentleWakeUp(pulseReason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200269 } else {
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700270 mDozeHost.extendPulse(pulseReason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200271 }
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700272 }, true /* alreadyPerformedProxCheck */, pulseReason);
Adrian Roosed85e582017-04-27 15:09:28 -0700273 }
274
275 if (isPickup) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700276 final long timeSinceNotification =
277 SystemClock.elapsedRealtime() - mNotificationPulseTime;
278 final boolean withinVibrationThreshold =
279 timeSinceNotification < mDozeParameters.getPickupVibrationThreshold();
Beverlycc4a62f2019-09-26 14:55:28 -0400280 mDozeLog.tracePickupWakeUp(withinVibrationThreshold);
Adrian Roosff2c4562016-11-03 12:13:36 -0700281 }
282 }
283
Lucas Dupined5b7a92019-03-20 11:00:27 -0700284 private void gentleWakeUp(int reason) {
285 // Log screen wake up reason (lift/pickup, tap, double-tap)
286 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
287 .setType(MetricsEvent.TYPE_UPDATE)
288 .setSubtype(reason));
thiruram70bc5832020-04-29 15:57:45 -0700289 Optional.ofNullable(DozingUpdateUiEvent.fromReason(reason))
290 .ifPresent(UI_EVENT_LOGGER::log);
Lucas Dupined5b7a92019-03-20 11:00:27 -0700291 if (mDozeParameters.getDisplayNeedsBlanking()) {
292 // Let's prepare the display to wake-up by drawing black.
293 // This will cover the hardware wake-up sequence, where the display
294 // becomes black for a few frames.
Lucas Dupin34306c32019-07-16 11:56:53 -0700295 mDozeHost.setAodDimmingScrim(1f);
Lucas Dupined5b7a92019-03-20 11:00:27 -0700296 }
297 mMachine.wakeUp();
298 }
299
Adrian Roos67cca742017-04-13 16:52:51 -0700300 private void onProximityFar(boolean far) {
Lucas Dupin1946b312019-03-21 14:48:23 -0700301 // Proximity checks are asynchronous and the user might have interacted with the phone
302 // when a new event is arriving. This means that a state transition might have happened
303 // and the proximity check is now obsolete.
304 if (mMachine.isExecutingTransition()) {
305 Log.w(TAG, "onProximityFar called during transition. Ignoring sensor response.");
306 return;
307 }
308
Adrian Roos67cca742017-04-13 16:52:51 -0700309 final boolean near = !far;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200310 final DozeMachine.State state = mMachine.getState();
Adrian Roosc7fd6962017-09-06 16:46:46 +0200311 final boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
312 final boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
313 final boolean aod = (state == DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200314
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700315 if (state == DozeMachine.State.DOZE_PULSING
316 || state == DozeMachine.State.DOZE_PULSING_BRIGHT) {
Adrian Roosa6c03f82017-07-26 16:20:30 +0200317 boolean ignoreTouch = near;
Dave Mankoff15bbec62019-06-27 15:17:42 -0400318 if (DEBUG) {
319 Log.i(TAG, "Prox changed, ignore touch = " + ignoreTouch);
320 }
Adrian Roosa6c03f82017-07-26 16:20:30 +0200321 mDozeHost.onIgnoreTouchWhilePulsing(ignoreTouch);
Adrian Roos67cca742017-04-13 16:52:51 -0700322 }
Lucas Dupin65104382018-12-04 11:53:42 -0800323
Adrian Roosc7fd6962017-09-06 16:46:46 +0200324 if (far && (paused || pausing)) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400325 if (DEBUG) {
326 Log.i(TAG, "Prox FAR, unpausing AOD");
327 }
Adrian Roos67cca742017-04-13 16:52:51 -0700328 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200329 } else if (near && aod) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400330 if (DEBUG) {
331 Log.i(TAG, "Prox NEAR, pausing AOD");
332 }
Adrian Roos6023ccb2017-06-28 16:22:02 +0200333 mMachine.requestState(DozeMachine.State.DOZE_AOD_PAUSING);
Adrian Roos67cca742017-04-13 16:52:51 -0700334 }
335 }
336
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700337 /**
338 * When a wake screen event is received from a sensor
339 * @param wake {@code true} when it's time to wake up, {@code false} when we should sleep.
340 * @param state The current state, or null if the state could not be determined due to enqueued
341 * transitions.
342 */
343 private void onWakeScreen(boolean wake, @Nullable DozeMachine.State state) {
Beverlycc4a62f2019-09-26 14:55:28 -0400344 mDozeLog.traceWakeDisplay(wake);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800345 sWakeDisplaySensorState = wake;
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700346
347 if (wake) {
348 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400349 if (result != null && result) {
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700350 // In pocket, drop event.
351 return;
352 }
Lucas Dupin5131f512019-02-01 12:57:19 -0800353 if (state == DozeMachine.State.DOZE) {
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700354 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Steven Wucef2d5d2019-04-23 13:27:33 -0400355 // Logs AOD open due to sensor wake up.
356 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
357 .setType(MetricsEvent.TYPE_OPEN)
Ned Burns30d67702020-01-28 12:58:45 -0500358 .setSubtype(DozeLog.REASON_SENSOR_WAKE_UP));
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700359 }
Ned Burns30d67702020-01-28 12:58:45 -0500360 }, true /* alreadyPerformedProxCheck */, DozeLog.REASON_SENSOR_WAKE_UP);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700361 } else {
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700362 boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
363 boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700364 if (!pausing && !paused) {
Lucas Dupin65104382018-12-04 11:53:42 -0800365 mMachine.requestState(DozeMachine.State.DOZE);
Steven Wucef2d5d2019-04-23 13:27:33 -0400366 // Logs AOD close due to sensor wake up.
367 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
368 .setType(MetricsEvent.TYPE_CLOSE)
Ned Burns30d67702020-01-28 12:58:45 -0500369 .setSubtype(DozeLog.REASON_SENSOR_WAKE_UP));
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700370 }
371 }
372 }
373
Adrian Roosff2c4562016-11-03 12:13:36 -0700374 @Override
375 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
376 switch (newState) {
377 case INITIALIZED:
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000378 mBroadcastReceiver.register(mBroadcastDispatcher);
Adrian Roosff2c4562016-11-03 12:13:36 -0700379 mDozeHost.addCallback(mHostCallback);
Jerry Changae4dc4b2019-10-16 18:45:03 +0800380 mDockManager.addListener(mDockEventListener);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800381 mDozeSensors.requestTemporaryDisable();
Adrian Roosff2c4562016-11-03 12:13:36 -0700382 checkTriggersAtInit();
383 break;
384 case DOZE:
385 case DOZE_AOD:
Adrian Roos67cca742017-04-13 16:52:51 -0700386 mDozeSensors.setProxListening(newState != DozeMachine.State.DOZE);
Adrian Roose3ac6f82017-06-30 16:15:22 +0200387 mDozeSensors.setListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700388 mDozeSensors.setPaused(false);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800389 if (newState == DozeMachine.State.DOZE_AOD && !sWakeDisplaySensorState) {
Lucas Dupin5131f512019-02-01 12:57:19 -0800390 onWakeScreen(false, newState);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800391 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700392 break;
Adrian Roos5f7bee42017-06-27 18:49:23 +0200393 case DOZE_AOD_PAUSED:
Adrian Roos6023ccb2017-06-28 16:22:02 +0200394 case DOZE_AOD_PAUSING:
Adrian Roos5f7bee42017-06-27 18:49:23 +0200395 mDozeSensors.setProxListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700396 mDozeSensors.setPaused(true);
Adrian Roos5f7bee42017-06-27 18:49:23 +0200397 break;
Adrian Roos67cca742017-04-13 16:52:51 -0700398 case DOZE_PULSING:
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700399 case DOZE_PULSING_BRIGHT:
Jerry Changae4dc4b2019-10-16 18:45:03 +0800400 case DOZE_AOD_DOCKED:
Adrian Roos98d31982017-08-02 20:50:16 +0200401 mDozeSensors.setTouchscreenSensorsListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700402 mDozeSensors.setProxListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700403 mDozeSensors.setPaused(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700404 break;
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800405 case DOZE_PULSE_DONE:
406 mDozeSensors.requestTemporaryDisable();
Lucas Dupin3174c662019-07-15 15:49:54 -0700407 // A pulse will temporarily disable sensors that require a touch screen.
408 // Let's make sure that they are re-enabled when the pulse is over.
409 mDozeSensors.updateListening();
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800410 break;
Adrian Roosff2c4562016-11-03 12:13:36 -0700411 case FINISH:
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000412 mBroadcastReceiver.unregister(mBroadcastDispatcher);
Adrian Roosff2c4562016-11-03 12:13:36 -0700413 mDozeHost.removeCallback(mHostCallback);
Jerry Changae4dc4b2019-10-16 18:45:03 +0800414 mDockManager.removeListener(mDockEventListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700415 mDozeSensors.setListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700416 mDozeSensors.setProxListening(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700417 break;
418 default:
419 }
420 }
421
422 private void checkTriggersAtInit() {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200423 if (mUiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR
Adrian Roos710a0b12017-07-07 19:02:34 +0200424 || mDozeHost.isBlockingDoze()
Adrian Roosf2d545e2017-07-05 16:45:42 +0200425 || !mDozeHost.isProvisioned()) {
426 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700427 }
428 }
429
Selim Cinek65c96f22019-07-25 20:09:04 -0700430 private void requestPulse(final int reason, boolean performedProxCheck,
431 Runnable onPulseSuppressedListener) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700432 Assert.isMainThread();
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700433 mDozeHost.extendPulse(reason);
434
435 // When already pulsing we're allowed to show the wallpaper directly without
436 // requesting a new pulse.
437 if (mMachine.getState() == DozeMachine.State.DOZE_PULSING
Ned Burns30d67702020-01-28 12:58:45 -0500438 && reason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN) {
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700439 mMachine.requestState(DozeMachine.State.DOZE_PULSING_BRIGHT);
440 return;
441 }
442
Adrian Roosf9d13f62016-11-08 15:42:20 -0800443 if (mPulsePending || !mAllowPulseTriggers || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700444 if (mAllowPulseTriggers) {
Beverlycc4a62f2019-09-26 14:55:28 -0400445 mDozeLog.tracePulseDropped(mPulsePending, mMachine.getState(),
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700446 mDozeHost.isPulsingBlocked());
447 }
Selim Cinek65c96f22019-07-25 20:09:04 -0700448 runIfNotNull(onPulseSuppressedListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700449 return;
450 }
451
452 mPulsePending = true;
Adrian Roosd32b3662017-06-27 14:48:50 +0200453 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400454 if (result != null && result) {
Adrian Roosd32b3662017-06-27 14:48:50 +0200455 // in pocket, abort pulse
Beverlycc4a62f2019-09-26 14:55:28 -0400456 mDozeLog.tracePulseDropped("inPocket");
Adrian Roosd32b3662017-06-27 14:48:50 +0200457 mPulsePending = false;
Selim Cinek65c96f22019-07-25 20:09:04 -0700458 runIfNotNull(onPulseSuppressedListener);
Adrian Roosd32b3662017-06-27 14:48:50 +0200459 } else {
460 // not in pocket, continue pulsing
Adrian Roosff2c4562016-11-03 12:13:36 -0700461 continuePulseRequest(reason);
462 }
Adrian Roosd32b3662017-06-27 14:48:50 +0200463 }, !mDozeParameters.getProxCheckBeforePulse() || performedProxCheck, reason);
Steven Wude353052019-03-12 13:49:23 -0400464
465 // Logs request pulse reason on AOD screen.
466 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
467 .setType(MetricsEvent.TYPE_UPDATE).setSubtype(reason));
thiruram70bc5832020-04-29 15:57:45 -0700468 Optional.ofNullable(DozingUpdateUiEvent.fromReason(reason))
469 .ifPresent(UI_EVENT_LOGGER::log);
Adrian Roosff2c4562016-11-03 12:13:36 -0700470 }
471
472 private boolean canPulse() {
473 return mMachine.getState() == DozeMachine.State.DOZE
Jerry Changae4dc4b2019-10-16 18:45:03 +0800474 || mMachine.getState() == DozeMachine.State.DOZE_AOD
475 || mMachine.getState() == DozeMachine.State.DOZE_AOD_DOCKED;
Adrian Roosff2c4562016-11-03 12:13:36 -0700476 }
477
478 private void continuePulseRequest(int reason) {
479 mPulsePending = false;
480 if (mDozeHost.isPulsingBlocked() || !canPulse()) {
Beverlycc4a62f2019-09-26 14:55:28 -0400481 mDozeLog.tracePulseDropped(mPulsePending, mMachine.getState(),
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700482 mDozeHost.isPulsingBlocked());
Adrian Roosff2c4562016-11-03 12:13:36 -0700483 return;
484 }
Adrian Roosd7b9d102017-04-28 15:42:58 -0700485 mMachine.requestPulse(reason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700486 }
487
488 @Override
489 public void dump(PrintWriter pw) {
490 pw.print(" notificationPulseTime=");
491 pw.println(Formatter.formatShortElapsedTime(mContext, mNotificationPulseTime));
492
Dave Mankoff63a12822019-09-16 14:38:06 -0400493 pw.println(" pulsePending=" + mPulsePending);
Adrian Roosff2c4562016-11-03 12:13:36 -0700494 pw.println("DozeSensors:");
495 mDozeSensors.dump(pw);
496 }
497
Adrian Roosff2c4562016-11-03 12:13:36 -0700498 private class TriggerReceiver extends BroadcastReceiver {
Adrian Roos67cca742017-04-13 16:52:51 -0700499 private boolean mRegistered;
500
Adrian Roosff2c4562016-11-03 12:13:36 -0700501 @Override
502 public void onReceive(Context context, Intent intent) {
503 if (PULSE_ACTION.equals(intent.getAction())) {
504 if (DozeMachine.DEBUG) Log.d(TAG, "Received pulse intent");
Ned Burns30d67702020-01-28 12:58:45 -0500505 requestPulse(DozeLog.PULSE_REASON_INTENT, false, /* performedProxCheck */
Selim Cinek65c96f22019-07-25 20:09:04 -0700506 null /* onPulseSupressedListener */);
Adrian Roosff2c4562016-11-03 12:13:36 -0700507 }
508 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200509 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700510 }
511 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
512 mDozeSensors.onUserSwitched();
513 }
514 }
515
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000516 public void register(BroadcastDispatcher broadcastDispatcher) {
Adrian Roos67cca742017-04-13 16:52:51 -0700517 if (mRegistered) {
518 return;
519 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700520 IntentFilter filter = new IntentFilter(PULSE_ACTION);
521 filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
522 filter.addAction(Intent.ACTION_USER_SWITCHED);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000523 broadcastDispatcher.registerReceiver(this, filter);
Adrian Roos67cca742017-04-13 16:52:51 -0700524 mRegistered = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700525 }
526
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000527 public void unregister(BroadcastDispatcher broadcastDispatcher) {
Adrian Roos67cca742017-04-13 16:52:51 -0700528 if (!mRegistered) {
529 return;
530 }
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000531 broadcastDispatcher.unregisterReceiver(this);
Adrian Roos67cca742017-04-13 16:52:51 -0700532 mRegistered = false;
Adrian Roosff2c4562016-11-03 12:13:36 -0700533 }
534 }
535
lpeter8a5f4702019-01-18 16:53:07 +0800536 private class DockEventListener implements DockManager.DockEventListener {
537 @Override
538 public void onEvent(int event) {
539 if (DEBUG) Log.d(TAG, "dock event = " + event);
540 switch (event) {
541 case DockManager.STATE_DOCKED:
542 case DockManager.STATE_DOCKED_HIDE:
543 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(true);
544 break;
545 case DockManager.STATE_NONE:
546 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(false);
547 break;
548 default:
549 // no-op
550 }
551 }
552 }
553
Adrian Roosff2c4562016-11-03 12:13:36 -0700554 private DozeHost.Callback mHostCallback = new DozeHost.Callback() {
555 @Override
Selim Cinek65c96f22019-07-25 20:09:04 -0700556 public void onNotificationAlerted(Runnable onPulseSuppressedListener) {
557 onNotification(onPulseSuppressedListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700558 }
559
560 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700561 public void onPowerSaveChanged(boolean active) {
Lucas Dupinbd7366d2019-09-25 13:39:21 -0700562 if (mDozeHost.isPowerSaveActive()) {
563 mMachine.requestState(DozeMachine.State.DOZE);
Lucas Dupinabe15d42020-03-24 18:14:35 -0700564 } else if (mMachine.getState() == DozeMachine.State.DOZE
565 && mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) {
566 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Adrian Roosff2c4562016-11-03 12:13:36 -0700567 }
568 }
Yogisha Dixit65f26b72020-02-07 17:30:49 +0000569
570 @Override
571 public void onDozeSuppressedChanged(boolean suppressed) {
572 final DozeMachine.State nextState;
573 if (mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT) && !suppressed) {
574 nextState = DozeMachine.State.DOZE_AOD;
575 } else {
576 nextState = DozeMachine.State.DOZE;
577 }
578 mMachine.requestState(nextState);
579 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700580 };
581}