blob: 97b08d5a12a6a27a879bac8144c9ac802fc3bb88 [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;
27import android.hardware.Sensor;
28import android.hardware.SensorEvent;
29import android.hardware.SensorEventListener;
30import android.hardware.SensorManager;
Issei Suzukica19e6e2019-02-26 12:39:11 +010031import android.hardware.display.AmbientDisplayConfiguration;
Steven Wude353052019-03-12 13:49:23 -040032import android.metrics.LogMaker;
Adrian Roosff2c4562016-11-03 12:13:36 -070033import android.os.Handler;
34import android.os.SystemClock;
35import android.os.UserHandle;
36import android.text.format.Formatter;
37import android.util.Log;
38
lpeter8a5f4702019-01-18 16:53:07 +080039import com.android.internal.annotations.VisibleForTesting;
Steven Wude353052019-03-12 13:49:23 -040040import com.android.internal.logging.MetricsLogger;
41import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Adrian Roosff2c4562016-11-03 12:13:36 -070042import com.android.internal.util.Preconditions;
Steven Wude353052019-03-12 13:49:23 -040043import com.android.systemui.Dependency;
lpeter8a5f4702019-01-18 16:53:07 +080044import com.android.systemui.dock.DockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070045import com.android.systemui.statusbar.phone.DozeParameters;
46import com.android.systemui.util.Assert;
Adrian Roosc1b50322017-02-27 21:07:58 +010047import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070048
49import java.io.PrintWriter;
Adrian Roosd32b3662017-06-27 14:48:50 +020050import java.util.function.IntConsumer;
Adrian Roosff2c4562016-11-03 12:13:36 -070051
52/**
53 * Handles triggers for ambient state changes.
54 */
55public class DozeTriggers implements DozeMachine.Part {
56
57 private static final String TAG = "DozeTriggers";
Adrian Roos67cca742017-04-13 16:52:51 -070058 private static final boolean DEBUG = DozeService.DEBUG;
Adrian Roosff2c4562016-11-03 12:13:36 -070059
60 /** adb shell am broadcast -a com.android.systemui.doze.pulse com.android.systemui */
61 private static final String PULSE_ACTION = "com.android.systemui.doze.pulse";
62
Lucas Dupin1ae6cf92018-12-14 18:06:38 -080063 /**
64 * Last value sent by the wake-display sensor.
65 * Assuming that the screen should start on.
66 */
67 private static boolean sWakeDisplaySensorState = true;
68
Adrian Roosff2c4562016-11-03 12:13:36 -070069 private final Context mContext;
70 private final DozeMachine mMachine;
71 private final DozeSensors mDozeSensors;
72 private final DozeHost mDozeHost;
73 private final AmbientDisplayConfiguration mConfig;
74 private final DozeParameters mDozeParameters;
75 private final SensorManager mSensorManager;
76 private final Handler mHandler;
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;
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,
Adrian Roosff2c4562016-11-03 12:13:36 -070091 DozeParameters dozeParameters, SensorManager sensorManager, Handler handler,
lpeter8a5f4702019-01-18 16:53:07 +080092 WakeLock wakeLock, boolean allowPulseTriggers, DockManager dockManager) {
Adrian Roosff2c4562016-11-03 12:13:36 -070093 mContext = context;
94 mMachine = machine;
95 mDozeHost = dozeHost;
96 mConfig = config;
97 mDozeParameters = dozeParameters;
98 mSensorManager = sensorManager;
99 mHandler = handler;
100 mWakeLock = wakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -0800101 mAllowPulseTriggers = allowPulseTriggers;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200102 mDozeSensors = new DozeSensors(context, alarmManager, mSensorManager, dozeParameters,
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800103 config, wakeLock, this::onSensor, this::onProximityFar,
Adrian Roos2f5a3852018-04-23 17:48:08 +0200104 dozeParameters.getPolicy());
Adrian Roosff2c4562016-11-03 12:13:36 -0700105 mUiModeManager = mContext.getSystemService(UiModeManager.class);
lpeter8a5f4702019-01-18 16:53:07 +0800106 mDockManager = dockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -0700107 }
108
109 private void onNotification() {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400110 if (DozeMachine.DEBUG) {
111 Log.d(TAG, "requestNotificationPulse");
112 }
113 if (!sWakeDisplaySensorState) {
114 Log.d(TAG, "Wake display false. Pulse denied.");
115 return;
116 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700117 mNotificationPulseTime = SystemClock.elapsedRealtime();
Dave Mankoff15bbec62019-06-27 15:17:42 -0400118 if (!mConfig.pulseOnNotificationEnabled(UserHandle.USER_CURRENT)) {
119 return;
120 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700121 requestPulse(DozeLog.PULSE_REASON_NOTIFICATION, false /* performedProxCheck */);
Adrian Roos181c3202016-11-07 13:24:31 -0800122 DozeLog.traceNotificationPulse(mContext);
Adrian Roosff2c4562016-11-03 12:13:36 -0700123 }
124
Adrian Roosd32b3662017-06-27 14:48:50 +0200125 private void proximityCheckThenCall(IntConsumer callback,
126 boolean alreadyPerformedProxCheck,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700127 int reason) {
Adrian Roos70da03a2017-07-24 16:42:57 +0200128 Boolean cachedProxFar = mDozeSensors.isProximityCurrentlyFar();
Adrian Roosd32b3662017-06-27 14:48:50 +0200129 if (alreadyPerformedProxCheck) {
130 callback.accept(ProximityCheck.RESULT_NOT_CHECKED);
Adrian Roos70da03a2017-07-24 16:42:57 +0200131 } else if (cachedProxFar != null) {
132 callback.accept(cachedProxFar ? ProximityCheck.RESULT_FAR : ProximityCheck.RESULT_NEAR);
Adrian Roosd32b3662017-06-27 14:48:50 +0200133 } else {
134 final long start = SystemClock.uptimeMillis();
135 new ProximityCheck() {
136 @Override
137 public void onProximityResult(int result) {
138 final long end = SystemClock.uptimeMillis();
139 DozeLog.traceProximityResult(mContext, result == RESULT_NEAR,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700140 end - start, reason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200141 callback.accept(result);
142 }
143 }.check();
144 }
145 }
146
lpeter8a5f4702019-01-18 16:53:07 +0800147 @VisibleForTesting
148 void onSensor(int pulseReason, boolean sensorPerformedProxCheck,
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800149 float screenX, float screenY, float[] rawValues) {
Lucas Dupin3d053532019-01-29 12:35:22 -0800150 boolean isDoubleTap = pulseReason == DozeLog.REASON_SENSOR_DOUBLE_TAP;
151 boolean isTap = pulseReason == DozeLog.REASON_SENSOR_TAP;
152 boolean isPickup = pulseReason == DozeLog.REASON_SENSOR_PICKUP;
Adrian Roosd0963a02017-05-15 14:33:37 -0700153 boolean isLongPress = pulseReason == DozeLog.PULSE_REASON_SENSOR_LONG_PRESS;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800154 boolean isWakeDisplay = pulseReason == DozeLog.REASON_SENSOR_WAKE_UP;
Lucas Dupinde64ee02018-12-21 14:45:12 -0800155 boolean isWakeLockScreen = pulseReason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800156 boolean wakeEvent = rawValues != null && rawValues.length > 0 && rawValues[0] != 0;
Adrian Roosff2c4562016-11-03 12:13:36 -0700157
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800158 if (isWakeDisplay) {
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700159 onWakeScreen(wakeEvent, mMachine.isExecutingTransition() ? null : mMachine.getState());
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800160 } else if (isLongPress) {
Lucas Dupin4359b552018-08-09 15:07:54 -0700161 requestPulse(pulseReason, sensorPerformedProxCheck);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800162 } else if (isWakeLockScreen) {
163 if (wakeEvent) {
164 requestPulse(pulseReason, sensorPerformedProxCheck);
165 }
Lucas Dupin4359b552018-08-09 15:07:54 -0700166 } else {
Adrian Roosd32b3662017-06-27 14:48:50 +0200167 proximityCheckThenCall((result) -> {
168 if (result == ProximityCheck.RESULT_NEAR) {
169 // In pocket, drop event.
170 return;
171 }
Lucas Dupind43bf702019-01-15 13:40:42 -0800172 if (isDoubleTap || isTap) {
173 if (screenX != -1 && screenY != -1) {
174 mDozeHost.onSlpiTap(screenX, screenY);
175 }
Lucas Dupined5b7a92019-03-20 11:00:27 -0700176 gentleWakeUp(pulseReason);
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800177 } else if (isPickup) {
Lucas Dupined5b7a92019-03-20 11:00:27 -0700178 gentleWakeUp(pulseReason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200179 } else {
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700180 mDozeHost.extendPulse(pulseReason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200181 }
lpeterac798f22019-02-12 15:15:22 +0800182 }, sensorPerformedProxCheck
183 || (mDockManager != null && mDockManager.isDocked()), pulseReason);
Adrian Roosed85e582017-04-27 15:09:28 -0700184 }
185
186 if (isPickup) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700187 final long timeSinceNotification =
188 SystemClock.elapsedRealtime() - mNotificationPulseTime;
189 final boolean withinVibrationThreshold =
190 timeSinceNotification < mDozeParameters.getPickupVibrationThreshold();
Lucas Dupin4359b552018-08-09 15:07:54 -0700191 DozeLog.tracePickupWakeUp(mContext, withinVibrationThreshold);
Adrian Roosff2c4562016-11-03 12:13:36 -0700192 }
193 }
194
Lucas Dupined5b7a92019-03-20 11:00:27 -0700195 private void gentleWakeUp(int reason) {
196 // Log screen wake up reason (lift/pickup, tap, double-tap)
197 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
198 .setType(MetricsEvent.TYPE_UPDATE)
199 .setSubtype(reason));
200 if (mDozeParameters.getDisplayNeedsBlanking()) {
201 // Let's prepare the display to wake-up by drawing black.
202 // This will cover the hardware wake-up sequence, where the display
203 // becomes black for a few frames.
204 mDozeHost.setAodDimmingScrim(255f);
205 }
206 mMachine.wakeUp();
207 }
208
Adrian Roos67cca742017-04-13 16:52:51 -0700209 private void onProximityFar(boolean far) {
Lucas Dupin1946b312019-03-21 14:48:23 -0700210 // Proximity checks are asynchronous and the user might have interacted with the phone
211 // when a new event is arriving. This means that a state transition might have happened
212 // and the proximity check is now obsolete.
213 if (mMachine.isExecutingTransition()) {
214 Log.w(TAG, "onProximityFar called during transition. Ignoring sensor response.");
215 return;
216 }
217
Adrian Roos67cca742017-04-13 16:52:51 -0700218 final boolean near = !far;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200219 final DozeMachine.State state = mMachine.getState();
Adrian Roosc7fd6962017-09-06 16:46:46 +0200220 final boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
221 final boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
222 final boolean aod = (state == DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200223
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700224 if (state == DozeMachine.State.DOZE_PULSING
225 || state == DozeMachine.State.DOZE_PULSING_BRIGHT) {
Adrian Roosa6c03f82017-07-26 16:20:30 +0200226 boolean ignoreTouch = near;
Dave Mankoff15bbec62019-06-27 15:17:42 -0400227 if (DEBUG) {
228 Log.i(TAG, "Prox changed, ignore touch = " + ignoreTouch);
229 }
Adrian Roosa6c03f82017-07-26 16:20:30 +0200230 mDozeHost.onIgnoreTouchWhilePulsing(ignoreTouch);
Adrian Roos67cca742017-04-13 16:52:51 -0700231 }
Lucas Dupin65104382018-12-04 11:53:42 -0800232
Adrian Roosc7fd6962017-09-06 16:46:46 +0200233 if (far && (paused || pausing)) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400234 if (DEBUG) {
235 Log.i(TAG, "Prox FAR, unpausing AOD");
236 }
Adrian Roos67cca742017-04-13 16:52:51 -0700237 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200238 } else if (near && aod) {
Dave Mankoff15bbec62019-06-27 15:17:42 -0400239 if (DEBUG) {
240 Log.i(TAG, "Prox NEAR, pausing AOD");
241 }
Adrian Roos6023ccb2017-06-28 16:22:02 +0200242 mMachine.requestState(DozeMachine.State.DOZE_AOD_PAUSING);
Adrian Roos67cca742017-04-13 16:52:51 -0700243 }
244 }
245
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700246 /**
247 * When a wake screen event is received from a sensor
248 * @param wake {@code true} when it's time to wake up, {@code false} when we should sleep.
249 * @param state The current state, or null if the state could not be determined due to enqueued
250 * transitions.
251 */
252 private void onWakeScreen(boolean wake, @Nullable DozeMachine.State state) {
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800253 DozeLog.traceWakeDisplay(wake);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800254 sWakeDisplaySensorState = wake;
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700255
256 if (wake) {
257 proximityCheckThenCall((result) -> {
258 if (result == ProximityCheck.RESULT_NEAR) {
259 // In pocket, drop event.
260 return;
261 }
Lucas Dupin5131f512019-02-01 12:57:19 -0800262 if (state == DozeMachine.State.DOZE) {
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700263 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Steven Wucef2d5d2019-04-23 13:27:33 -0400264 // Logs AOD open due to sensor wake up.
265 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
266 .setType(MetricsEvent.TYPE_OPEN)
267 .setSubtype(DozeLog.REASON_SENSOR_WAKE_UP));
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700268 }
269 }, false /* alreadyPerformedProxCheck */, DozeLog.REASON_SENSOR_WAKE_UP);
270 } else {
Lucas Dupinb7fd1eb2019-03-28 12:05:17 -0700271 boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
272 boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700273 if (!pausing && !paused) {
Lucas Dupin65104382018-12-04 11:53:42 -0800274 mMachine.requestState(DozeMachine.State.DOZE);
Steven Wucef2d5d2019-04-23 13:27:33 -0400275 // Logs AOD close due to sensor wake up.
276 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
277 .setType(MetricsEvent.TYPE_CLOSE)
278 .setSubtype(DozeLog.REASON_SENSOR_WAKE_UP));
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700279 }
280 }
281 }
282
Adrian Roosff2c4562016-11-03 12:13:36 -0700283 @Override
284 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
285 switch (newState) {
286 case INITIALIZED:
287 mBroadcastReceiver.register(mContext);
288 mDozeHost.addCallback(mHostCallback);
lpeterac798f22019-02-12 15:15:22 +0800289 if (mDockManager != null) {
290 mDockManager.addListener(mDockEventListener);
291 }
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800292 mDozeSensors.requestTemporaryDisable();
Adrian Roosff2c4562016-11-03 12:13:36 -0700293 checkTriggersAtInit();
294 break;
295 case DOZE:
296 case DOZE_AOD:
Adrian Roos67cca742017-04-13 16:52:51 -0700297 mDozeSensors.setProxListening(newState != DozeMachine.State.DOZE);
Adrian Roose3ac6f82017-06-30 16:15:22 +0200298 mDozeSensors.setListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700299 mDozeSensors.setPaused(false);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800300 if (newState == DozeMachine.State.DOZE_AOD && !sWakeDisplaySensorState) {
Lucas Dupin5131f512019-02-01 12:57:19 -0800301 onWakeScreen(false, newState);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800302 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700303 break;
Adrian Roos5f7bee42017-06-27 18:49:23 +0200304 case DOZE_AOD_PAUSED:
Adrian Roos6023ccb2017-06-28 16:22:02 +0200305 case DOZE_AOD_PAUSING:
Adrian Roos5f7bee42017-06-27 18:49:23 +0200306 mDozeSensors.setProxListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700307 mDozeSensors.setPaused(true);
Adrian Roos5f7bee42017-06-27 18:49:23 +0200308 break;
Adrian Roos67cca742017-04-13 16:52:51 -0700309 case DOZE_PULSING:
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700310 case DOZE_PULSING_BRIGHT:
Adrian Roos98d31982017-08-02 20:50:16 +0200311 mDozeSensors.setTouchscreenSensorsListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700312 mDozeSensors.setProxListening(true);
Lucas Dupinfac2e8e2019-06-27 16:10:19 -0700313 mDozeSensors.setPaused(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700314 break;
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800315 case DOZE_PULSE_DONE:
316 mDozeSensors.requestTemporaryDisable();
317 break;
Adrian Roosff2c4562016-11-03 12:13:36 -0700318 case FINISH:
319 mBroadcastReceiver.unregister(mContext);
320 mDozeHost.removeCallback(mHostCallback);
lpeterac798f22019-02-12 15:15:22 +0800321 if (mDockManager != null) {
322 mDockManager.removeListener(mDockEventListener);
323 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700324 mDozeSensors.setListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700325 mDozeSensors.setProxListening(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700326 break;
327 default:
328 }
329 }
330
331 private void checkTriggersAtInit() {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200332 if (mUiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR
333 || mDozeHost.isPowerSaveActive()
Adrian Roos710a0b12017-07-07 19:02:34 +0200334 || mDozeHost.isBlockingDoze()
Adrian Roosf2d545e2017-07-05 16:45:42 +0200335 || !mDozeHost.isProvisioned()) {
336 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700337 }
338 }
339
340 private void requestPulse(final int reason, boolean performedProxCheck) {
341 Assert.isMainThread();
Lucas Dupin5f00fa52019-03-27 22:46:53 -0700342 mDozeHost.extendPulse(reason);
343
344 // When already pulsing we're allowed to show the wallpaper directly without
345 // requesting a new pulse.
346 if (mMachine.getState() == DozeMachine.State.DOZE_PULSING
347 && reason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN) {
348 mMachine.requestState(DozeMachine.State.DOZE_PULSING_BRIGHT);
349 return;
350 }
351
Adrian Roosf9d13f62016-11-08 15:42:20 -0800352 if (mPulsePending || !mAllowPulseTriggers || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700353 if (mAllowPulseTriggers) {
354 DozeLog.tracePulseDropped(mContext, mPulsePending, mMachine.getState(),
355 mDozeHost.isPulsingBlocked());
356 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700357 return;
358 }
359
360 mPulsePending = true;
Adrian Roosd32b3662017-06-27 14:48:50 +0200361 proximityCheckThenCall((result) -> {
362 if (result == ProximityCheck.RESULT_NEAR) {
363 // in pocket, abort pulse
364 mPulsePending = false;
365 } else {
366 // not in pocket, continue pulsing
Adrian Roosff2c4562016-11-03 12:13:36 -0700367 continuePulseRequest(reason);
368 }
Adrian Roosd32b3662017-06-27 14:48:50 +0200369 }, !mDozeParameters.getProxCheckBeforePulse() || performedProxCheck, reason);
Steven Wude353052019-03-12 13:49:23 -0400370
371 // Logs request pulse reason on AOD screen.
372 mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING)
373 .setType(MetricsEvent.TYPE_UPDATE).setSubtype(reason));
Adrian Roosff2c4562016-11-03 12:13:36 -0700374 }
375
376 private boolean canPulse() {
377 return mMachine.getState() == DozeMachine.State.DOZE
378 || mMachine.getState() == DozeMachine.State.DOZE_AOD;
379 }
380
381 private void continuePulseRequest(int reason) {
382 mPulsePending = false;
383 if (mDozeHost.isPulsingBlocked() || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700384 DozeLog.tracePulseDropped(mContext, mPulsePending, mMachine.getState(),
385 mDozeHost.isPulsingBlocked());
Adrian Roosff2c4562016-11-03 12:13:36 -0700386 return;
387 }
Adrian Roosd7b9d102017-04-28 15:42:58 -0700388 mMachine.requestPulse(reason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700389 }
390
391 @Override
392 public void dump(PrintWriter pw) {
393 pw.print(" notificationPulseTime=");
394 pw.println(Formatter.formatShortElapsedTime(mContext, mNotificationPulseTime));
395
396 pw.print(" pulsePending="); pw.println(mPulsePending);
397 pw.println("DozeSensors:");
398 mDozeSensors.dump(pw);
399 }
400
401 private abstract class ProximityCheck implements SensorEventListener, Runnable {
402 private static final int TIMEOUT_DELAY_MS = 500;
403
404 protected static final int RESULT_UNKNOWN = 0;
405 protected static final int RESULT_NEAR = 1;
406 protected static final int RESULT_FAR = 2;
Adrian Roosd32b3662017-06-27 14:48:50 +0200407 protected static final int RESULT_NOT_CHECKED = 3;
Adrian Roosff2c4562016-11-03 12:13:36 -0700408
409 private boolean mRegistered;
410 private boolean mFinished;
411 private float mMaxRange;
412
413 protected abstract void onProximityResult(int result);
414
415 public void check() {
416 Preconditions.checkState(!mFinished && !mRegistered);
417 final Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
418 if (sensor == null) {
419 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: No sensor found");
420 finishWithResult(RESULT_UNKNOWN);
421 return;
422 }
423 mDozeSensors.setDisableSensorsInterferingWithProximity(true);
424
425 mMaxRange = sensor.getMaximumRange();
426 mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL, 0,
427 mHandler);
428 mHandler.postDelayed(this, TIMEOUT_DELAY_MS);
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800429 mWakeLock.acquire(TAG);
Adrian Roosff2c4562016-11-03 12:13:36 -0700430 mRegistered = true;
431 }
432
433 @Override
434 public void onSensorChanged(SensorEvent event) {
435 if (event.values.length == 0) {
436 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: Event has no values!");
437 finishWithResult(RESULT_UNKNOWN);
438 } else {
439 if (DozeMachine.DEBUG) {
440 Log.d(TAG, "ProxCheck: Event: value=" + event.values[0] + " max=" + mMaxRange);
441 }
442 final boolean isNear = event.values[0] < mMaxRange;
443 finishWithResult(isNear ? RESULT_NEAR : RESULT_FAR);
444 }
445 }
446
447 @Override
448 public void run() {
449 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: No event received before timeout");
450 finishWithResult(RESULT_UNKNOWN);
451 }
452
453 private void finishWithResult(int result) {
454 if (mFinished) return;
455 boolean wasRegistered = mRegistered;
456 if (mRegistered) {
457 mHandler.removeCallbacks(this);
458 mSensorManager.unregisterListener(this);
459 mDozeSensors.setDisableSensorsInterferingWithProximity(false);
460 mRegistered = false;
461 }
462 onProximityResult(result);
463 if (wasRegistered) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800464 mWakeLock.release(TAG);
Adrian Roosff2c4562016-11-03 12:13:36 -0700465 }
466 mFinished = true;
467 }
468
469 @Override
470 public void onAccuracyChanged(Sensor sensor, int accuracy) {
471 // noop
472 }
473 }
474
475 private class TriggerReceiver extends BroadcastReceiver {
Adrian Roos67cca742017-04-13 16:52:51 -0700476 private boolean mRegistered;
477
Adrian Roosff2c4562016-11-03 12:13:36 -0700478 @Override
479 public void onReceive(Context context, Intent intent) {
480 if (PULSE_ACTION.equals(intent.getAction())) {
481 if (DozeMachine.DEBUG) Log.d(TAG, "Received pulse intent");
482 requestPulse(DozeLog.PULSE_REASON_INTENT, false /* performedProxCheck */);
483 }
484 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200485 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700486 }
487 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
488 mDozeSensors.onUserSwitched();
489 }
490 }
491
492 public void register(Context context) {
Adrian Roos67cca742017-04-13 16:52:51 -0700493 if (mRegistered) {
494 return;
495 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700496 IntentFilter filter = new IntentFilter(PULSE_ACTION);
497 filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
498 filter.addAction(Intent.ACTION_USER_SWITCHED);
499 context.registerReceiver(this, filter);
Adrian Roos67cca742017-04-13 16:52:51 -0700500 mRegistered = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700501 }
502
503 public void unregister(Context context) {
Adrian Roos67cca742017-04-13 16:52:51 -0700504 if (!mRegistered) {
505 return;
506 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700507 context.unregisterReceiver(this);
Adrian Roos67cca742017-04-13 16:52:51 -0700508 mRegistered = false;
Adrian Roosff2c4562016-11-03 12:13:36 -0700509 }
510 }
511
lpeter8a5f4702019-01-18 16:53:07 +0800512 private class DockEventListener implements DockManager.DockEventListener {
513 @Override
514 public void onEvent(int event) {
515 if (DEBUG) Log.d(TAG, "dock event = " + event);
516 switch (event) {
517 case DockManager.STATE_DOCKED:
518 case DockManager.STATE_DOCKED_HIDE:
519 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(true);
520 break;
521 case DockManager.STATE_NONE:
522 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(false);
523 break;
524 default:
525 // no-op
526 }
527 }
528 }
529
Adrian Roosff2c4562016-11-03 12:13:36 -0700530 private DozeHost.Callback mHostCallback = new DozeHost.Callback() {
531 @Override
Kevina97ea052018-09-11 13:53:18 -0700532 public void onNotificationAlerted() {
Adrian Roosff2c4562016-11-03 12:13:36 -0700533 onNotification();
534 }
535
536 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700537 public void onPowerSaveChanged(boolean active) {
538 if (active) {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200539 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700540 }
541 }
542 };
543}