blob: b212884ebb9885a82b08ebcb9e6f8af7d3502b07 [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.Handler;
30import android.os.SystemClock;
31import android.os.UserHandle;
32import android.text.format.Formatter;
33import android.util.Log;
34
lpeter8a5f4702019-01-18 16:53:07 +080035import com.android.internal.annotations.VisibleForTesting;
Steven Wude353052019-03-12 13:49:23 -040036import com.android.internal.logging.MetricsLogger;
37import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steven Wude353052019-03-12 13:49:23 -040038import com.android.systemui.Dependency;
lpeter8a5f4702019-01-18 16:53:07 +080039import com.android.systemui.dock.DockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070040import com.android.systemui.statusbar.phone.DozeParameters;
41import com.android.systemui.util.Assert;
Dave Mankoff63a12822019-09-16 14:38:06 -040042import com.android.systemui.util.sensors.AsyncSensorManager;
43import com.android.systemui.util.sensors.ProximitySensor;
Adrian Roosc1b50322017-02-27 21:07:58 +010044import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070045
46import java.io.PrintWriter;
Dave Mankoff63a12822019-09-16 14:38:06 -040047import java.util.function.Consumer;
Adrian Roosff2c4562016-11-03 12:13:36 -070048
49/**
50 * Handles triggers for ambient state changes.
51 */
52public class DozeTriggers implements DozeMachine.Part {
53
54 private static final String TAG = "DozeTriggers";
Adrian Roos67cca742017-04-13 16:52:51 -070055 private static final boolean DEBUG = DozeService.DEBUG;
Adrian Roosff2c4562016-11-03 12:13:36 -070056
57 /** adb shell am broadcast -a com.android.systemui.doze.pulse com.android.systemui */
58 private static final String PULSE_ACTION = "com.android.systemui.doze.pulse";
59
Lucas Dupin1ae6cf92018-12-14 18:06:38 -080060 /**
61 * Last value sent by the wake-display sensor.
62 * Assuming that the screen should start on.
63 */
64 private static boolean sWakeDisplaySensorState = true;
65
Dave Mankoff63a12822019-09-16 14:38:06 -040066 private static final int PROXIMITY_TIMEOUT_DELAY_MS = 500;
67
Adrian Roosff2c4562016-11-03 12:13:36 -070068 private final Context mContext;
69 private final DozeMachine mMachine;
Beverlycc4a62f2019-09-26 14:55:28 -040070 private final DozeLog mDozeLog;
Adrian Roosff2c4562016-11-03 12:13:36 -070071 private final DozeSensors mDozeSensors;
72 private final DozeHost mDozeHost;
73 private final AmbientDisplayConfiguration mConfig;
74 private final DozeParameters mDozeParameters;
Dave Mankoff63a12822019-09-16 14:38:06 -040075 private final AsyncSensorManager mSensorManager;
Adrian Roosc1b50322017-02-27 21:07:58 +010076 private final WakeLock mWakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -080077 private final boolean mAllowPulseTriggers;
Adrian Roosff2c4562016-11-03 12:13:36 -070078 private final UiModeManager mUiModeManager;
79 private final TriggerReceiver mBroadcastReceiver = new TriggerReceiver();
lpeter8a5f4702019-01-18 16:53:07 +080080 private final DockEventListener mDockEventListener = new DockEventListener();
81 private final DockManager mDockManager;
Dave Mankoff63a12822019-09-16 14:38:06 -040082 private final ProximitySensor.ProximityCheck mProxCheck;
Adrian Roosff2c4562016-11-03 12:13:36 -070083
84 private long mNotificationPulseTime;
85 private boolean mPulsePending;
86
Steven Wude353052019-03-12 13:49:23 -040087 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070088
89 public DozeTriggers(Context context, DozeMachine machine, DozeHost dozeHost,
Adrian Roos6023ccb2017-06-28 16:22:02 +020090 AlarmManager alarmManager, AmbientDisplayConfiguration config,
Dave Mankoff63a12822019-09-16 14:38:06 -040091 DozeParameters dozeParameters, AsyncSensorManager sensorManager, Handler handler,
Dave Mankoffbf52f4b2019-09-20 14:34:28 -040092 WakeLock wakeLock, boolean allowPulseTriggers, DockManager dockManager,
Beverlycc4a62f2019-09-26 14:55:28 -040093 ProximitySensor proximitySensor,
94 DozeLog dozeLog) {
Adrian Roosff2c4562016-11-03 12:13:36 -070095 mContext = context;
96 mMachine = machine;
97 mDozeHost = dozeHost;
98 mConfig = config;
99 mDozeParameters = dozeParameters;
100 mSensorManager = sensorManager;
Adrian Roosff2c4562016-11-03 12:13:36 -0700101 mWakeLock = wakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -0800102 mAllowPulseTriggers = allowPulseTriggers;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200103 mDozeSensors = new DozeSensors(context, alarmManager, mSensorManager, dozeParameters,
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800104 config, wakeLock, this::onSensor, this::onProximityFar,
Beverlycc4a62f2019-09-26 14:55:28 -0400105 dozeParameters.getPolicy(), dozeLog);
Adrian Roosff2c4562016-11-03 12:13:36 -0700106 mUiModeManager = mContext.getSystemService(UiModeManager.class);
lpeter8a5f4702019-01-18 16:53:07 +0800107 mDockManager = dockManager;
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400108 mProxCheck = new ProximitySensor.ProximityCheck(proximitySensor, handler);
Beverlycc4a62f2019-09-26 14:55:28 -0400109 mDozeLog = dozeLog;
Adrian Roosff2c4562016-11-03 12:13:36 -0700110 }
111
Selim Cinek65c96f22019-07-25 20:09:04 -0700112 private void onNotification(Runnable onPulseSuppressedListener) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400113 if (DozeMachine.DEBUG) {
114 Log.d(TAG, "requestNotificationPulse");
115 }
116 if (!sWakeDisplaySensorState) {
117 Log.d(TAG, "Wake display false. Pulse denied.");
Selim Cinek65c96f22019-07-25 20:09:04 -0700118 runIfNotNull(onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400119 mDozeLog.tracePulseDropped("wakeDisplaySensor");
Dave Mankoff15bbec62019-06-27 15:17:42 -0400120 return;
121 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700122 mNotificationPulseTime = SystemClock.elapsedRealtime();
Dave Mankoff15bbec62019-06-27 15:17:42 -0400123 if (!mConfig.pulseOnNotificationEnabled(UserHandle.USER_CURRENT)) {
Selim Cinek65c96f22019-07-25 20:09:04 -0700124 runIfNotNull(onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400125 mDozeLog.tracePulseDropped("pulseOnNotificationsDisabled");
Dave Mankoff15bbec62019-06-27 15:17:42 -0400126 return;
127 }
Beverlycc4a62f2019-09-26 14:55:28 -0400128 requestPulse(DozeEvent.PULSE_REASON_NOTIFICATION, false /* performedProxCheck */,
Selim Cinek65c96f22019-07-25 20:09:04 -0700129 onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400130 mDozeLog.traceNotificationPulse();
Adrian Roosff2c4562016-11-03 12:13:36 -0700131 }
132
Selim Cinek65c96f22019-07-25 20:09:04 -0700133 private static void runIfNotNull(Runnable runnable) {
134 if (runnable != null) {
135 runnable.run();
136 }
137 }
138
Dave Mankoff63a12822019-09-16 14:38:06 -0400139 private void proximityCheckThenCall(Consumer<Boolean> callback,
Adrian Roosd32b3662017-06-27 14:48:50 +0200140 boolean alreadyPerformedProxCheck,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700141 int reason) {
Dave Mankoff63a12822019-09-16 14:38:06 -0400142 Boolean cachedProxNear = mDozeSensors.isProximityCurrentlyNear();
Adrian Roosd32b3662017-06-27 14:48:50 +0200143 if (alreadyPerformedProxCheck) {
Dave Mankoff63a12822019-09-16 14:38:06 -0400144 callback.accept(null);
145 } else if (cachedProxNear != null) {
146 callback.accept(cachedProxNear);
Adrian Roosd32b3662017-06-27 14:48:50 +0200147 } else {
148 final long start = SystemClock.uptimeMillis();
Dave Mankoff63a12822019-09-16 14:38:06 -0400149 mProxCheck.check(PROXIMITY_TIMEOUT_DELAY_MS, near -> {
150 final long end = SystemClock.uptimeMillis();
Beverlycc4a62f2019-09-26 14:55:28 -0400151 mDozeLog.traceProximityResult(
Dave Mankoff63a12822019-09-16 14:38:06 -0400152 near == null ? false : near,
153 end - start,
154 reason);
155 callback.accept(near);
156 mWakeLock.release(TAG);
157 });
158 mWakeLock.acquire(TAG);
Adrian Roosd32b3662017-06-27 14:48:50 +0200159 }
160 }
161
lpeter8a5f4702019-01-18 16:53:07 +0800162 @VisibleForTesting
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700163 void onSensor(int pulseReason, float screenX, float screenY, float[] rawValues) {
Beverlycc4a62f2019-09-26 14:55:28 -0400164 boolean isDoubleTap = pulseReason == DozeEvent.REASON_SENSOR_DOUBLE_TAP;
165 boolean isTap = pulseReason == DozeEvent.REASON_SENSOR_TAP;
166 boolean isPickup = pulseReason == DozeEvent.REASON_SENSOR_PICKUP;
167 boolean isLongPress = pulseReason == DozeEvent.PULSE_REASON_SENSOR_LONG_PRESS;
168 boolean isWakeDisplay = pulseReason == DozeEvent.REASON_SENSOR_WAKE_UP;
169 boolean isWakeLockScreen = pulseReason == DozeEvent.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800170 boolean wakeEvent = rawValues != null && rawValues.length > 0 && rawValues[0] != 0;
Adrian Roosff2c4562016-11-03 12:13:36 -0700171
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800172 if (isWakeDisplay) {
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700173 onWakeScreen(wakeEvent, mMachine.isExecutingTransition() ? null : mMachine.getState());
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800174 } else if (isLongPress) {
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700175 requestPulse(pulseReason, true /* alreadyPerformedProxCheck */,
176 null /* onPulseSupressedListener */);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800177 } else if (isWakeLockScreen) {
178 if (wakeEvent) {
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700179 requestPulse(pulseReason, true /* alreadyPerformedProxCheck */,
Selim Cinek65c96f22019-07-25 20:09:04 -0700180 null /* onPulseSupressedListener */);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800181 }
Lucas Dupin4359b552018-08-09 15:07:54 -0700182 } else {
Adrian Roosd32b3662017-06-27 14:48:50 +0200183 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400184 if (result != null && result) {
Adrian Roosd32b3662017-06-27 14:48:50 +0200185 // In pocket, drop event.
186 return;
187 }
Lucas Dupind43bf702019-01-15 13:40:42 -0800188 if (isDoubleTap || isTap) {
189 if (screenX != -1 && screenY != -1) {
190 mDozeHost.onSlpiTap(screenX, screenY);
191 }
Lucas Dupined5b7a92019-03-20 11:00:27 -0700192 gentleWakeUp(pulseReason);
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800193 } else if (isPickup) {
Lucas Dupined5b7a92019-03-20 11:00:27 -0700194 gentleWakeUp(pulseReason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200195 } else {
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700196 mDozeHost.extendPulse(pulseReason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200197 }
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700198 }, true /* alreadyPerformedProxCheck */, pulseReason);
Adrian Roosed85e582017-04-27 15:09:28 -0700199 }
200
201 if (isPickup) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700202 final long timeSinceNotification =
203 SystemClock.elapsedRealtime() - mNotificationPulseTime;
204 final boolean withinVibrationThreshold =
205 timeSinceNotification < mDozeParameters.getPickupVibrationThreshold();
Beverlycc4a62f2019-09-26 14:55:28 -0400206 mDozeLog.tracePickupWakeUp(withinVibrationThreshold);
Adrian Roosff2c4562016-11-03 12:13:36 -0700207 }
208 }
209
Lucas Dupined5b7a92019-03-20 11:00:27 -0700210 private void gentleWakeUp(int reason) {
211 // Log screen wake up reason (lift/pickup, tap, double-tap)
212 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
213 .setType(MetricsEvent.TYPE_UPDATE)
214 .setSubtype(reason));
215 if (mDozeParameters.getDisplayNeedsBlanking()) {
216 // Let's prepare the display to wake-up by drawing black.
217 // This will cover the hardware wake-up sequence, where the display
218 // becomes black for a few frames.
Lucas Dupin34306c32019-07-16 11:56:53 -0700219 mDozeHost.setAodDimmingScrim(1f);
Lucas Dupined5b7a92019-03-20 11:00:27 -0700220 }
221 mMachine.wakeUp();
222 }
223
Adrian Roos67cca742017-04-13 16:52:51 -0700224 private void onProximityFar(boolean far) {
Lucas Dupin1946b312019-03-21 14:48:23 -0700225 // Proximity checks are asynchronous and the user might have interacted with the phone
226 // when a new event is arriving. This means that a state transition might have happened
227 // and the proximity check is now obsolete.
228 if (mMachine.isExecutingTransition()) {
229 Log.w(TAG, "onProximityFar called during transition. Ignoring sensor response.");
230 return;
231 }
232
Adrian Roos67cca742017-04-13 16:52:51 -0700233 final boolean near = !far;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200234 final DozeMachine.State state = mMachine.getState();
Adrian Roosc7fd6962017-09-06 16:46:46 +0200235 final boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
236 final boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
237 final boolean aod = (state == DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200238
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700239 if (state == DozeMachine.State.DOZE_PULSING
240 || state == DozeMachine.State.DOZE_PULSING_BRIGHT) {
Adrian Roosa6c03f82017-07-26 16:20:30 +0200241 boolean ignoreTouch = near;
Dave Mankoff15bbec62019-06-27 15:17:42 -0400242 if (DEBUG) {
243 Log.i(TAG, "Prox changed, ignore touch = " + ignoreTouch);
244 }
Adrian Roosa6c03f82017-07-26 16:20:30 +0200245 mDozeHost.onIgnoreTouchWhilePulsing(ignoreTouch);
Adrian Roos67cca742017-04-13 16:52:51 -0700246 }
Lucas Dupin65104382018-12-04 11:53:42 -0800247
Adrian Roosc7fd6962017-09-06 16:46:46 +0200248 if (far && (paused || pausing)) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400249 if (DEBUG) {
250 Log.i(TAG, "Prox FAR, unpausing AOD");
251 }
Adrian Roos67cca742017-04-13 16:52:51 -0700252 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200253 } else if (near && aod) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400254 if (DEBUG) {
255 Log.i(TAG, "Prox NEAR, pausing AOD");
256 }
Adrian Roos6023ccb2017-06-28 16:22:02 +0200257 mMachine.requestState(DozeMachine.State.DOZE_AOD_PAUSING);
Adrian Roos67cca742017-04-13 16:52:51 -0700258 }
259 }
260
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700261 /**
262 * When a wake screen event is received from a sensor
263 * @param wake {@code true} when it's time to wake up, {@code false} when we should sleep.
264 * @param state The current state, or null if the state could not be determined due to enqueued
265 * transitions.
266 */
267 private void onWakeScreen(boolean wake, @Nullable DozeMachine.State state) {
Beverlycc4a62f2019-09-26 14:55:28 -0400268 mDozeLog.traceWakeDisplay(wake);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800269 sWakeDisplaySensorState = wake;
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700270
271 if (wake) {
272 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400273 if (result != null && result) {
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700274 // In pocket, drop event.
275 return;
276 }
Lucas Dupin5131f512019-02-01 12:57:19 -0800277 if (state == DozeMachine.State.DOZE) {
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700278 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Steven Wucef2d5d2019-04-23 13:27:33 -0400279 // Logs AOD open due to sensor wake up.
280 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
281 .setType(MetricsEvent.TYPE_OPEN)
Beverlycc4a62f2019-09-26 14:55:28 -0400282 .setSubtype(DozeEvent.REASON_SENSOR_WAKE_UP));
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700283 }
Beverlycc4a62f2019-09-26 14:55:28 -0400284 }, true /* alreadyPerformedProxCheck */, DozeEvent.REASON_SENSOR_WAKE_UP);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700285 } else {
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700286 boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
287 boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700288 if (!pausing && !paused) {
Lucas Dupin65104382018-12-04 11:53:42 -0800289 mMachine.requestState(DozeMachine.State.DOZE);
Steven Wucef2d5d2019-04-23 13:27:33 -0400290 // Logs AOD close due to sensor wake up.
291 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
292 .setType(MetricsEvent.TYPE_CLOSE)
Beverlycc4a62f2019-09-26 14:55:28 -0400293 .setSubtype(DozeEvent.REASON_SENSOR_WAKE_UP));
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700294 }
295 }
296 }
297
Adrian Roosff2c4562016-11-03 12:13:36 -0700298 @Override
299 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
300 switch (newState) {
301 case INITIALIZED:
302 mBroadcastReceiver.register(mContext);
303 mDozeHost.addCallback(mHostCallback);
lpeterac798f22019-02-12 15:15:22 +0800304 if (mDockManager != null) {
305 mDockManager.addListener(mDockEventListener);
306 }
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800307 mDozeSensors.requestTemporaryDisable();
Adrian Roosff2c4562016-11-03 12:13:36 -0700308 checkTriggersAtInit();
309 break;
310 case DOZE:
311 case DOZE_AOD:
Adrian Roos67cca742017-04-13 16:52:51 -0700312 mDozeSensors.setProxListening(newState != DozeMachine.State.DOZE);
Adrian Roose3ac6f82017-06-30 16:15:22 +0200313 mDozeSensors.setListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700314 mDozeSensors.setPaused(false);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800315 if (newState == DozeMachine.State.DOZE_AOD && !sWakeDisplaySensorState) {
Lucas Dupin5131f512019-02-01 12:57:19 -0800316 onWakeScreen(false, newState);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800317 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700318 break;
Adrian Roos5f7bee42017-06-27 18:49:23 +0200319 case DOZE_AOD_PAUSED:
Adrian Roos6023ccb2017-06-28 16:22:02 +0200320 case DOZE_AOD_PAUSING:
Adrian Roos5f7bee42017-06-27 18:49:23 +0200321 mDozeSensors.setProxListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700322 mDozeSensors.setPaused(true);
Adrian Roos5f7bee42017-06-27 18:49:23 +0200323 break;
Adrian Roos67cca742017-04-13 16:52:51 -0700324 case DOZE_PULSING:
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700325 case DOZE_PULSING_BRIGHT:
Adrian Roos98d31982017-08-02 20:50:16 +0200326 mDozeSensors.setTouchscreenSensorsListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700327 mDozeSensors.setProxListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700328 mDozeSensors.setPaused(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700329 break;
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800330 case DOZE_PULSE_DONE:
331 mDozeSensors.requestTemporaryDisable();
Lucas Dupin3174c662019-07-15 15:49:54 -0700332 // A pulse will temporarily disable sensors that require a touch screen.
333 // Let's make sure that they are re-enabled when the pulse is over.
334 mDozeSensors.updateListening();
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800335 break;
Adrian Roosff2c4562016-11-03 12:13:36 -0700336 case FINISH:
337 mBroadcastReceiver.unregister(mContext);
338 mDozeHost.removeCallback(mHostCallback);
lpeterac798f22019-02-12 15:15:22 +0800339 if (mDockManager != null) {
340 mDockManager.removeListener(mDockEventListener);
341 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700342 mDozeSensors.setListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700343 mDozeSensors.setProxListening(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700344 break;
345 default:
346 }
347 }
348
349 private void checkTriggersAtInit() {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200350 if (mUiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR
Adrian Roos710a0b12017-07-07 19:02:34 +0200351 || mDozeHost.isBlockingDoze()
Adrian Roosf2d545e2017-07-05 16:45:42 +0200352 || !mDozeHost.isProvisioned()) {
353 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700354 }
355 }
356
Selim Cinek65c96f22019-07-25 20:09:04 -0700357 private void requestPulse(final int reason, boolean performedProxCheck,
358 Runnable onPulseSuppressedListener) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700359 Assert.isMainThread();
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700360 mDozeHost.extendPulse(reason);
361
362 // When already pulsing we're allowed to show the wallpaper directly without
363 // requesting a new pulse.
364 if (mMachine.getState() == DozeMachine.State.DOZE_PULSING
Beverlycc4a62f2019-09-26 14:55:28 -0400365 && reason == DozeEvent.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN) {
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700366 mMachine.requestState(DozeMachine.State.DOZE_PULSING_BRIGHT);
367 return;
368 }
369
Adrian Roosf9d13f62016-11-08 15:42:20 -0800370 if (mPulsePending || !mAllowPulseTriggers || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700371 if (mAllowPulseTriggers) {
Beverlycc4a62f2019-09-26 14:55:28 -0400372 mDozeLog.tracePulseDropped(mPulsePending, mMachine.getState(),
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700373 mDozeHost.isPulsingBlocked());
374 }
Selim Cinek65c96f22019-07-25 20:09:04 -0700375 runIfNotNull(onPulseSuppressedListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700376 return;
377 }
378
379 mPulsePending = true;
Adrian Roosd32b3662017-06-27 14:48:50 +0200380 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400381 if (result != null && result) {
Adrian Roosd32b3662017-06-27 14:48:50 +0200382 // in pocket, abort pulse
Beverlycc4a62f2019-09-26 14:55:28 -0400383 mDozeLog.tracePulseDropped("inPocket");
Adrian Roosd32b3662017-06-27 14:48:50 +0200384 mPulsePending = false;
Selim Cinek65c96f22019-07-25 20:09:04 -0700385 runIfNotNull(onPulseSuppressedListener);
Adrian Roosd32b3662017-06-27 14:48:50 +0200386 } else {
387 // not in pocket, continue pulsing
Adrian Roosff2c4562016-11-03 12:13:36 -0700388 continuePulseRequest(reason);
389 }
Adrian Roosd32b3662017-06-27 14:48:50 +0200390 }, !mDozeParameters.getProxCheckBeforePulse() || performedProxCheck, reason);
Steven Wude353052019-03-12 13:49:23 -0400391
392 // Logs request pulse reason on AOD screen.
393 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
394 .setType(MetricsEvent.TYPE_UPDATE).setSubtype(reason));
Adrian Roosff2c4562016-11-03 12:13:36 -0700395 }
396
397 private boolean canPulse() {
398 return mMachine.getState() == DozeMachine.State.DOZE
399 || mMachine.getState() == DozeMachine.State.DOZE_AOD;
400 }
401
402 private void continuePulseRequest(int reason) {
403 mPulsePending = false;
404 if (mDozeHost.isPulsingBlocked() || !canPulse()) {
Beverlycc4a62f2019-09-26 14:55:28 -0400405 mDozeLog.tracePulseDropped(mPulsePending, mMachine.getState(),
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700406 mDozeHost.isPulsingBlocked());
Adrian Roosff2c4562016-11-03 12:13:36 -0700407 return;
408 }
Adrian Roosd7b9d102017-04-28 15:42:58 -0700409 mMachine.requestPulse(reason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700410 }
411
412 @Override
413 public void dump(PrintWriter pw) {
414 pw.print(" notificationPulseTime=");
415 pw.println(Formatter.formatShortElapsedTime(mContext, mNotificationPulseTime));
416
Dave Mankoff63a12822019-09-16 14:38:06 -0400417 pw.println(" pulsePending=" + mPulsePending);
Adrian Roosff2c4562016-11-03 12:13:36 -0700418 pw.println("DozeSensors:");
419 mDozeSensors.dump(pw);
420 }
421
Adrian Roosff2c4562016-11-03 12:13:36 -0700422 private class TriggerReceiver extends BroadcastReceiver {
Adrian Roos67cca742017-04-13 16:52:51 -0700423 private boolean mRegistered;
424
Adrian Roosff2c4562016-11-03 12:13:36 -0700425 @Override
426 public void onReceive(Context context, Intent intent) {
427 if (PULSE_ACTION.equals(intent.getAction())) {
428 if (DozeMachine.DEBUG) Log.d(TAG, "Received pulse intent");
Beverlycc4a62f2019-09-26 14:55:28 -0400429 requestPulse(DozeEvent.PULSE_REASON_INTENT, false, /* performedProxCheck */
Selim Cinek65c96f22019-07-25 20:09:04 -0700430 null /* onPulseSupressedListener */);
Adrian Roosff2c4562016-11-03 12:13:36 -0700431 }
432 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200433 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700434 }
435 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
436 mDozeSensors.onUserSwitched();
437 }
438 }
439
440 public void register(Context context) {
Adrian Roos67cca742017-04-13 16:52:51 -0700441 if (mRegistered) {
442 return;
443 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700444 IntentFilter filter = new IntentFilter(PULSE_ACTION);
445 filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
446 filter.addAction(Intent.ACTION_USER_SWITCHED);
447 context.registerReceiver(this, filter);
Adrian Roos67cca742017-04-13 16:52:51 -0700448 mRegistered = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700449 }
450
451 public void unregister(Context context) {
Adrian Roos67cca742017-04-13 16:52:51 -0700452 if (!mRegistered) {
453 return;
454 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700455 context.unregisterReceiver(this);
Adrian Roos67cca742017-04-13 16:52:51 -0700456 mRegistered = false;
Adrian Roosff2c4562016-11-03 12:13:36 -0700457 }
458 }
459
lpeter8a5f4702019-01-18 16:53:07 +0800460 private class DockEventListener implements DockManager.DockEventListener {
461 @Override
462 public void onEvent(int event) {
463 if (DEBUG) Log.d(TAG, "dock event = " + event);
464 switch (event) {
465 case DockManager.STATE_DOCKED:
466 case DockManager.STATE_DOCKED_HIDE:
467 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(true);
468 break;
469 case DockManager.STATE_NONE:
470 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(false);
471 break;
472 default:
473 // no-op
474 }
475 }
476 }
477
Adrian Roosff2c4562016-11-03 12:13:36 -0700478 private DozeHost.Callback mHostCallback = new DozeHost.Callback() {
479 @Override
Selim Cinek65c96f22019-07-25 20:09:04 -0700480 public void onNotificationAlerted(Runnable onPulseSuppressedListener) {
481 onNotification(onPulseSuppressedListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700482 }
483
484 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700485 public void onPowerSaveChanged(boolean active) {
Lucas Dupinbd7366d2019-09-25 13:39:21 -0700486 if (mDozeHost.isPowerSaveActive()) {
487 mMachine.requestState(DozeMachine.State.DOZE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700488 }
489 }
490 };
491}