Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.doze; |
| 18 | |
Lucas Dupin | b7fd1eb | 2019-03-28 12:05:17 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 20 | import android.app.AlarmManager; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 21 | import android.app.UiModeManager; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.content.res.Configuration; |
Issei Suzuki | ca19e6e | 2019-02-26 12:39:11 +0100 | [diff] [blame] | 27 | import android.hardware.display.AmbientDisplayConfiguration; |
Steven Wu | de35305 | 2019-03-12 13:49:23 -0400 | [diff] [blame] | 28 | import android.metrics.LogMaker; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 29 | import android.os.SystemClock; |
| 30 | import android.os.UserHandle; |
| 31 | import android.text.format.Formatter; |
| 32 | import android.util.Log; |
| 33 | |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 34 | import com.android.internal.annotations.VisibleForTesting; |
Steven Wu | de35305 | 2019-03-12 13:49:23 -0400 | [diff] [blame] | 35 | import com.android.internal.logging.MetricsLogger; |
thiruram | 70bc583 | 2020-04-29 15:57:45 -0700 | [diff] [blame] | 36 | import com.android.internal.logging.UiEvent; |
| 37 | import com.android.internal.logging.UiEventLogger; |
| 38 | import com.android.internal.logging.UiEventLoggerImpl; |
Steven Wu | de35305 | 2019-03-12 13:49:23 -0400 | [diff] [blame] | 39 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Steven Wu | de35305 | 2019-03-12 13:49:23 -0400 | [diff] [blame] | 40 | import com.android.systemui.Dependency; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 41 | import com.android.systemui.broadcast.BroadcastDispatcher; |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 42 | import com.android.systemui.dock.DockManager; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 43 | import com.android.systemui.statusbar.phone.DozeParameters; |
| 44 | import com.android.systemui.util.Assert; |
Dave Mankoff | dddcae9 | 2020-05-13 12:46:18 -0400 | [diff] [blame] | 45 | import com.android.systemui.util.concurrency.DelayableExecutor; |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 46 | import com.android.systemui.util.sensors.AsyncSensorManager; |
| 47 | import com.android.systemui.util.sensors.ProximitySensor; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 48 | import com.android.systemui.util.wakelock.WakeLock; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 49 | |
| 50 | import java.io.PrintWriter; |
thiruram | 70bc583 | 2020-04-29 15:57:45 -0700 | [diff] [blame] | 51 | import java.util.Optional; |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 52 | import java.util.function.Consumer; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Handles triggers for ambient state changes. |
| 56 | */ |
| 57 | public class DozeTriggers implements DozeMachine.Part { |
| 58 | |
| 59 | private static final String TAG = "DozeTriggers"; |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 60 | private static final boolean DEBUG = DozeService.DEBUG; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 61 | |
| 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 | |
thiruram | 70bc583 | 2020-04-29 15:57:45 -0700 | [diff] [blame] | 65 | private static final UiEventLogger UI_EVENT_LOGGER = new UiEventLoggerImpl(); |
| 66 | |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 67 | /** |
| 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 Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 73 | private static final int PROXIMITY_TIMEOUT_DELAY_MS = 500; |
| 74 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 75 | private final Context mContext; |
| 76 | private final DozeMachine mMachine; |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 77 | private final DozeLog mDozeLog; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 78 | private final DozeSensors mDozeSensors; |
| 79 | private final DozeHost mDozeHost; |
| 80 | private final AmbientDisplayConfiguration mConfig; |
| 81 | private final DozeParameters mDozeParameters; |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 82 | private final AsyncSensorManager mSensorManager; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 83 | private final WakeLock mWakeLock; |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 84 | private final boolean mAllowPulseTriggers; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 85 | private final UiModeManager mUiModeManager; |
| 86 | private final TriggerReceiver mBroadcastReceiver = new TriggerReceiver(); |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 87 | private final DockEventListener mDockEventListener = new DockEventListener(); |
| 88 | private final DockManager mDockManager; |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 89 | private final ProximitySensor.ProximityCheck mProxCheck; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 90 | private final BroadcastDispatcher mBroadcastDispatcher; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 91 | |
| 92 | private long mNotificationPulseTime; |
| 93 | private boolean mPulsePending; |
| 94 | |
Steven Wu | de35305 | 2019-03-12 13:49:23 -0400 | [diff] [blame] | 95 | private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 96 | |
thiruram | 70bc583 | 2020-04-29 15:57:45 -0700 | [diff] [blame] | 97 | @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 Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 153 | public DozeTriggers(Context context, DozeMachine machine, DozeHost dozeHost, |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 154 | AlarmManager alarmManager, AmbientDisplayConfiguration config, |
Dave Mankoff | dddcae9 | 2020-05-13 12:46:18 -0400 | [diff] [blame] | 155 | DozeParameters dozeParameters, AsyncSensorManager sensorManager, |
| 156 | DelayableExecutor delayableExecutor, WakeLock wakeLock, boolean allowPulseTriggers, |
| 157 | DockManager dockManager, ProximitySensor proximitySensor, |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 158 | DozeLog dozeLog, BroadcastDispatcher broadcastDispatcher) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 159 | mContext = context; |
| 160 | mMachine = machine; |
| 161 | mDozeHost = dozeHost; |
| 162 | mConfig = config; |
| 163 | mDozeParameters = dozeParameters; |
| 164 | mSensorManager = sensorManager; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 165 | mWakeLock = wakeLock; |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 166 | mAllowPulseTriggers = allowPulseTriggers; |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 167 | mDozeSensors = new DozeSensors(context, alarmManager, mSensorManager, dozeParameters, |
Dave Mankoff | dddcae9 | 2020-05-13 12:46:18 -0400 | [diff] [blame] | 168 | config, wakeLock, this::onSensor, this::onProximityFar, dozeLog, proximitySensor); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 169 | mUiModeManager = mContext.getSystemService(UiModeManager.class); |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 170 | mDockManager = dockManager; |
Dave Mankoff | dddcae9 | 2020-05-13 12:46:18 -0400 | [diff] [blame] | 171 | mProxCheck = new ProximitySensor.ProximityCheck(proximitySensor, delayableExecutor); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 172 | mDozeLog = dozeLog; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 173 | mBroadcastDispatcher = broadcastDispatcher; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Dave Mankoff | 52229d8 | 2020-04-27 16:55:50 -0400 | [diff] [blame] | 176 | @Override |
| 177 | public void destroy() { |
| 178 | mDozeSensors.destroy(); |
| 179 | } |
| 180 | |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 181 | private void onNotification(Runnable onPulseSuppressedListener) { |
Dave Mankoff | 15bbec6 | 2019-06-27 15:17:42 -0400 | [diff] [blame] | 182 | if (DozeMachine.DEBUG) { |
| 183 | Log.d(TAG, "requestNotificationPulse"); |
| 184 | } |
| 185 | if (!sWakeDisplaySensorState) { |
| 186 | Log.d(TAG, "Wake display false. Pulse denied."); |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 187 | runIfNotNull(onPulseSuppressedListener); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 188 | mDozeLog.tracePulseDropped("wakeDisplaySensor"); |
Dave Mankoff | 15bbec6 | 2019-06-27 15:17:42 -0400 | [diff] [blame] | 189 | return; |
| 190 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 191 | mNotificationPulseTime = SystemClock.elapsedRealtime(); |
Dave Mankoff | 15bbec6 | 2019-06-27 15:17:42 -0400 | [diff] [blame] | 192 | if (!mConfig.pulseOnNotificationEnabled(UserHandle.USER_CURRENT)) { |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 193 | runIfNotNull(onPulseSuppressedListener); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 194 | mDozeLog.tracePulseDropped("pulseOnNotificationsDisabled"); |
Dave Mankoff | 15bbec6 | 2019-06-27 15:17:42 -0400 | [diff] [blame] | 195 | return; |
| 196 | } |
Yogisha Dixit | 65f26b7 | 2020-02-07 17:30:49 +0000 | [diff] [blame] | 197 | if (mDozeHost.isDozeSuppressed()) { |
Yogisha Dixit | b6594de | 2020-01-15 19:30:23 +0000 | [diff] [blame] | 198 | runIfNotNull(onPulseSuppressedListener); |
| 199 | mDozeLog.tracePulseDropped("dozeSuppressed"); |
| 200 | return; |
| 201 | } |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 202 | requestPulse(DozeLog.PULSE_REASON_NOTIFICATION, false /* performedProxCheck */, |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 203 | onPulseSuppressedListener); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 204 | mDozeLog.traceNotificationPulse(); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 207 | private static void runIfNotNull(Runnable runnable) { |
| 208 | if (runnable != null) { |
| 209 | runnable.run(); |
| 210 | } |
| 211 | } |
| 212 | |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 213 | private void proximityCheckThenCall(Consumer<Boolean> callback, |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 214 | boolean alreadyPerformedProxCheck, |
Lucas Dupin | 323f9ff | 2018-08-27 16:55:56 -0700 | [diff] [blame] | 215 | int reason) { |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 216 | Boolean cachedProxNear = mDozeSensors.isProximityCurrentlyNear(); |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 217 | if (alreadyPerformedProxCheck) { |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 218 | callback.accept(null); |
| 219 | } else if (cachedProxNear != null) { |
| 220 | callback.accept(cachedProxNear); |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 221 | } else { |
| 222 | final long start = SystemClock.uptimeMillis(); |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 223 | mProxCheck.check(PROXIMITY_TIMEOUT_DELAY_MS, near -> { |
| 224 | final long end = SystemClock.uptimeMillis(); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 225 | mDozeLog.traceProximityResult( |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 226 | near == null ? false : near, |
| 227 | end - start, |
| 228 | reason); |
| 229 | callback.accept(near); |
| 230 | mWakeLock.release(TAG); |
| 231 | }); |
| 232 | mWakeLock.acquire(TAG); |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 236 | @VisibleForTesting |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 237 | void onSensor(int pulseReason, float screenX, float screenY, float[] rawValues) { |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 238 | 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 Dupin | b2d9f48 | 2018-11-16 18:55:13 -0800 | [diff] [blame] | 244 | boolean wakeEvent = rawValues != null && rawValues.length > 0 && rawValues[0] != 0; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 245 | |
Lucas Dupin | b2d9f48 | 2018-11-16 18:55:13 -0800 | [diff] [blame] | 246 | if (isWakeDisplay) { |
Lucas Dupin | b7fd1eb | 2019-03-28 12:05:17 -0700 | [diff] [blame] | 247 | onWakeScreen(wakeEvent, mMachine.isExecutingTransition() ? null : mMachine.getState()); |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 248 | } else if (isLongPress) { |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 249 | requestPulse(pulseReason, true /* alreadyPerformedProxCheck */, |
| 250 | null /* onPulseSupressedListener */); |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 251 | } else if (isWakeLockScreen) { |
| 252 | if (wakeEvent) { |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 253 | requestPulse(pulseReason, true /* alreadyPerformedProxCheck */, |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 254 | null /* onPulseSupressedListener */); |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 255 | } |
Lucas Dupin | 4359b55 | 2018-08-09 15:07:54 -0700 | [diff] [blame] | 256 | } else { |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 257 | proximityCheckThenCall((result) -> { |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 258 | if (result != null && result) { |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 259 | // In pocket, drop event. |
| 260 | return; |
| 261 | } |
Lucas Dupin | d43bf70 | 2019-01-15 13:40:42 -0800 | [diff] [blame] | 262 | if (isDoubleTap || isTap) { |
| 263 | if (screenX != -1 && screenY != -1) { |
| 264 | mDozeHost.onSlpiTap(screenX, screenY); |
| 265 | } |
Lucas Dupin | ed5b7a9 | 2019-03-20 11:00:27 -0700 | [diff] [blame] | 266 | gentleWakeUp(pulseReason); |
Lucas Dupin | b2d9f48 | 2018-11-16 18:55:13 -0800 | [diff] [blame] | 267 | } else if (isPickup) { |
Lucas Dupin | ed5b7a9 | 2019-03-20 11:00:27 -0700 | [diff] [blame] | 268 | gentleWakeUp(pulseReason); |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 269 | } else { |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 270 | mDozeHost.extendPulse(pulseReason); |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 271 | } |
Lucas Dupin | f40bd8f | 2019-08-07 15:55:00 -0700 | [diff] [blame] | 272 | }, true /* alreadyPerformedProxCheck */, pulseReason); |
Adrian Roos | ed85e58 | 2017-04-27 15:09:28 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | if (isPickup) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 276 | final long timeSinceNotification = |
| 277 | SystemClock.elapsedRealtime() - mNotificationPulseTime; |
| 278 | final boolean withinVibrationThreshold = |
| 279 | timeSinceNotification < mDozeParameters.getPickupVibrationThreshold(); |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 280 | mDozeLog.tracePickupWakeUp(withinVibrationThreshold); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Lucas Dupin | ed5b7a9 | 2019-03-20 11:00:27 -0700 | [diff] [blame] | 284 | 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)); |
thiruram | 70bc583 | 2020-04-29 15:57:45 -0700 | [diff] [blame] | 289 | Optional.ofNullable(DozingUpdateUiEvent.fromReason(reason)) |
| 290 | .ifPresent(UI_EVENT_LOGGER::log); |
Lucas Dupin | ed5b7a9 | 2019-03-20 11:00:27 -0700 | [diff] [blame] | 291 | 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 Dupin | 34306c3 | 2019-07-16 11:56:53 -0700 | [diff] [blame] | 295 | mDozeHost.setAodDimmingScrim(1f); |
Lucas Dupin | ed5b7a9 | 2019-03-20 11:00:27 -0700 | [diff] [blame] | 296 | } |
| 297 | mMachine.wakeUp(); |
| 298 | } |
| 299 | |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 300 | private void onProximityFar(boolean far) { |
Lucas Dupin | 1946b31 | 2019-03-21 14:48:23 -0700 | [diff] [blame] | 301 | // 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 Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 309 | final boolean near = !far; |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 310 | final DozeMachine.State state = mMachine.getState(); |
Adrian Roos | c7fd696 | 2017-09-06 16:46:46 +0200 | [diff] [blame] | 311 | 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 Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 314 | |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 315 | if (state == DozeMachine.State.DOZE_PULSING |
| 316 | || state == DozeMachine.State.DOZE_PULSING_BRIGHT) { |
Adrian Roos | a6c03f8 | 2017-07-26 16:20:30 +0200 | [diff] [blame] | 317 | boolean ignoreTouch = near; |
Dave Mankoff | 15bbec6 | 2019-06-27 15:17:42 -0400 | [diff] [blame] | 318 | if (DEBUG) { |
| 319 | Log.i(TAG, "Prox changed, ignore touch = " + ignoreTouch); |
| 320 | } |
Adrian Roos | a6c03f8 | 2017-07-26 16:20:30 +0200 | [diff] [blame] | 321 | mDozeHost.onIgnoreTouchWhilePulsing(ignoreTouch); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 322 | } |
Lucas Dupin | 6510438 | 2018-12-04 11:53:42 -0800 | [diff] [blame] | 323 | |
Adrian Roos | c7fd696 | 2017-09-06 16:46:46 +0200 | [diff] [blame] | 324 | if (far && (paused || pausing)) { |
Dave Mankoff | 15bbec6 | 2019-06-27 15:17:42 -0400 | [diff] [blame] | 325 | if (DEBUG) { |
| 326 | Log.i(TAG, "Prox FAR, unpausing AOD"); |
| 327 | } |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 328 | mMachine.requestState(DozeMachine.State.DOZE_AOD); |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 329 | } else if (near && aod) { |
Dave Mankoff | 15bbec6 | 2019-06-27 15:17:42 -0400 | [diff] [blame] | 330 | if (DEBUG) { |
| 331 | Log.i(TAG, "Prox NEAR, pausing AOD"); |
| 332 | } |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 333 | mMachine.requestState(DozeMachine.State.DOZE_AOD_PAUSING); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Lucas Dupin | b7fd1eb | 2019-03-28 12:05:17 -0700 | [diff] [blame] | 337 | /** |
| 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) { |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 344 | mDozeLog.traceWakeDisplay(wake); |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 345 | sWakeDisplaySensorState = wake; |
Lucas Dupin | 323f9ff | 2018-08-27 16:55:56 -0700 | [diff] [blame] | 346 | |
| 347 | if (wake) { |
| 348 | proximityCheckThenCall((result) -> { |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 349 | if (result != null && result) { |
Lucas Dupin | 323f9ff | 2018-08-27 16:55:56 -0700 | [diff] [blame] | 350 | // In pocket, drop event. |
| 351 | return; |
| 352 | } |
Lucas Dupin | 5131f51 | 2019-02-01 12:57:19 -0800 | [diff] [blame] | 353 | if (state == DozeMachine.State.DOZE) { |
Lucas Dupin | 323f9ff | 2018-08-27 16:55:56 -0700 | [diff] [blame] | 354 | mMachine.requestState(DozeMachine.State.DOZE_AOD); |
Steven Wu | cef2d5d | 2019-04-23 13:27:33 -0400 | [diff] [blame] | 355 | // Logs AOD open due to sensor wake up. |
| 356 | mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING) |
| 357 | .setType(MetricsEvent.TYPE_OPEN) |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 358 | .setSubtype(DozeLog.REASON_SENSOR_WAKE_UP)); |
Lucas Dupin | 323f9ff | 2018-08-27 16:55:56 -0700 | [diff] [blame] | 359 | } |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 360 | }, true /* alreadyPerformedProxCheck */, DozeLog.REASON_SENSOR_WAKE_UP); |
Lucas Dupin | 323f9ff | 2018-08-27 16:55:56 -0700 | [diff] [blame] | 361 | } else { |
Lucas Dupin | b7fd1eb | 2019-03-28 12:05:17 -0700 | [diff] [blame] | 362 | boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED); |
| 363 | boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING); |
Lucas Dupin | 323f9ff | 2018-08-27 16:55:56 -0700 | [diff] [blame] | 364 | if (!pausing && !paused) { |
Lucas Dupin | 6510438 | 2018-12-04 11:53:42 -0800 | [diff] [blame] | 365 | mMachine.requestState(DozeMachine.State.DOZE); |
Steven Wu | cef2d5d | 2019-04-23 13:27:33 -0400 | [diff] [blame] | 366 | // Logs AOD close due to sensor wake up. |
| 367 | mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING) |
| 368 | .setType(MetricsEvent.TYPE_CLOSE) |
Ned Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 369 | .setSubtype(DozeLog.REASON_SENSOR_WAKE_UP)); |
Lucas Dupin | 323f9ff | 2018-08-27 16:55:56 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 374 | @Override |
| 375 | public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) { |
| 376 | switch (newState) { |
| 377 | case INITIALIZED: |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 378 | mBroadcastReceiver.register(mBroadcastDispatcher); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 379 | mDozeHost.addCallback(mHostCallback); |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 380 | mDockManager.addListener(mDockEventListener); |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 381 | mDozeSensors.requestTemporaryDisable(); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 382 | checkTriggersAtInit(); |
| 383 | break; |
| 384 | case DOZE: |
| 385 | case DOZE_AOD: |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 386 | mDozeSensors.setProxListening(newState != DozeMachine.State.DOZE); |
Adrian Roos | e3ac6f8 | 2017-06-30 16:15:22 +0200 | [diff] [blame] | 387 | mDozeSensors.setListening(true); |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 388 | mDozeSensors.setPaused(false); |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 389 | if (newState == DozeMachine.State.DOZE_AOD && !sWakeDisplaySensorState) { |
Lucas Dupin | 5131f51 | 2019-02-01 12:57:19 -0800 | [diff] [blame] | 390 | onWakeScreen(false, newState); |
Lucas Dupin | 1ae6cf9 | 2018-12-14 18:06:38 -0800 | [diff] [blame] | 391 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 392 | break; |
Adrian Roos | 5f7bee4 | 2017-06-27 18:49:23 +0200 | [diff] [blame] | 393 | case DOZE_AOD_PAUSED: |
Adrian Roos | 6023ccb | 2017-06-28 16:22:02 +0200 | [diff] [blame] | 394 | case DOZE_AOD_PAUSING: |
Adrian Roos | 5f7bee4 | 2017-06-27 18:49:23 +0200 | [diff] [blame] | 395 | mDozeSensors.setProxListening(true); |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 396 | mDozeSensors.setPaused(true); |
Adrian Roos | 5f7bee4 | 2017-06-27 18:49:23 +0200 | [diff] [blame] | 397 | break; |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 398 | case DOZE_PULSING: |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 399 | case DOZE_PULSING_BRIGHT: |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 400 | case DOZE_AOD_DOCKED: |
Adrian Roos | 98d3198 | 2017-08-02 20:50:16 +0200 | [diff] [blame] | 401 | mDozeSensors.setTouchscreenSensorsListening(false); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 402 | mDozeSensors.setProxListening(true); |
Lucas Dupin | fac2e8e | 2019-06-27 16:10:19 -0700 | [diff] [blame] | 403 | mDozeSensors.setPaused(false); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 404 | break; |
Lucas Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 405 | case DOZE_PULSE_DONE: |
| 406 | mDozeSensors.requestTemporaryDisable(); |
Lucas Dupin | 3174c66 | 2019-07-15 15:49:54 -0700 | [diff] [blame] | 407 | // 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 Dupin | 8a13aa7 | 2019-02-22 12:45:21 -0800 | [diff] [blame] | 410 | break; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 411 | case FINISH: |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 412 | mBroadcastReceiver.unregister(mBroadcastDispatcher); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 413 | mDozeHost.removeCallback(mHostCallback); |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 414 | mDockManager.removeListener(mDockEventListener); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 415 | mDozeSensors.setListening(false); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 416 | mDozeSensors.setProxListening(false); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 417 | break; |
| 418 | default: |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | private void checkTriggersAtInit() { |
Adrian Roos | f2d545e | 2017-07-05 16:45:42 +0200 | [diff] [blame] | 423 | if (mUiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR |
Adrian Roos | 710a0b1 | 2017-07-07 19:02:34 +0200 | [diff] [blame] | 424 | || mDozeHost.isBlockingDoze() |
Adrian Roos | f2d545e | 2017-07-05 16:45:42 +0200 | [diff] [blame] | 425 | || !mDozeHost.isProvisioned()) { |
| 426 | mMachine.requestState(DozeMachine.State.FINISH); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 430 | private void requestPulse(final int reason, boolean performedProxCheck, |
| 431 | Runnable onPulseSuppressedListener) { |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 432 | Assert.isMainThread(); |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 433 | 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 Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 438 | && reason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN) { |
Lucas Dupin | 5f00fa5 | 2019-03-27 22:46:53 -0700 | [diff] [blame] | 439 | mMachine.requestState(DozeMachine.State.DOZE_PULSING_BRIGHT); |
| 440 | return; |
| 441 | } |
| 442 | |
Adrian Roos | f9d13f6 | 2016-11-08 15:42:20 -0800 | [diff] [blame] | 443 | if (mPulsePending || !mAllowPulseTriggers || !canPulse()) { |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 444 | if (mAllowPulseTriggers) { |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 445 | mDozeLog.tracePulseDropped(mPulsePending, mMachine.getState(), |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 446 | mDozeHost.isPulsingBlocked()); |
| 447 | } |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 448 | runIfNotNull(onPulseSuppressedListener); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | |
| 452 | mPulsePending = true; |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 453 | proximityCheckThenCall((result) -> { |
Dave Mankoff | bf52f4b | 2019-09-20 14:34:28 -0400 | [diff] [blame] | 454 | if (result != null && result) { |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 455 | // in pocket, abort pulse |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 456 | mDozeLog.tracePulseDropped("inPocket"); |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 457 | mPulsePending = false; |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 458 | runIfNotNull(onPulseSuppressedListener); |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 459 | } else { |
| 460 | // not in pocket, continue pulsing |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 461 | continuePulseRequest(reason); |
| 462 | } |
Adrian Roos | d32b366 | 2017-06-27 14:48:50 +0200 | [diff] [blame] | 463 | }, !mDozeParameters.getProxCheckBeforePulse() || performedProxCheck, reason); |
Steven Wu | de35305 | 2019-03-12 13:49:23 -0400 | [diff] [blame] | 464 | |
| 465 | // Logs request pulse reason on AOD screen. |
| 466 | mMetricsLogger.write(new LogMaker(MetricsEvent.DOZING) |
| 467 | .setType(MetricsEvent.TYPE_UPDATE).setSubtype(reason)); |
thiruram | 70bc583 | 2020-04-29 15:57:45 -0700 | [diff] [blame] | 468 | Optional.ofNullable(DozingUpdateUiEvent.fromReason(reason)) |
| 469 | .ifPresent(UI_EVENT_LOGGER::log); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | private boolean canPulse() { |
| 473 | return mMachine.getState() == DozeMachine.State.DOZE |
Jerry Chang | ae4dc4b | 2019-10-16 18:45:03 +0800 | [diff] [blame] | 474 | || mMachine.getState() == DozeMachine.State.DOZE_AOD |
| 475 | || mMachine.getState() == DozeMachine.State.DOZE_AOD_DOCKED; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | private void continuePulseRequest(int reason) { |
| 479 | mPulsePending = false; |
| 480 | if (mDozeHost.isPulsingBlocked() || !canPulse()) { |
Beverly | cc4a62f | 2019-09-26 14:55:28 -0400 | [diff] [blame] | 481 | mDozeLog.tracePulseDropped(mPulsePending, mMachine.getState(), |
Adrian Roos | d35d4ca | 2017-04-19 14:31:03 -0700 | [diff] [blame] | 482 | mDozeHost.isPulsingBlocked()); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 483 | return; |
| 484 | } |
Adrian Roos | d7b9d10 | 2017-04-28 15:42:58 -0700 | [diff] [blame] | 485 | mMachine.requestPulse(reason); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public void dump(PrintWriter pw) { |
| 490 | pw.print(" notificationPulseTime="); |
| 491 | pw.println(Formatter.formatShortElapsedTime(mContext, mNotificationPulseTime)); |
| 492 | |
Dave Mankoff | 63a1282 | 2019-09-16 14:38:06 -0400 | [diff] [blame] | 493 | pw.println(" pulsePending=" + mPulsePending); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 494 | pw.println("DozeSensors:"); |
| 495 | mDozeSensors.dump(pw); |
| 496 | } |
| 497 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 498 | private class TriggerReceiver extends BroadcastReceiver { |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 499 | private boolean mRegistered; |
| 500 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 501 | @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 Burns | 30d6770 | 2020-01-28 12:58:45 -0500 | [diff] [blame] | 505 | requestPulse(DozeLog.PULSE_REASON_INTENT, false, /* performedProxCheck */ |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 506 | null /* onPulseSupressedListener */); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 507 | } |
| 508 | if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) { |
Adrian Roos | f2d545e | 2017-07-05 16:45:42 +0200 | [diff] [blame] | 509 | mMachine.requestState(DozeMachine.State.FINISH); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 510 | } |
| 511 | if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) { |
| 512 | mDozeSensors.onUserSwitched(); |
| 513 | } |
| 514 | } |
| 515 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 516 | public void register(BroadcastDispatcher broadcastDispatcher) { |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 517 | if (mRegistered) { |
| 518 | return; |
| 519 | } |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 520 | IntentFilter filter = new IntentFilter(PULSE_ACTION); |
| 521 | filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE); |
| 522 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 523 | broadcastDispatcher.registerReceiver(this, filter); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 524 | mRegistered = true; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 527 | public void unregister(BroadcastDispatcher broadcastDispatcher) { |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 528 | if (!mRegistered) { |
| 529 | return; |
| 530 | } |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 531 | broadcastDispatcher.unregisterReceiver(this); |
Adrian Roos | 67cca74 | 2017-04-13 16:52:51 -0700 | [diff] [blame] | 532 | mRegistered = false; |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
lpeter | 8a5f470 | 2019-01-18 16:53:07 +0800 | [diff] [blame] | 536 | 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 Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 554 | private DozeHost.Callback mHostCallback = new DozeHost.Callback() { |
| 555 | @Override |
Selim Cinek | 65c96f2 | 2019-07-25 20:09:04 -0700 | [diff] [blame] | 556 | public void onNotificationAlerted(Runnable onPulseSuppressedListener) { |
| 557 | onNotification(onPulseSuppressedListener); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | @Override |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 561 | public void onPowerSaveChanged(boolean active) { |
Lucas Dupin | bd7366d | 2019-09-25 13:39:21 -0700 | [diff] [blame] | 562 | if (mDozeHost.isPowerSaveActive()) { |
| 563 | mMachine.requestState(DozeMachine.State.DOZE); |
Lucas Dupin | abe15d4 | 2020-03-24 18:14:35 -0700 | [diff] [blame] | 564 | } else if (mMachine.getState() == DozeMachine.State.DOZE |
| 565 | && mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT)) { |
| 566 | mMachine.requestState(DozeMachine.State.DOZE_AOD); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
Yogisha Dixit | 65f26b7 | 2020-02-07 17:30:49 +0000 | [diff] [blame] | 569 | |
| 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 Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 580 | }; |
| 581 | } |