blob: dc505b57eb6b69b7bef84c7e839a5400d77ba686 [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
Adrian Roos6023ccb2017-06-28 16:22:02 +020019import android.app.AlarmManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070020import android.app.UiModeManager;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.res.Configuration;
26import android.hardware.Sensor;
27import android.hardware.SensorEvent;
28import android.hardware.SensorEventListener;
29import android.hardware.SensorManager;
30import android.os.Handler;
31import android.os.SystemClock;
32import android.os.UserHandle;
33import android.text.format.Formatter;
34import android.util.Log;
35
lpeter8a5f4702019-01-18 16:53:07 +080036import com.android.internal.annotations.VisibleForTesting;
Adrian Roosff2c4562016-11-03 12:13:36 -070037import com.android.internal.hardware.AmbientDisplayConfiguration;
38import com.android.internal.util.Preconditions;
lpeter8a5f4702019-01-18 16:53:07 +080039import com.android.systemui.dock.DockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070040import com.android.systemui.statusbar.phone.DozeParameters;
41import com.android.systemui.util.Assert;
Adrian Roosc1b50322017-02-27 21:07:58 +010042import com.android.systemui.util.wakelock.WakeLock;
Adrian Roosff2c4562016-11-03 12:13:36 -070043
44import java.io.PrintWriter;
Adrian Roosd32b3662017-06-27 14:48:50 +020045import java.util.function.IntConsumer;
Adrian Roosff2c4562016-11-03 12:13:36 -070046
47/**
48 * Handles triggers for ambient state changes.
49 */
50public class DozeTriggers implements DozeMachine.Part {
51
52 private static final String TAG = "DozeTriggers";
Adrian Roos67cca742017-04-13 16:52:51 -070053 private static final boolean DEBUG = DozeService.DEBUG;
Adrian Roosff2c4562016-11-03 12:13:36 -070054
55 /** adb shell am broadcast -a com.android.systemui.doze.pulse com.android.systemui */
56 private static final String PULSE_ACTION = "com.android.systemui.doze.pulse";
57
Lucas Dupin1ae6cf92018-12-14 18:06:38 -080058 /**
59 * Last value sent by the wake-display sensor.
60 * Assuming that the screen should start on.
61 */
62 private static boolean sWakeDisplaySensorState = true;
63
Adrian Roosff2c4562016-11-03 12:13:36 -070064 private final Context mContext;
65 private final DozeMachine mMachine;
66 private final DozeSensors mDozeSensors;
67 private final DozeHost mDozeHost;
68 private final AmbientDisplayConfiguration mConfig;
69 private final DozeParameters mDozeParameters;
70 private final SensorManager mSensorManager;
71 private final Handler mHandler;
Adrian Roosc1b50322017-02-27 21:07:58 +010072 private final WakeLock mWakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -080073 private final boolean mAllowPulseTriggers;
Adrian Roosff2c4562016-11-03 12:13:36 -070074 private final UiModeManager mUiModeManager;
75 private final TriggerReceiver mBroadcastReceiver = new TriggerReceiver();
lpeter8a5f4702019-01-18 16:53:07 +080076 private final DockEventListener mDockEventListener = new DockEventListener();
77 private final DockManager mDockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070078
79 private long mNotificationPulseTime;
80 private boolean mPulsePending;
81
82
83 public DozeTriggers(Context context, DozeMachine machine, DozeHost dozeHost,
Adrian Roos6023ccb2017-06-28 16:22:02 +020084 AlarmManager alarmManager, AmbientDisplayConfiguration config,
Adrian Roosff2c4562016-11-03 12:13:36 -070085 DozeParameters dozeParameters, SensorManager sensorManager, Handler handler,
lpeter8a5f4702019-01-18 16:53:07 +080086 WakeLock wakeLock, boolean allowPulseTriggers, DockManager dockManager) {
Adrian Roosff2c4562016-11-03 12:13:36 -070087 mContext = context;
88 mMachine = machine;
89 mDozeHost = dozeHost;
90 mConfig = config;
91 mDozeParameters = dozeParameters;
92 mSensorManager = sensorManager;
93 mHandler = handler;
94 mWakeLock = wakeLock;
Adrian Roosf9d13f62016-11-08 15:42:20 -080095 mAllowPulseTriggers = allowPulseTriggers;
Adrian Roos6023ccb2017-06-28 16:22:02 +020096 mDozeSensors = new DozeSensors(context, alarmManager, mSensorManager, dozeParameters,
Lucas Dupinb2d9f482018-11-16 18:55:13 -080097 config, wakeLock, this::onSensor, this::onProximityFar,
Adrian Roos2f5a3852018-04-23 17:48:08 +020098 dozeParameters.getPolicy());
Adrian Roosff2c4562016-11-03 12:13:36 -070099 mUiModeManager = mContext.getSystemService(UiModeManager.class);
lpeter8a5f4702019-01-18 16:53:07 +0800100 mDockManager = dockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -0700101 }
102
103 private void onNotification() {
104 if (DozeMachine.DEBUG) Log.d(TAG, "requestNotificationPulse");
105 mNotificationPulseTime = SystemClock.elapsedRealtime();
106 if (!mConfig.pulseOnNotificationEnabled(UserHandle.USER_CURRENT)) return;
107 requestPulse(DozeLog.PULSE_REASON_NOTIFICATION, false /* performedProxCheck */);
Adrian Roos181c3202016-11-07 13:24:31 -0800108 DozeLog.traceNotificationPulse(mContext);
Adrian Roosff2c4562016-11-03 12:13:36 -0700109 }
110
111 private void onWhisper() {
112 requestPulse(DozeLog.PULSE_REASON_NOTIFICATION, false /* performedProxCheck */);
113 }
114
Adrian Roosd32b3662017-06-27 14:48:50 +0200115 private void proximityCheckThenCall(IntConsumer callback,
116 boolean alreadyPerformedProxCheck,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700117 int reason) {
Adrian Roos70da03a2017-07-24 16:42:57 +0200118 Boolean cachedProxFar = mDozeSensors.isProximityCurrentlyFar();
Adrian Roosd32b3662017-06-27 14:48:50 +0200119 if (alreadyPerformedProxCheck) {
120 callback.accept(ProximityCheck.RESULT_NOT_CHECKED);
Adrian Roos70da03a2017-07-24 16:42:57 +0200121 } else if (cachedProxFar != null) {
122 callback.accept(cachedProxFar ? ProximityCheck.RESULT_FAR : ProximityCheck.RESULT_NEAR);
Adrian Roosd32b3662017-06-27 14:48:50 +0200123 } else {
124 final long start = SystemClock.uptimeMillis();
125 new ProximityCheck() {
126 @Override
127 public void onProximityResult(int result) {
128 final long end = SystemClock.uptimeMillis();
129 DozeLog.traceProximityResult(mContext, result == RESULT_NEAR,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700130 end - start, reason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200131 callback.accept(result);
132 }
133 }.check();
134 }
135 }
136
lpeter8a5f4702019-01-18 16:53:07 +0800137 @VisibleForTesting
138 void onSensor(int pulseReason, boolean sensorPerformedProxCheck,
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800139 float screenX, float screenY, float[] rawValues) {
Adrian Roosed85e582017-04-27 15:09:28 -0700140 boolean isDoubleTap = pulseReason == DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP;
Lucas Dupind43bf702019-01-15 13:40:42 -0800141 boolean isTap = pulseReason == DozeLog.PULSE_REASON_SENSOR_TAP;
Adrian Roosed85e582017-04-27 15:09:28 -0700142 boolean isPickup = pulseReason == DozeLog.PULSE_REASON_SENSOR_PICKUP;
Adrian Roosd0963a02017-05-15 14:33:37 -0700143 boolean isLongPress = pulseReason == DozeLog.PULSE_REASON_SENSOR_LONG_PRESS;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800144 boolean isWakeDisplay = pulseReason == DozeLog.REASON_SENSOR_WAKE_UP;
Lucas Dupinde64ee02018-12-21 14:45:12 -0800145 boolean isWakeLockScreen = pulseReason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800146 boolean wakeEvent = rawValues != null && rawValues.length > 0 && rawValues[0] != 0;
Adrian Roosff2c4562016-11-03 12:13:36 -0700147
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800148 if (isWakeDisplay) {
149 onWakeScreen(wakeEvent);
Lucas Dupinde64ee02018-12-21 14:45:12 -0800150 } else if (isLongPress || isWakeLockScreen) {
Lucas Dupin4359b552018-08-09 15:07:54 -0700151 requestPulse(pulseReason, sensorPerformedProxCheck);
152 } else {
Adrian Roosd32b3662017-06-27 14:48:50 +0200153 proximityCheckThenCall((result) -> {
154 if (result == ProximityCheck.RESULT_NEAR) {
155 // In pocket, drop event.
156 return;
157 }
Lucas Dupind43bf702019-01-15 13:40:42 -0800158 if (isDoubleTap || isTap) {
159 if (screenX != -1 && screenY != -1) {
160 mDozeHost.onSlpiTap(screenX, screenY);
161 }
Adrian Roosd32b3662017-06-27 14:48:50 +0200162 mMachine.wakeUp();
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800163 } else if (isPickup) {
Lucas Dupin4359b552018-08-09 15:07:54 -0700164 mMachine.wakeUp();
Adrian Roosd32b3662017-06-27 14:48:50 +0200165 } else {
166 mDozeHost.extendPulse();
167 }
lpeter8a5f4702019-01-18 16:53:07 +0800168 }, sensorPerformedProxCheck || mDockManager.isDocked(), pulseReason);
Adrian Roosed85e582017-04-27 15:09:28 -0700169 }
170
171 if (isPickup) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700172 final long timeSinceNotification =
173 SystemClock.elapsedRealtime() - mNotificationPulseTime;
174 final boolean withinVibrationThreshold =
175 timeSinceNotification < mDozeParameters.getPickupVibrationThreshold();
Lucas Dupin4359b552018-08-09 15:07:54 -0700176 DozeLog.tracePickupWakeUp(mContext, withinVibrationThreshold);
Adrian Roosff2c4562016-11-03 12:13:36 -0700177 }
178 }
179
Adrian Roos67cca742017-04-13 16:52:51 -0700180 private void onProximityFar(boolean far) {
181 final boolean near = !far;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200182 final DozeMachine.State state = mMachine.getState();
Adrian Roosc7fd6962017-09-06 16:46:46 +0200183 final boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
184 final boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
185 final boolean aod = (state == DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200186
Adrian Roosa6c03f82017-07-26 16:20:30 +0200187 if (state == DozeMachine.State.DOZE_PULSING) {
188 boolean ignoreTouch = near;
189 if (DEBUG) Log.i(TAG, "Prox changed, ignore touch = " + ignoreTouch);
190 mDozeHost.onIgnoreTouchWhilePulsing(ignoreTouch);
Adrian Roos67cca742017-04-13 16:52:51 -0700191 }
Lucas Dupin65104382018-12-04 11:53:42 -0800192
Adrian Roosc7fd6962017-09-06 16:46:46 +0200193 if (far && (paused || pausing)) {
Adrian Roos67cca742017-04-13 16:52:51 -0700194 if (DEBUG) Log.i(TAG, "Prox FAR, unpausing AOD");
195 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200196 } else if (near && aod) {
Adrian Roos67cca742017-04-13 16:52:51 -0700197 if (DEBUG) Log.i(TAG, "Prox NEAR, pausing AOD");
Adrian Roos6023ccb2017-06-28 16:22:02 +0200198 mMachine.requestState(DozeMachine.State.DOZE_AOD_PAUSING);
Adrian Roos67cca742017-04-13 16:52:51 -0700199 }
200 }
201
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700202 private void onWakeScreen(boolean wake) {
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800203 DozeLog.traceWakeDisplay(wake);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700204 DozeMachine.State state = mMachine.getState();
205 boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
206 boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800207 sWakeDisplaySensorState = wake;
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700208
209 if (wake) {
210 proximityCheckThenCall((result) -> {
211 if (result == ProximityCheck.RESULT_NEAR) {
212 // In pocket, drop event.
213 return;
214 }
Lucas Dupin65104382018-12-04 11:53:42 -0800215 if (mMachine.getState() == DozeMachine.State.DOZE) {
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700216 mMachine.requestState(DozeMachine.State.DOZE_AOD);
217 }
218 }, false /* alreadyPerformedProxCheck */, DozeLog.REASON_SENSOR_WAKE_UP);
219 } else {
220 if (!pausing && !paused) {
Lucas Dupin65104382018-12-04 11:53:42 -0800221 mMachine.requestState(DozeMachine.State.DOZE);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700222 }
223 }
224 }
225
Adrian Roosff2c4562016-11-03 12:13:36 -0700226 @Override
227 public void transitionTo(DozeMachine.State oldState, DozeMachine.State newState) {
228 switch (newState) {
229 case INITIALIZED:
230 mBroadcastReceiver.register(mContext);
231 mDozeHost.addCallback(mHostCallback);
lpeter8a5f4702019-01-18 16:53:07 +0800232 mDockManager.addListener(mDockEventListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700233 checkTriggersAtInit();
234 break;
235 case DOZE:
236 case DOZE_AOD:
Adrian Roos67cca742017-04-13 16:52:51 -0700237 mDozeSensors.setProxListening(newState != DozeMachine.State.DOZE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700238 if (oldState != DozeMachine.State.INITIALIZED) {
239 mDozeSensors.reregisterAllSensors();
240 }
Adrian Roose3ac6f82017-06-30 16:15:22 +0200241 mDozeSensors.setListening(true);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800242 if (newState == DozeMachine.State.DOZE_AOD && !sWakeDisplaySensorState) {
243 onWakeScreen(false);
244 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700245 break;
Adrian Roos5f7bee42017-06-27 18:49:23 +0200246 case DOZE_AOD_PAUSED:
Adrian Roos6023ccb2017-06-28 16:22:02 +0200247 case DOZE_AOD_PAUSING:
Adrian Roos5f7bee42017-06-27 18:49:23 +0200248 mDozeSensors.setProxListening(true);
249 mDozeSensors.setListening(false);
250 break;
Adrian Roos67cca742017-04-13 16:52:51 -0700251 case DOZE_PULSING:
Adrian Roos98d31982017-08-02 20:50:16 +0200252 mDozeSensors.setTouchscreenSensorsListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700253 mDozeSensors.setProxListening(true);
254 break;
Adrian Roosff2c4562016-11-03 12:13:36 -0700255 case FINISH:
256 mBroadcastReceiver.unregister(mContext);
257 mDozeHost.removeCallback(mHostCallback);
lpeter8a5f4702019-01-18 16:53:07 +0800258 mDockManager.removeListener(mDockEventListener);
Adrian Roosff2c4562016-11-03 12:13:36 -0700259 mDozeSensors.setListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700260 mDozeSensors.setProxListening(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700261 break;
262 default:
263 }
264 }
265
266 private void checkTriggersAtInit() {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200267 if (mUiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR
268 || mDozeHost.isPowerSaveActive()
Adrian Roos710a0b12017-07-07 19:02:34 +0200269 || mDozeHost.isBlockingDoze()
Adrian Roosf2d545e2017-07-05 16:45:42 +0200270 || !mDozeHost.isProvisioned()) {
271 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700272 }
273 }
274
275 private void requestPulse(final int reason, boolean performedProxCheck) {
276 Assert.isMainThread();
Adrian Roos67cca742017-04-13 16:52:51 -0700277 mDozeHost.extendPulse();
Adrian Roosf9d13f62016-11-08 15:42:20 -0800278 if (mPulsePending || !mAllowPulseTriggers || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700279 if (mAllowPulseTriggers) {
280 DozeLog.tracePulseDropped(mContext, mPulsePending, mMachine.getState(),
281 mDozeHost.isPulsingBlocked());
282 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700283 return;
284 }
285
286 mPulsePending = true;
Adrian Roosd32b3662017-06-27 14:48:50 +0200287 proximityCheckThenCall((result) -> {
288 if (result == ProximityCheck.RESULT_NEAR) {
289 // in pocket, abort pulse
290 mPulsePending = false;
291 } else {
292 // not in pocket, continue pulsing
Adrian Roosff2c4562016-11-03 12:13:36 -0700293 continuePulseRequest(reason);
294 }
Adrian Roosd32b3662017-06-27 14:48:50 +0200295 }, !mDozeParameters.getProxCheckBeforePulse() || performedProxCheck, reason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700296 }
297
298 private boolean canPulse() {
299 return mMachine.getState() == DozeMachine.State.DOZE
300 || mMachine.getState() == DozeMachine.State.DOZE_AOD;
301 }
302
303 private void continuePulseRequest(int reason) {
304 mPulsePending = false;
305 if (mDozeHost.isPulsingBlocked() || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700306 DozeLog.tracePulseDropped(mContext, mPulsePending, mMachine.getState(),
307 mDozeHost.isPulsingBlocked());
Adrian Roosff2c4562016-11-03 12:13:36 -0700308 return;
309 }
Adrian Roosd7b9d102017-04-28 15:42:58 -0700310 mMachine.requestPulse(reason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700311 }
312
313 @Override
314 public void dump(PrintWriter pw) {
315 pw.print(" notificationPulseTime=");
316 pw.println(Formatter.formatShortElapsedTime(mContext, mNotificationPulseTime));
317
318 pw.print(" pulsePending="); pw.println(mPulsePending);
319 pw.println("DozeSensors:");
320 mDozeSensors.dump(pw);
321 }
322
323 private abstract class ProximityCheck implements SensorEventListener, Runnable {
324 private static final int TIMEOUT_DELAY_MS = 500;
325
326 protected static final int RESULT_UNKNOWN = 0;
327 protected static final int RESULT_NEAR = 1;
328 protected static final int RESULT_FAR = 2;
Adrian Roosd32b3662017-06-27 14:48:50 +0200329 protected static final int RESULT_NOT_CHECKED = 3;
Adrian Roosff2c4562016-11-03 12:13:36 -0700330
331 private boolean mRegistered;
332 private boolean mFinished;
333 private float mMaxRange;
334
335 protected abstract void onProximityResult(int result);
336
337 public void check() {
338 Preconditions.checkState(!mFinished && !mRegistered);
339 final Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
340 if (sensor == null) {
341 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: No sensor found");
342 finishWithResult(RESULT_UNKNOWN);
343 return;
344 }
345 mDozeSensors.setDisableSensorsInterferingWithProximity(true);
346
347 mMaxRange = sensor.getMaximumRange();
348 mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL, 0,
349 mHandler);
350 mHandler.postDelayed(this, TIMEOUT_DELAY_MS);
351 mWakeLock.acquire();
352 mRegistered = true;
353 }
354
355 @Override
356 public void onSensorChanged(SensorEvent event) {
357 if (event.values.length == 0) {
358 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: Event has no values!");
359 finishWithResult(RESULT_UNKNOWN);
360 } else {
361 if (DozeMachine.DEBUG) {
362 Log.d(TAG, "ProxCheck: Event: value=" + event.values[0] + " max=" + mMaxRange);
363 }
364 final boolean isNear = event.values[0] < mMaxRange;
365 finishWithResult(isNear ? RESULT_NEAR : RESULT_FAR);
366 }
367 }
368
369 @Override
370 public void run() {
371 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: No event received before timeout");
372 finishWithResult(RESULT_UNKNOWN);
373 }
374
375 private void finishWithResult(int result) {
376 if (mFinished) return;
377 boolean wasRegistered = mRegistered;
378 if (mRegistered) {
379 mHandler.removeCallbacks(this);
380 mSensorManager.unregisterListener(this);
381 mDozeSensors.setDisableSensorsInterferingWithProximity(false);
382 mRegistered = false;
383 }
384 onProximityResult(result);
385 if (wasRegistered) {
386 mWakeLock.release();
387 }
388 mFinished = true;
389 }
390
391 @Override
392 public void onAccuracyChanged(Sensor sensor, int accuracy) {
393 // noop
394 }
395 }
396
397 private class TriggerReceiver extends BroadcastReceiver {
Adrian Roos67cca742017-04-13 16:52:51 -0700398 private boolean mRegistered;
399
Adrian Roosff2c4562016-11-03 12:13:36 -0700400 @Override
401 public void onReceive(Context context, Intent intent) {
402 if (PULSE_ACTION.equals(intent.getAction())) {
403 if (DozeMachine.DEBUG) Log.d(TAG, "Received pulse intent");
404 requestPulse(DozeLog.PULSE_REASON_INTENT, false /* performedProxCheck */);
405 }
406 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200407 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700408 }
409 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
410 mDozeSensors.onUserSwitched();
411 }
412 }
413
414 public void register(Context context) {
Adrian Roos67cca742017-04-13 16:52:51 -0700415 if (mRegistered) {
416 return;
417 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700418 IntentFilter filter = new IntentFilter(PULSE_ACTION);
419 filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
420 filter.addAction(Intent.ACTION_USER_SWITCHED);
421 context.registerReceiver(this, filter);
Adrian Roos67cca742017-04-13 16:52:51 -0700422 mRegistered = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700423 }
424
425 public void unregister(Context context) {
Adrian Roos67cca742017-04-13 16:52:51 -0700426 if (!mRegistered) {
427 return;
428 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700429 context.unregisterReceiver(this);
Adrian Roos67cca742017-04-13 16:52:51 -0700430 mRegistered = false;
Adrian Roosff2c4562016-11-03 12:13:36 -0700431 }
432 }
433
lpeter8a5f4702019-01-18 16:53:07 +0800434 private class DockEventListener implements DockManager.DockEventListener {
435 @Override
436 public void onEvent(int event) {
437 if (DEBUG) Log.d(TAG, "dock event = " + event);
438 switch (event) {
439 case DockManager.STATE_DOCKED:
440 case DockManager.STATE_DOCKED_HIDE:
441 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(true);
442 break;
443 case DockManager.STATE_NONE:
444 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(false);
445 break;
446 default:
447 // no-op
448 }
449 }
450 }
451
Adrian Roosff2c4562016-11-03 12:13:36 -0700452 private DozeHost.Callback mHostCallback = new DozeHost.Callback() {
453 @Override
Kevina97ea052018-09-11 13:53:18 -0700454 public void onNotificationAlerted() {
Adrian Roosff2c4562016-11-03 12:13:36 -0700455 onNotification();
456 }
457
458 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700459 public void onPowerSaveChanged(boolean active) {
460 if (active) {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200461 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700462 }
463 }
464 };
465}