blob: 3654aace2ba5024f611060cf87c61299bb32df90 [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;
Issei Suzukica19e6e2019-02-26 12:39:11 +010030import android.hardware.display.AmbientDisplayConfiguration;
Adrian Roosff2c4562016-11-03 12:13:36 -070031import android.os.Handler;
32import android.os.SystemClock;
33import android.os.UserHandle;
34import android.text.format.Formatter;
35import android.util.Log;
36
lpeter8a5f4702019-01-18 16:53:07 +080037import com.android.internal.annotations.VisibleForTesting;
Adrian Roosff2c4562016-11-03 12:13:36 -070038import 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
Adrian Roosd32b3662017-06-27 14:48:50 +0200111 private void proximityCheckThenCall(IntConsumer callback,
112 boolean alreadyPerformedProxCheck,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700113 int reason) {
Adrian Roos70da03a2017-07-24 16:42:57 +0200114 Boolean cachedProxFar = mDozeSensors.isProximityCurrentlyFar();
Adrian Roosd32b3662017-06-27 14:48:50 +0200115 if (alreadyPerformedProxCheck) {
116 callback.accept(ProximityCheck.RESULT_NOT_CHECKED);
Adrian Roos70da03a2017-07-24 16:42:57 +0200117 } else if (cachedProxFar != null) {
118 callback.accept(cachedProxFar ? ProximityCheck.RESULT_FAR : ProximityCheck.RESULT_NEAR);
Adrian Roosd32b3662017-06-27 14:48:50 +0200119 } else {
120 final long start = SystemClock.uptimeMillis();
121 new ProximityCheck() {
122 @Override
123 public void onProximityResult(int result) {
124 final long end = SystemClock.uptimeMillis();
125 DozeLog.traceProximityResult(mContext, result == RESULT_NEAR,
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700126 end - start, reason);
Adrian Roosd32b3662017-06-27 14:48:50 +0200127 callback.accept(result);
128 }
129 }.check();
130 }
131 }
132
lpeter8a5f4702019-01-18 16:53:07 +0800133 @VisibleForTesting
134 void onSensor(int pulseReason, boolean sensorPerformedProxCheck,
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800135 float screenX, float screenY, float[] rawValues) {
Lucas Dupin3d053532019-01-29 12:35:22 -0800136 boolean isDoubleTap = pulseReason == DozeLog.REASON_SENSOR_DOUBLE_TAP;
137 boolean isTap = pulseReason == DozeLog.REASON_SENSOR_TAP;
138 boolean isPickup = pulseReason == DozeLog.REASON_SENSOR_PICKUP;
Adrian Roosd0963a02017-05-15 14:33:37 -0700139 boolean isLongPress = pulseReason == DozeLog.PULSE_REASON_SENSOR_LONG_PRESS;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800140 boolean isWakeDisplay = pulseReason == DozeLog.REASON_SENSOR_WAKE_UP;
Lucas Dupinde64ee02018-12-21 14:45:12 -0800141 boolean isWakeLockScreen = pulseReason == DozeLog.PULSE_REASON_SENSOR_WAKE_LOCK_SCREEN;
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800142 boolean wakeEvent = rawValues != null && rawValues.length > 0 && rawValues[0] != 0;
Adrian Roosff2c4562016-11-03 12:13:36 -0700143
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800144 if (isWakeDisplay) {
Lucas Dupin5131f512019-02-01 12:57:19 -0800145 onWakeScreen(wakeEvent, mMachine.getState());
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800146 } else if (isLongPress) {
Lucas Dupin4359b552018-08-09 15:07:54 -0700147 requestPulse(pulseReason, sensorPerformedProxCheck);
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800148 } else if (isWakeLockScreen) {
149 if (wakeEvent) {
150 requestPulse(pulseReason, sensorPerformedProxCheck);
151 }
Lucas Dupin4359b552018-08-09 15:07:54 -0700152 } 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 }
lpeterac798f22019-02-12 15:15:22 +0800168 }, sensorPerformedProxCheck
169 || (mDockManager != null && mDockManager.isDocked()), pulseReason);
Adrian Roosed85e582017-04-27 15:09:28 -0700170 }
171
172 if (isPickup) {
Adrian Roosff2c4562016-11-03 12:13:36 -0700173 final long timeSinceNotification =
174 SystemClock.elapsedRealtime() - mNotificationPulseTime;
175 final boolean withinVibrationThreshold =
176 timeSinceNotification < mDozeParameters.getPickupVibrationThreshold();
Lucas Dupin4359b552018-08-09 15:07:54 -0700177 DozeLog.tracePickupWakeUp(mContext, withinVibrationThreshold);
Adrian Roosff2c4562016-11-03 12:13:36 -0700178 }
179 }
180
Adrian Roos67cca742017-04-13 16:52:51 -0700181 private void onProximityFar(boolean far) {
182 final boolean near = !far;
Adrian Roos6023ccb2017-06-28 16:22:02 +0200183 final DozeMachine.State state = mMachine.getState();
Adrian Roosc7fd6962017-09-06 16:46:46 +0200184 final boolean paused = (state == DozeMachine.State.DOZE_AOD_PAUSED);
185 final boolean pausing = (state == DozeMachine.State.DOZE_AOD_PAUSING);
186 final boolean aod = (state == DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200187
Adrian Roosa6c03f82017-07-26 16:20:30 +0200188 if (state == DozeMachine.State.DOZE_PULSING) {
189 boolean ignoreTouch = near;
190 if (DEBUG) Log.i(TAG, "Prox changed, ignore touch = " + ignoreTouch);
191 mDozeHost.onIgnoreTouchWhilePulsing(ignoreTouch);
Adrian Roos67cca742017-04-13 16:52:51 -0700192 }
Lucas Dupin65104382018-12-04 11:53:42 -0800193
Adrian Roosc7fd6962017-09-06 16:46:46 +0200194 if (far && (paused || pausing)) {
Adrian Roos67cca742017-04-13 16:52:51 -0700195 if (DEBUG) Log.i(TAG, "Prox FAR, unpausing AOD");
196 mMachine.requestState(DozeMachine.State.DOZE_AOD);
Adrian Roos6023ccb2017-06-28 16:22:02 +0200197 } else if (near && aod) {
Adrian Roos67cca742017-04-13 16:52:51 -0700198 if (DEBUG) Log.i(TAG, "Prox NEAR, pausing AOD");
Adrian Roos6023ccb2017-06-28 16:22:02 +0200199 mMachine.requestState(DozeMachine.State.DOZE_AOD_PAUSING);
Adrian Roos67cca742017-04-13 16:52:51 -0700200 }
201 }
202
Lucas Dupin5131f512019-02-01 12:57:19 -0800203 private void onWakeScreen(boolean wake, DozeMachine.State state) {
Lucas Dupinb2d9f482018-11-16 18:55:13 -0800204 DozeLog.traceWakeDisplay(wake);
Lucas Dupin323f9ff2018-08-27 16:55:56 -0700205 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 Dupin5131f512019-02-01 12:57:19 -0800215 if (state == 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);
lpeterac798f22019-02-12 15:15:22 +0800232 if (mDockManager != null) {
233 mDockManager.addListener(mDockEventListener);
234 }
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800235 mDozeSensors.requestTemporaryDisable();
Adrian Roosff2c4562016-11-03 12:13:36 -0700236 checkTriggersAtInit();
237 break;
238 case DOZE:
239 case DOZE_AOD:
Adrian Roos67cca742017-04-13 16:52:51 -0700240 mDozeSensors.setProxListening(newState != DozeMachine.State.DOZE);
Adrian Roosff2c4562016-11-03 12:13:36 -0700241 if (oldState != DozeMachine.State.INITIALIZED) {
242 mDozeSensors.reregisterAllSensors();
243 }
Adrian Roose3ac6f82017-06-30 16:15:22 +0200244 mDozeSensors.setListening(true);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800245 if (newState == DozeMachine.State.DOZE_AOD && !sWakeDisplaySensorState) {
Lucas Dupin5131f512019-02-01 12:57:19 -0800246 onWakeScreen(false, newState);
Lucas Dupin1ae6cf92018-12-14 18:06:38 -0800247 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700248 break;
Adrian Roos5f7bee42017-06-27 18:49:23 +0200249 case DOZE_AOD_PAUSED:
Adrian Roos6023ccb2017-06-28 16:22:02 +0200250 case DOZE_AOD_PAUSING:
Adrian Roos5f7bee42017-06-27 18:49:23 +0200251 mDozeSensors.setProxListening(true);
252 mDozeSensors.setListening(false);
253 break;
Adrian Roos67cca742017-04-13 16:52:51 -0700254 case DOZE_PULSING:
Adrian Roos98d31982017-08-02 20:50:16 +0200255 mDozeSensors.setTouchscreenSensorsListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700256 mDozeSensors.setProxListening(true);
257 break;
Lucas Dupin8a13aa72019-02-22 12:45:21 -0800258 case DOZE_PULSE_DONE:
259 mDozeSensors.requestTemporaryDisable();
260 break;
Adrian Roosff2c4562016-11-03 12:13:36 -0700261 case FINISH:
262 mBroadcastReceiver.unregister(mContext);
263 mDozeHost.removeCallback(mHostCallback);
lpeterac798f22019-02-12 15:15:22 +0800264 if (mDockManager != null) {
265 mDockManager.removeListener(mDockEventListener);
266 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700267 mDozeSensors.setListening(false);
Adrian Roos67cca742017-04-13 16:52:51 -0700268 mDozeSensors.setProxListening(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700269 break;
270 default:
271 }
272 }
273
274 private void checkTriggersAtInit() {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200275 if (mUiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR
276 || mDozeHost.isPowerSaveActive()
Adrian Roos710a0b12017-07-07 19:02:34 +0200277 || mDozeHost.isBlockingDoze()
Adrian Roosf2d545e2017-07-05 16:45:42 +0200278 || !mDozeHost.isProvisioned()) {
279 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700280 }
281 }
282
283 private void requestPulse(final int reason, boolean performedProxCheck) {
284 Assert.isMainThread();
Adrian Roos67cca742017-04-13 16:52:51 -0700285 mDozeHost.extendPulse();
Adrian Roosf9d13f62016-11-08 15:42:20 -0800286 if (mPulsePending || !mAllowPulseTriggers || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700287 if (mAllowPulseTriggers) {
288 DozeLog.tracePulseDropped(mContext, mPulsePending, mMachine.getState(),
289 mDozeHost.isPulsingBlocked());
290 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700291 return;
292 }
293
294 mPulsePending = true;
Adrian Roosd32b3662017-06-27 14:48:50 +0200295 proximityCheckThenCall((result) -> {
296 if (result == ProximityCheck.RESULT_NEAR) {
297 // in pocket, abort pulse
298 mPulsePending = false;
299 } else {
300 // not in pocket, continue pulsing
Adrian Roosff2c4562016-11-03 12:13:36 -0700301 continuePulseRequest(reason);
302 }
Adrian Roosd32b3662017-06-27 14:48:50 +0200303 }, !mDozeParameters.getProxCheckBeforePulse() || performedProxCheck, reason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700304 }
305
306 private boolean canPulse() {
307 return mMachine.getState() == DozeMachine.State.DOZE
308 || mMachine.getState() == DozeMachine.State.DOZE_AOD;
309 }
310
311 private void continuePulseRequest(int reason) {
312 mPulsePending = false;
313 if (mDozeHost.isPulsingBlocked() || !canPulse()) {
Adrian Roosd35d4ca2017-04-19 14:31:03 -0700314 DozeLog.tracePulseDropped(mContext, mPulsePending, mMachine.getState(),
315 mDozeHost.isPulsingBlocked());
Adrian Roosff2c4562016-11-03 12:13:36 -0700316 return;
317 }
Adrian Roosd7b9d102017-04-28 15:42:58 -0700318 mMachine.requestPulse(reason);
Adrian Roosff2c4562016-11-03 12:13:36 -0700319 }
320
321 @Override
322 public void dump(PrintWriter pw) {
323 pw.print(" notificationPulseTime=");
324 pw.println(Formatter.formatShortElapsedTime(mContext, mNotificationPulseTime));
325
326 pw.print(" pulsePending="); pw.println(mPulsePending);
327 pw.println("DozeSensors:");
328 mDozeSensors.dump(pw);
329 }
330
331 private abstract class ProximityCheck implements SensorEventListener, Runnable {
332 private static final int TIMEOUT_DELAY_MS = 500;
333
334 protected static final int RESULT_UNKNOWN = 0;
335 protected static final int RESULT_NEAR = 1;
336 protected static final int RESULT_FAR = 2;
Adrian Roosd32b3662017-06-27 14:48:50 +0200337 protected static final int RESULT_NOT_CHECKED = 3;
Adrian Roosff2c4562016-11-03 12:13:36 -0700338
339 private boolean mRegistered;
340 private boolean mFinished;
341 private float mMaxRange;
342
343 protected abstract void onProximityResult(int result);
344
345 public void check() {
346 Preconditions.checkState(!mFinished && !mRegistered);
347 final Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
348 if (sensor == null) {
349 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: No sensor found");
350 finishWithResult(RESULT_UNKNOWN);
351 return;
352 }
353 mDozeSensors.setDisableSensorsInterferingWithProximity(true);
354
355 mMaxRange = sensor.getMaximumRange();
356 mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL, 0,
357 mHandler);
358 mHandler.postDelayed(this, TIMEOUT_DELAY_MS);
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800359 mWakeLock.acquire(TAG);
Adrian Roosff2c4562016-11-03 12:13:36 -0700360 mRegistered = true;
361 }
362
363 @Override
364 public void onSensorChanged(SensorEvent event) {
365 if (event.values.length == 0) {
366 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: Event has no values!");
367 finishWithResult(RESULT_UNKNOWN);
368 } else {
369 if (DozeMachine.DEBUG) {
370 Log.d(TAG, "ProxCheck: Event: value=" + event.values[0] + " max=" + mMaxRange);
371 }
372 final boolean isNear = event.values[0] < mMaxRange;
373 finishWithResult(isNear ? RESULT_NEAR : RESULT_FAR);
374 }
375 }
376
377 @Override
378 public void run() {
379 if (DozeMachine.DEBUG) Log.d(TAG, "ProxCheck: No event received before timeout");
380 finishWithResult(RESULT_UNKNOWN);
381 }
382
383 private void finishWithResult(int result) {
384 if (mFinished) return;
385 boolean wasRegistered = mRegistered;
386 if (mRegistered) {
387 mHandler.removeCallbacks(this);
388 mSensorManager.unregisterListener(this);
389 mDozeSensors.setDisableSensorsInterferingWithProximity(false);
390 mRegistered = false;
391 }
392 onProximityResult(result);
393 if (wasRegistered) {
Lucas Dupinee4c9b72019-02-18 17:04:58 -0800394 mWakeLock.release(TAG);
Adrian Roosff2c4562016-11-03 12:13:36 -0700395 }
396 mFinished = true;
397 }
398
399 @Override
400 public void onAccuracyChanged(Sensor sensor, int accuracy) {
401 // noop
402 }
403 }
404
405 private class TriggerReceiver extends BroadcastReceiver {
Adrian Roos67cca742017-04-13 16:52:51 -0700406 private boolean mRegistered;
407
Adrian Roosff2c4562016-11-03 12:13:36 -0700408 @Override
409 public void onReceive(Context context, Intent intent) {
410 if (PULSE_ACTION.equals(intent.getAction())) {
411 if (DozeMachine.DEBUG) Log.d(TAG, "Received pulse intent");
412 requestPulse(DozeLog.PULSE_REASON_INTENT, false /* performedProxCheck */);
413 }
414 if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200415 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700416 }
417 if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
418 mDozeSensors.onUserSwitched();
419 }
420 }
421
422 public void register(Context context) {
Adrian Roos67cca742017-04-13 16:52:51 -0700423 if (mRegistered) {
424 return;
425 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700426 IntentFilter filter = new IntentFilter(PULSE_ACTION);
427 filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
428 filter.addAction(Intent.ACTION_USER_SWITCHED);
429 context.registerReceiver(this, filter);
Adrian Roos67cca742017-04-13 16:52:51 -0700430 mRegistered = true;
Adrian Roosff2c4562016-11-03 12:13:36 -0700431 }
432
433 public void unregister(Context context) {
Adrian Roos67cca742017-04-13 16:52:51 -0700434 if (!mRegistered) {
435 return;
436 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700437 context.unregisterReceiver(this);
Adrian Roos67cca742017-04-13 16:52:51 -0700438 mRegistered = false;
Adrian Roosff2c4562016-11-03 12:13:36 -0700439 }
440 }
441
lpeter8a5f4702019-01-18 16:53:07 +0800442 private class DockEventListener implements DockManager.DockEventListener {
443 @Override
444 public void onEvent(int event) {
445 if (DEBUG) Log.d(TAG, "dock event = " + event);
446 switch (event) {
447 case DockManager.STATE_DOCKED:
448 case DockManager.STATE_DOCKED_HIDE:
449 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(true);
450 break;
451 case DockManager.STATE_NONE:
452 mDozeSensors.ignoreTouchScreenSensorsSettingInterferingWithDocking(false);
453 break;
454 default:
455 // no-op
456 }
457 }
458 }
459
Adrian Roosff2c4562016-11-03 12:13:36 -0700460 private DozeHost.Callback mHostCallback = new DozeHost.Callback() {
461 @Override
Kevina97ea052018-09-11 13:53:18 -0700462 public void onNotificationAlerted() {
Adrian Roosff2c4562016-11-03 12:13:36 -0700463 onNotification();
464 }
465
466 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700467 public void onPowerSaveChanged(boolean active) {
468 if (active) {
Adrian Roosf2d545e2017-07-05 16:45:42 +0200469 mMachine.requestState(DozeMachine.State.FINISH);
Adrian Roosff2c4562016-11-03 12:13:36 -0700470 }
471 }
472 };
473}