blob: 722dc038f8531c93f3c2117cd1bf9cd00ae381bd [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;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000039import com.android.systemui.broadcast.BroadcastDispatcher;
lpeter8a5f4702019-01-18 16:53:07 +080040import com.android.systemui.dock.DockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070041import com.android.systemui.statusbar.phone.DozeParameters;
42import com.android.systemui.util.Assert;
Dave Mankoff63a12822019-09-16 14:38:06 -040043import com.android.systemui.util.sensors.AsyncSensorManager;
44import com.android.systemui.util.sensors.ProximitySensor;
Adrian Roosc1b50322017-02-27 21:07:58 +010045import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070046
47import java.io.PrintWriter;
Dave Mankoff63a12822019-09-16 14:38:06 -040048import java.util.function.Consumer;
Adrian Roosff2c4562016-11-03 12:13:36 -070049
50/**
51 * Handles triggers for ambient state changes.
52 */
53public class DozeTriggers implements DozeMachine.Part {
54
55 private static final String TAG = "DozeTriggers";
Adrian Roos67cca742017-04-13 16:52:51 -070056 private static final boolean DEBUG = DozeService.DEBUG;
Adrian Roosff2c4562016-11-03 12:13:36 -070057
58 /** adb shell am broadcast -a com.android.systemui.doze.pulse com.android.systemui */
59 private static final String PULSE_ACTION = "com.android.systemui.doze.pulse";
60
Lucas Dupin1ae6cf92018-12-14 18:06:38 -080061 /**
62 * Last value sent by the wake-display sensor.
63 * Assuming that the screen should start on.
64 */
65 private static boolean sWakeDisplaySensorState = true;
66
Dave Mankoff63a12822019-09-16 14:38:06 -040067 private static final int PROXIMITY_TIMEOUT_DELAY_MS = 500;
68
Adrian Roosff2c4562016-11-03 12:13:36 -070069 private final Context mContext;
70 private final DozeMachine mMachine;
Beverlycc4a62f2019-09-26 14:55:28 -040071 private final DozeLog mDozeLog;
Adrian Roosff2c4562016-11-03 12:13:36 -070072 private final DozeSensors mDozeSensors;
73 private final DozeHost mDozeHost;
74 private final AmbientDisplayConfiguration mConfig;
75 private final DozeParameters mDozeParameters;
Dave Mankoff63a12822019-09-16 14:38:06 -040076 private final AsyncSensorManager mSensorManager;
Adrian Roosc1b50322017-02-27 21:07:58 +010077 private final WakeLock mWakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -080078 private final boolean mAllowPulseTriggers;
Adrian Roosff2c4562016-11-03 12:13:36 -070079 private final UiModeManager mUiModeManager;
80 private final TriggerReceiver mBroadcastReceiver = new TriggerReceiver();
lpeter8a5f4702019-01-18 16:53:07 +080081 private final DockEventListener mDockEventListener = new DockEventListener();
82 private final DockManager mDockManager;
Dave Mankoff63a12822019-09-16 14:38:06 -040083 private final ProximitySensor.ProximityCheck mProxCheck;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000084 private final BroadcastDispatcher mBroadcastDispatcher;
Adrian Roosff2c4562016-11-03 12:13:36 -070085
86 private long mNotificationPulseTime;
87 private boolean mPulsePending;
88
Steven Wude353052019-03-12 13:49:23 -040089 private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070090
91 public DozeTriggers(Context context, DozeMachine machine, DozeHost dozeHost,
Adrian Roos6023ccb2017-06-28 16:22:02 +020092 AlarmManager alarmManager, AmbientDisplayConfiguration config,
Dave Mankoff63a12822019-09-16 14:38:06 -040093 DozeParameters dozeParameters, AsyncSensorManager sensorManager, Handler handler,
Dave Mankoffbf52f4b2019-09-20 14:34:28 -040094 WakeLock wakeLock, boolean allowPulseTriggers, DockManager dockManager,
Beverlycc4a62f2019-09-26 14:55:28 -040095 ProximitySensor proximitySensor,
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000096 DozeLog dozeLog, BroadcastDispatcher broadcastDispatcher) {
Adrian Roosff2c4562016-11-03 12:13:36 -070097 mContext = context;
98 mMachine = machine;
99 mDozeHost = dozeHost;
100 mConfig = config;
101 mDozeParameters = dozeParameters;
102 mSensorManager = sensorManager;
Adrian Roosff2c4562016-11-03 12:13:36 -0700103 mWakeLock = wakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -0800104 mAllowPulseTriggers = allowPulseTriggers;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200105 mDozeSensors = new DozeSensors(context, alarmManager, mSensorManager, dozeParameters,
Dave Mankoff86c73442019-12-17 12:15:16 -0500106 config, wakeLock, this::onSensor, this::onProximityFar, dozeLog);
Adrian Roosff2c4562016-11-03 12:13:36 -0700107 mUiModeManager = mContext.getSystemService(UiModeManager.class);
lpeter8a5f4702019-01-18 16:53:07 +0800108 mDockManager = dockManager;
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400109 mProxCheck = new ProximitySensor.ProximityCheck(proximitySensor, handler);
Beverlycc4a62f2019-09-26 14:55:28 -0400110 mDozeLog = dozeLog;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000111 mBroadcastDispatcher = broadcastDispatcher;
Adrian Roosff2c4562016-11-03 12:13:36 -0700112 }
113
Selim Cinek65c96f22019-07-25 20:09:04 -0700114 private void onNotification(Runnable onPulseSuppressedListener) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400115 if (DozeMachine.DEBUG) {
116 Log.d(TAG, "requestNotificationPulse");
117 }
118 if (!sWakeDisplaySensorState) {
119 Log.d(TAG, "Wake display false. Pulse denied.");
Selim Cinek65c96f22019-07-25 20:09:04 -0700120 runIfNotNull(onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400121 mDozeLog.tracePulseDropped("wakeDisplaySensor");
Dave Mankoff15bbec62019-06-27 15:17:42 -0400122 return;
123 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700124 mNotificationPulseTime = SystemClock.elapsedRealtime();
Dave Mankoff15bbec62019-06-27 15:17:42 -0400125 if (!mConfig.pulseOnNotificationEnabled(UserHandle.USER_CURRENT)) {
Selim Cinek65c96f22019-07-25 20:09:04 -0700126 runIfNotNull(onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400127 mDozeLog.tracePulseDropped("pulseOnNotificationsDisabled");
Dave Mankoff15bbec62019-06-27 15:17:42 -0400128 return;
129 }
Beverlycc4a62f2019-09-26 14:55:28 -0400130 requestPulse(DozeEvent.PULSE_REASON_NOTIFICATION, false /* performedProxCheck */,
Selim Cinek65c96f22019-07-25 20:09:04 -0700131 onPulseSuppressedListener);
Beverlycc4a62f2019-09-26 14:55:28 -0400132 mDozeLog.traceNotificationPulse();
Adrian Roosff2c4562016-11-03 12:13:36 -0700133 }
134
Selim Cinek65c96f22019-07-25 20:09:04 -0700135 private static void runIfNotNull(Runnable runnable) {
136 if (runnable != null) {
137 runnable.run();
138 }
139 }
140
Dave Mankoff63a12822019-09-16 14:38:06 -0400141 private void proximityCheckThenCall(Consumer<Boolean> callback,
Adrian Roosd32b3662017-06-27 14:48:50 +0200142 boolean alreadyPerformedProxCheck,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700143 int reason) {
Dave Mankoff63a12822019-09-16 14:38:06 -0400144 Boolean cachedProxNear = mDozeSensors.isProximityCurrentlyNear();
Adrian Roosd32b3662017-06-27 14:48:50 +0200145 if (alreadyPerformedProxCheck) {
Dave Mankoff63a12822019-09-16 14:38:06 -0400146 callback.accept(null);
147 } else if (cachedProxNear != null) {
148 callback.accept(cachedProxNear);
Adrian Roosd32b3662017-06-27 14:48:50 +0200149 } else {
150 final long start = SystemClock.uptimeMillis();
Dave Mankoff63a12822019-09-16 14:38:06 -0400151 mProxCheck.check(PROXIMITY_TIMEOUT_DELAY_MS, near -> {
152 final long end = SystemClock.uptimeMillis();
Beverlycc4a62f2019-09-26 14:55:28 -0400153 mDozeLog.traceProximityResult(
Dave Mankoff63a12822019-09-16 14:38:06 -0400154 near == null ? false : near,
155 end - start,
156 reason);
157 callback.accept(near);
158 mWakeLock.release(TAG);
159 });
160 mWakeLock.acquire(TAG);
Adrian Roosd32b3662017-06-27 14:48:50 +0200161 }
162 }
163
lpeter8a5f4702019-01-18 16:53:07 +0800164 @VisibleForTesting
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700165 void onSensor(int pulseReason, float screenX, float screenY, float[] rawValues) {
Beverlycc4a62f2019-09-26 14:55:28 -0400166 boolean isDoubleTap = pulseReason == DozeEvent.REASON_SENSOR_DOUBLE_TAP;
167 boolean isTap = pulseReason == DozeEvent.REASON_SENSOR_TAP;
168 boolean isPickup = pulseReason == DozeEvent.REASON_SENSOR_PICKUP;
169 boolean isLongPress = pulseReason == DozeEvent.PULSE_REASON_SENSOR_LONG_PRESS;
170 boolean isWakeDisplay = pulseReason == DozeEvent.REASON_SENSOR_WAKE_UP;
171 boolean isWakeLockScreen = pulseReason == DozeEvent.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800172 boolean wakeEvent = rawValues != null && rawValues.length > 0 && rawValues[0] != 0;
Adrian Roosff2c4562016-11-03 12:13:36 -0700173
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800174 if (isWakeDisplay) {
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700175 onWakeScreen(wakeEvent, mMachine.isExecutingTransition() ? null : mMachine.getState());
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800176 } else if (isLongPress) {
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700177 requestPulse(pulseReason, true /* alreadyPerformedProxCheck */,
178 null /* onPulseSupressedListener */);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800179 } else if (isWakeLockScreen) {
180 if (wakeEvent) {
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700181 requestPulse(pulseReason, true /* alreadyPerformedProxCheck */,
Selim Cinek65c96f22019-07-25 20:09:04 -0700182 null /* onPulseSupressedListener */);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800183 }
Lucas Dupin4359b552018-08-09 15:07:54 -0700184 } else {
Adrian Roosd32b3662017-06-27 14:48:50 +0200185 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400186 if (result != null && result) {
Adrian Roosd32b3662017-06-27 14:48:50 +0200187 // In pocket, drop event.
188 return;
189 }
Lucas Dupind43bf702019-01-15 13:40:42 -0800190 if (isDoubleTap || isTap) {
191 if (screenX != -1 && screenY != -1) {
192 mDozeHost.onSlpiTap(screenX, screenY);
193 }
Lucas Dupined5b7a92019-03-20 11:00:27 -0700194 gentleWakeUp(pulseReason);
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800195 } else if (isPickup) {
Lucas Dupined5b7a92019-03-20 11:00:27 -0700196 gentleWakeUp(pulseReason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200197 } else {
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700198 mDozeHost.extendPulse(pulseReason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200199 }
Lucas Dupinf40bd8f2019-08-07 15:55:00 -0700200 }, true /* alreadyPerformedProxCheck */, pulseReason);
Adrian Roosed85e582017-04-27 15:09:28 -0700201 }
202
203 if (isPickup) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700204 final long timeSinceNotification =
205 SystemClock.elapsedRealtime() - mNotificationPulseTime;
206 final boolean withinVibrationThreshold =
207 timeSinceNotification < mDozeParameters.getPickupVibrationThreshold();
Beverlycc4a62f2019-09-26 14:55:28 -0400208 mDozeLog.tracePickupWakeUp(withinVibrationThreshold);
Adrian Roosff2c4562016-11-03 12:13:36 -0700209 }
210 }
211
Lucas Dupined5b7a92019-03-20 11:00:27 -0700212 private void gentleWakeUp(int reason) {
213 // Log screen wake up reason (lift/pickup, tap, double-tap)
214 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
215 .setType(MetricsEvent.TYPE_UPDATE)
216 .setSubtype(reason));
217 if (mDozeParameters.getDisplayNeedsBlanking()) {
218 // Let's prepare the display to wake-up by drawing black.
219 // This will cover the hardware wake-up sequence, where the display
220 // becomes black for a few frames.
Lucas Dupin34306c32019-07-16 11:56:53 -0700221 mDozeHost.setAodDimmingScrim(1f);
Lucas Dupined5b7a92019-03-20 11:00:27 -0700222 }
223 mMachine.wakeUp();
224 }
225
Adrian Roos67cca742017-04-13 16:52:51 -0700226 private void onProximityFar(boolean far) {
Lucas Dupin1946b312019-03-21 14:48:23 -0700227 // Proximity checks are asynchronous and the user might have interacted with the phone
228 // when a new event is arriving. This means that a state transition might have happened
229 // and the proximity check is now obsolete.
230 if (mMachine.isExecutingTransition()) {
231 Log.w(TAG, "onProximityFar called during transition. Ignoring sensor response.");
232 return;
233 }
234
Adrian Roos67cca742017-04-13 16:52:51 -0700235 final boolean near = !far;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200236 final DozeMachine.State state = mMachine.getState();
Adrian Roosc7fd6962017-09-06 16:46:46 +0200237 final boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
238 final boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
239 final boolean aod = (state == DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200240
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700241 if (state == DozeMachine.State.DOZE_PULSING
242 || state == DozeMachine.State.DOZE_PULSING_BRIGHT) {
Adrian Roosa6c03f82017-07-26 16:20:30 +0200243 boolean ignoreTouch = near;
Dave Mankoff15bbec62019-06-27 15:17:42 -0400244 if (DEBUG) {
245 Log.i(TAG, "Prox changed, ignore touch = " + ignoreTouch);
246 }
Adrian Roosa6c03f82017-07-26 16:20:30 +0200247 mDozeHost.onIgnoreTouchWhilePulsing(ignoreTouch);
Adrian Roos67cca742017-04-13 16:52:51 -0700248 }
Lucas Dupin65104382018-12-04 11:53:42 -0800249
Adrian Roosc7fd6962017-09-06 16:46:46 +0200250 if (far && (paused || pausing)) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400251 if (DEBUG) {
252 Log.i(TAG, "Prox FAR, unpausing AOD");
253 }
Adrian Roos67cca742017-04-13 16:52:51 -0700254 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200255 } else if (near && aod) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400256 if (DEBUG) {
257 Log.i(TAG, "Prox NEAR, pausing AOD");
258 }
Adrian Roos6023ccb2017-06-28 16:22:02 +0200259 mMachine.requestState(DozeMachine.State.DOZE_AOD_PAUSING);
Adrian Roos67cca742017-04-13 16:52:51 -0700260 }
261 }
262
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700263 /**
264 * When a wake screen event is received from a sensor
265 * @param wake {@code true} when it's time to wake up, {@code false} when we should sleep.
266 * @param state The current state, or null if the state could not be determined due to enqueued
267 * transitions.
268 */
269 private void onWakeScreen(boolean wake, @Nullable DozeMachine.State state) {
Beverlycc4a62f2019-09-26 14:55:28 -0400270 mDozeLog.traceWakeDisplay(wake);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800271 sWakeDisplaySensorState = wake;
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700272
273 if (wake) {
274 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400275 if (result != null && result) {
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700276 // In pocket, drop event.
277 return;
278 }
Lucas Dupin5131f512019-02-01 12:57:19 -0800279 if (state == DozeMachine.State.DOZE) {
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700280 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Steven Wucef2d5d2019-04-23 13:27:33 -0400281 // Logs AOD open due to sensor wake up.
282 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
283 .setType(MetricsEvent.TYPE_OPEN)
Beverlycc4a62f2019-09-26 14:55:28 -0400284 .setSubtype(DozeEvent.REASON_SENSOR_WAKE_UP));
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700285 }
Beverlycc4a62f2019-09-26 14:55:28 -0400286 }, true /* alreadyPerformedProxCheck */, DozeEvent.REASON_SENSOR_WAKE_UP);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700287 } else {
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700288 boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
289 boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700290 if (!pausing && !paused) {
Lucas Dupin65104382018-12-04 11:53:42 -0800291 mMachine.requestState(DozeMachine.State.DOZE);
Steven Wucef2d5d2019-04-23 13:27:33 -0400292 // Logs AOD close due to sensor wake up.
293 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
294 .setType(MetricsEvent.TYPE_CLOSE)
Beverlycc4a62f2019-09-26 14:55:28 -0400295 .setSubtype(DozeEvent.REASON_SENSOR_WAKE_UP));
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700296 }
297 }
298 }
299
Adrian Roosff2c4562016-11-03 12:13:36 -0700300 @Override
301 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
302 switch (newState) {
303 case INITIALIZED:
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000304 mBroadcastReceiver.register(mBroadcastDispatcher);
Adrian Roosff2c4562016-11-03 12:13:36 -0700305 mDozeHost.addCallback(mHostCallback);
Jerry Changae4dc4b2019-10-16 18:45:03 +0800306 mDockManager.addListener(mDockEventListener);
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:
Jerry Changae4dc4b2019-10-16 18:45:03 +0800326 case DOZE_AOD_DOCKED:
Adrian Roos98d31982017-08-02 20:50:16 +0200327 mDozeSensors.setTouchscreenSensorsListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700328 mDozeSensors.setProxListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700329 mDozeSensors.setPaused(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700330 break;
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800331 case DOZE_PULSE_DONE:
332 mDozeSensors.requestTemporaryDisable();
Lucas Dupin3174c662019-07-15 15:49:54 -0700333 // A pulse will temporarily disable sensors that require a touch screen.
334 // Let's make sure that they are re-enabled when the pulse is over.
335 mDozeSensors.updateListening();
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800336 break;
Adrian Roosff2c4562016-11-03 12:13:36 -0700337 case FINISH:
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000338 mBroadcastReceiver.unregister(mBroadcastDispatcher);
Adrian Roosff2c4562016-11-03 12:13:36 -0700339 mDozeHost.removeCallback(mHostCallback);
Jerry Changae4dc4b2019-10-16 18:45:03 +0800340 mDockManager.removeListener(mDockEventListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700341 mDozeSensors.setListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700342 mDozeSensors.setProxListening(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700343 break;
344 default:
345 }
346 }
347
348 private void checkTriggersAtInit() {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200349 if (mUiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR
Adrian Roos710a0b12017-07-07 19:02:34 +0200350 || mDozeHost.isBlockingDoze()
Adrian Roosf2d545e2017-07-05 16:45:42 +0200351 || !mDozeHost.isProvisioned()) {
352 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700353 }
354 }
355
Selim Cinek65c96f22019-07-25 20:09:04 -0700356 private void requestPulse(final int reason, boolean performedProxCheck,
357 Runnable onPulseSuppressedListener) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700358 Assert.isMainThread();
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700359 mDozeHost.extendPulse(reason);
360
361 // When already pulsing we're allowed to show the wallpaper directly without
362 // requesting a new pulse.
363 if (mMachine.getState() == DozeMachine.State.DOZE_PULSING
Beverlycc4a62f2019-09-26 14:55:28 -0400364 && reason == DozeEvent.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN) {
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700365 mMachine.requestState(DozeMachine.State.DOZE_PULSING_BRIGHT);
366 return;
367 }
368
Adrian Roosf9d13f62016-11-08 15:42:20 -0800369 if (mPulsePending || !mAllowPulseTriggers || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700370 if (mAllowPulseTriggers) {
Beverlycc4a62f2019-09-26 14:55:28 -0400371 mDozeLog.tracePulseDropped(mPulsePending, mMachine.getState(),
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700372 mDozeHost.isPulsingBlocked());
373 }
Selim Cinek65c96f22019-07-25 20:09:04 -0700374 runIfNotNull(onPulseSuppressedListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700375 return;
376 }
377
378 mPulsePending = true;
Adrian Roosd32b3662017-06-27 14:48:50 +0200379 proximityCheckThenCall((result) -> {
Dave Mankoffbf52f4b2019-09-20 14:34:28 -0400380 if (result != null && result) {
Adrian Roosd32b3662017-06-27 14:48:50 +0200381 // in pocket, abort pulse
Beverlycc4a62f2019-09-26 14:55:28 -0400382 mDozeLog.tracePulseDropped("inPocket");
Adrian Roosd32b3662017-06-27 14:48:50 +0200383 mPulsePending = false;
Selim Cinek65c96f22019-07-25 20:09:04 -0700384 runIfNotNull(onPulseSuppressedListener);
Adrian Roosd32b3662017-06-27 14:48:50 +0200385 } else {
386 // not in pocket, continue pulsing
Adrian Roosff2c4562016-11-03 12:13:36 -0700387 continuePulseRequest(reason);
388 }
Adrian Roosd32b3662017-06-27 14:48:50 +0200389 }, !mDozeParameters.getProxCheckBeforePulse() || performedProxCheck, reason);
Steven Wude353052019-03-12 13:49:23 -0400390
391 // Logs request pulse reason on AOD screen.
392 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
393 .setType(MetricsEvent.TYPE_UPDATE).setSubtype(reason));
Adrian Roosff2c4562016-11-03 12:13:36 -0700394 }
395
396 private boolean canPulse() {
397 return mMachine.getState() == DozeMachine.State.DOZE
Jerry Changae4dc4b2019-10-16 18:45:03 +0800398 || mMachine.getState() == DozeMachine.State.DOZE_AOD
399 || mMachine.getState() == DozeMachine.State.DOZE_AOD_DOCKED;
Adrian Roosff2c4562016-11-03 12:13:36 -0700400 }
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
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000440 public void register(BroadcastDispatcher broadcastDispatcher) {
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);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000447 broadcastDispatcher.registerReceiver(this, filter);
Adrian Roos67cca742017-04-13 16:52:51 -0700448 mRegistered = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700449 }
450
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000451 public void unregister(BroadcastDispatcher broadcastDispatcher) {
Adrian Roos67cca742017-04-13 16:52:51 -0700452 if (!mRegistered) {
453 return;
454 }
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000455 broadcastDispatcher.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}