blob: 3f0b8b0771c0df182840ddb8db4e651c0a689956 [file] [log] [blame]
Jeff Brown96307042012-07-27 15:51:34 -07001/*
2 * Copyright (C) 2012 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
Jeff Brown131206b2014-04-08 17:27:14 -070017package com.android.server.display;
Jeff Brown96307042012-07-27 15:51:34 -070018
Jeff Brown131206b2014-04-08 17:27:14 -070019import com.android.internal.app.IBatteryStats;
20import com.android.server.LocalServices;
21import com.android.server.am.BatteryStatsService;
Jeff Brown96307042012-07-27 15:51:34 -070022
23import android.animation.Animator;
24import android.animation.ObjectAnimator;
25import android.content.Context;
26import android.content.res.Resources;
27import android.hardware.Sensor;
28import android.hardware.SensorEvent;
29import android.hardware.SensorEventListener;
30import android.hardware.SensorManager;
Jeff Brown131206b2014-04-08 17:27:14 -070031import android.hardware.display.DisplayManagerInternal.DisplayPowerCallbacks;
32import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest;
Jeff Brown96307042012-07-27 15:51:34 -070033import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
Jeff Brown330560f2012-08-21 22:10:57 -070036import android.os.PowerManager;
Jeff Brown131206b2014-04-08 17:27:14 -070037import android.os.RemoteException;
Jeff Brown96307042012-07-27 15:51:34 -070038import android.os.SystemClock;
Jeff Brown3edf5272014-08-14 19:25:14 -070039import android.os.Trace;
Michael Wright41a5cdf2013-11-13 16:18:32 -080040import android.util.MathUtils;
Jeff Brown96307042012-07-27 15:51:34 -070041import android.util.Slog;
Jeff Brown1a30b552012-08-16 01:31:11 -070042import android.util.Spline;
Jeff Brown96307042012-07-27 15:51:34 -070043import android.util.TimeUtils;
Jeff Brown037c33e2014-04-09 00:31:55 -070044import android.view.Display;
Jeff Brown3ee549c2014-09-22 20:14:39 -070045import android.view.WindowManagerPolicy;
Jeff Brown96307042012-07-27 15:51:34 -070046
47import java.io.PrintWriter;
Jeff Brown96307042012-07-27 15:51:34 -070048
49/**
50 * Controls the power state of the display.
51 *
52 * Handles the proximity sensor, light sensor, and animations between states
53 * including the screen off animation.
54 *
55 * This component acts independently of the rest of the power manager service.
56 * In particular, it does not share any state and it only communicates
57 * via asynchronous callbacks to inform the power manager that something has
58 * changed.
59 *
60 * Everything this class does internally is serialized on its handler although
61 * it may be accessed by other threads from the outside.
62 *
63 * Note that the power manager service guarantees that it will hold a suspend
64 * blocker as long as the display is not ready. So most of the work done here
65 * does not need to worry about holding a suspend blocker unless it happens
66 * independently of the display ready signal.
Michael Lentine0839adb2014-07-29 18:47:56 -070067 *
Jeff Brown3ee549c2014-09-22 20:14:39 -070068 * For debugging, you can make the color fade and brightness animations run
Jeff Brown96307042012-07-27 15:51:34 -070069 * slower by changing the "animator duration scale" option in Development Settings.
70 */
Michael Wright639c8be2014-01-17 18:29:12 -080071final class DisplayPowerController implements AutomaticBrightnessController.Callbacks {
Jeff Brown96307042012-07-27 15:51:34 -070072 private static final String TAG = "DisplayPowerController";
Jeff Browna576b4d2015-04-23 19:58:06 -070073 private static final String SCREEN_ON_BLOCKED_TRACE_NAME = "Screen on blocked";
Jeff Brown96307042012-07-27 15:51:34 -070074
Julius D'souza3275a3a2016-08-07 19:08:30 -070075 private static final boolean DEBUG = false;
Jeff Brown96307042012-07-27 15:51:34 -070076 private static final boolean DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT = false;
Jeff Brown96307042012-07-27 15:51:34 -070077
Jeff Brown3ee549c2014-09-22 20:14:39 -070078 // If true, uses the color fade on animation.
Jeff Brown13c589b2012-08-16 16:20:54 -070079 // We might want to turn this off if we cannot get a guarantee that the screen
80 // actually turns on and starts showing new content after the call to set the
Jeff Brown5356c7dc2012-08-20 20:17:36 -070081 // screen state returns. Playing the animation can also be somewhat slow.
Michael Lentine0839adb2014-07-29 18:47:56 -070082 private static final boolean USE_COLOR_FADE_ON_ANIMATION = false;
Jeff Brown13c589b2012-08-16 16:20:54 -070083
Jeff Brownb76eebff2012-10-05 22:26:44 -070084 // The minimum reduction in brightness when dimmed.
85 private static final int SCREEN_DIM_MINIMUM_REDUCTION = 10;
86
Michael Lentine0839adb2014-07-29 18:47:56 -070087 private static final int COLOR_FADE_ON_ANIMATION_DURATION_MILLIS = 250;
Michael Lentine0c9a62d2014-08-20 09:19:07 -070088 private static final int COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS = 400;
Jeff Brown96307042012-07-27 15:51:34 -070089
90 private static final int MSG_UPDATE_POWER_STATE = 1;
91 private static final int MSG_PROXIMITY_SENSOR_DEBOUNCED = 2;
Jeff Brown3ee549c2014-09-22 20:14:39 -070092 private static final int MSG_SCREEN_ON_UNBLOCKED = 3;
Jeff Brown96307042012-07-27 15:51:34 -070093
94 private static final int PROXIMITY_UNKNOWN = -1;
95 private static final int PROXIMITY_NEGATIVE = 0;
96 private static final int PROXIMITY_POSITIVE = 1;
97
Jeff Brown93cbbb22012-10-04 13:18:36 -070098 // Proximity sensor debounce delay in milliseconds for positive or negative transitions.
99 private static final int PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY = 0;
Jeff Brownec083212013-09-11 20:45:25 -0700100 private static final int PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY = 250;
Jeff Brown96307042012-07-27 15:51:34 -0700101
102 // Trigger proximity if distance is less than 5 cm.
103 private static final float TYPICAL_PROXIMITY_THRESHOLD = 5.0f;
104
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700105 private static final int REPORTED_TO_POLICY_SCREEN_OFF = 0;
106 private static final int REPORTED_TO_POLICY_SCREEN_TURNING_ON = 1;
107 private static final int REPORTED_TO_POLICY_SCREEN_ON = 2;
108
Jeff Brown96307042012-07-27 15:51:34 -0700109 private final Object mLock = new Object();
110
Michael Lentine0839adb2014-07-29 18:47:56 -0700111 private final Context mContext;
112
Jeff Brown96307042012-07-27 15:51:34 -0700113 // Our handler.
114 private final DisplayControllerHandler mHandler;
115
116 // Asynchronous callbacks into the power manager service.
117 // Only invoked from the handler thread while no locks are held.
Jeff Brown131206b2014-04-08 17:27:14 -0700118 private final DisplayPowerCallbacks mCallbacks;
119
120 // Battery stats.
121 private final IBatteryStats mBatteryStats;
Jeff Brown96307042012-07-27 15:51:34 -0700122
Jeff Brown96307042012-07-27 15:51:34 -0700123 // The sensor manager.
124 private final SensorManager mSensorManager;
125
Jeff Brown3ee549c2014-09-22 20:14:39 -0700126 // The window manager policy.
127 private final WindowManagerPolicy mWindowManagerPolicy;
128
Jeff Brown037c33e2014-04-09 00:31:55 -0700129 // The display blanker.
130 private final DisplayBlanker mBlanker;
131
Jeff Brown96307042012-07-27 15:51:34 -0700132 // The proximity sensor, or null if not available or needed.
133 private Sensor mProximitySensor;
134
Jeff Brown26875502014-01-30 21:47:47 -0800135 // The doze screen brightness.
136 private final int mScreenBrightnessDozeConfig;
137
Jeff Brown96307042012-07-27 15:51:34 -0700138 // The dim screen brightness.
139 private final int mScreenBrightnessDimConfig;
140
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700141 // The minimum screen brightness to use in a very dark room.
142 private final int mScreenBrightnessDarkConfig;
143
Jeff Brownb76eebff2012-10-05 22:26:44 -0700144 // The minimum allowed brightness.
145 private final int mScreenBrightnessRangeMinimum;
146
147 // The maximum allowed brightness.
148 private final int mScreenBrightnessRangeMaximum;
149
Jeff Brown330560f2012-08-21 22:10:57 -0700150 // True if auto-brightness should be used.
Jeff Brown96307042012-07-27 15:51:34 -0700151 private boolean mUseSoftwareAutoBrightnessConfig;
Jeff Brown330560f2012-08-21 22:10:57 -0700152
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700153 // True if should use light sensor to automatically determine doze screen brightness.
154 private final boolean mAllowAutoBrightnessWhileDozingConfig;
155
Jeff Brown252c2062012-10-08 16:21:01 -0700156 // True if we should fade the screen while turning it off, false if we should play
Jeff Brown3ee549c2014-09-22 20:14:39 -0700157 // a stylish color fade animation instead.
Michael Lentine0839adb2014-07-29 18:47:56 -0700158 private boolean mColorFadeFadesConfig;
Jeff Browna52772f2012-10-04 18:38:09 -0700159
Jeff Brown96307042012-07-27 15:51:34 -0700160 // The pending power request.
161 // Initially null until the first call to requestPowerState.
162 // Guarded by mLock.
163 private DisplayPowerRequest mPendingRequestLocked;
164
165 // True if a request has been made to wait for the proximity sensor to go negative.
166 // Guarded by mLock.
167 private boolean mPendingWaitForNegativeProximityLocked;
168
169 // True if the pending power request or wait for negative proximity flag
170 // has been changed since the last update occurred.
171 // Guarded by mLock.
172 private boolean mPendingRequestChangedLocked;
173
174 // Set to true when the important parts of the pending power request have been applied.
175 // The important parts are mainly the screen state. Brightness changes may occur
176 // concurrently.
177 // Guarded by mLock.
178 private boolean mDisplayReadyLocked;
179
180 // Set to true if a power state update is required.
181 // Guarded by mLock.
182 private boolean mPendingUpdatePowerStateLocked;
183
184 /* The following state must only be accessed by the handler thread. */
185
186 // The currently requested power state.
187 // The power controller will progressively update its internal state to match
188 // the requested power state. Initially null until the first update.
189 private DisplayPowerRequest mPowerRequest;
190
191 // The current power state.
192 // Must only be accessed on the handler thread.
193 private DisplayPowerState mPowerState;
194
195 // True if the device should wait for negative proximity sensor before
196 // waking up the screen. This is set to false as soon as a negative
197 // proximity sensor measurement is observed or when the device is forced to
198 // go to sleep by the user. While true, the screen remains off.
199 private boolean mWaitingForNegativeProximity;
200
201 // The actual proximity sensor threshold value.
202 private float mProximityThreshold;
203
204 // Set to true if the proximity sensor listener has been registered
205 // with the sensor manager.
206 private boolean mProximitySensorEnabled;
207
208 // The debounced proximity sensor state.
209 private int mProximity = PROXIMITY_UNKNOWN;
210
211 // The raw non-debounced proximity sensor state.
212 private int mPendingProximity = PROXIMITY_UNKNOWN;
Jeff Brownec083212013-09-11 20:45:25 -0700213 private long mPendingProximityDebounceTime = -1; // -1 if fully debounced
Jeff Brown96307042012-07-27 15:51:34 -0700214
215 // True if the screen was turned off because of the proximity sensor.
216 // When the screen turns on again, we report user activity to the power manager.
217 private boolean mScreenOffBecauseOfProximity;
218
Jeff Brown3ee549c2014-09-22 20:14:39 -0700219 // The currently active screen on unblocker. This field is non-null whenever
220 // we are waiting for a callback to release it and unblock the screen.
221 private ScreenOnUnblocker mPendingScreenOnUnblocker;
222
223 // True if we were in the process of turning off the screen.
224 // This allows us to recover more gracefully from situations where we abort
225 // turning off the screen.
226 private boolean mPendingScreenOff;
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700227
Jeff Brown0a434772014-09-30 14:42:27 -0700228 // True if we have unfinished business and are holding a suspend blocker.
229 private boolean mUnfinishedBusiness;
230
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700231 // The elapsed real time when the screen on was blocked.
232 private long mScreenOnBlockStartRealTime;
233
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700234 // Screen state we reported to policy. Must be one of REPORTED_TO_POLICY_SCREEN_* fields.
235 private int mReportedScreenStateToPolicy;
Jeff Browne1633ad2015-06-22 19:24:24 -0700236
Jeff Brown970d4132014-07-19 11:33:47 -0700237 // Remembers whether certain kinds of brightness adjustments
238 // were recently applied so that we can decide how to transition.
239 private boolean mAppliedAutoBrightness;
240 private boolean mAppliedDimming;
241 private boolean mAppliedLowPower;
Jeff Brown96307042012-07-27 15:51:34 -0700242
Julius D'souza13edf0a2016-08-05 14:34:38 -0700243 // Brightness animation ramp rates in brightness units per second
Prashant Malani61038dd2016-03-29 16:32:37 -0700244 private final int mBrightnessRampRateFast;
Julius D'souza13edf0a2016-08-05 14:34:38 -0700245 private final int mBrightnessRampRateSlow;
Prashant Malani61038dd2016-03-29 16:32:37 -0700246
Michael Wright639c8be2014-01-17 18:29:12 -0800247 // The controller for the automatic brightness level.
248 private AutomaticBrightnessController mAutomaticBrightnessController;
249
Jeff Brown96307042012-07-27 15:51:34 -0700250 // Animators.
Michael Lentine0839adb2014-07-29 18:47:56 -0700251 private ObjectAnimator mColorFadeOnAnimator;
252 private ObjectAnimator mColorFadeOffAnimator;
Jeff Brown96307042012-07-27 15:51:34 -0700253 private RampAnimator<DisplayPowerState> mScreenBrightnessRampAnimator;
254
255 /**
256 * Creates the display power controller.
257 */
Jeff Brown131206b2014-04-08 17:27:14 -0700258 public DisplayPowerController(Context context,
Jeff Brown037c33e2014-04-09 00:31:55 -0700259 DisplayPowerCallbacks callbacks, Handler handler,
260 SensorManager sensorManager, DisplayBlanker blanker) {
Jeff Brown131206b2014-04-08 17:27:14 -0700261 mHandler = new DisplayControllerHandler(handler.getLooper());
Jeff Brown96307042012-07-27 15:51:34 -0700262 mCallbacks = callbacks;
Jeff Brown96307042012-07-27 15:51:34 -0700263
Jeff Brown131206b2014-04-08 17:27:14 -0700264 mBatteryStats = BatteryStatsService.getService();
Jeff Brown3b971592013-01-09 18:46:37 -0800265 mSensorManager = sensorManager;
Jeff Brown3ee549c2014-09-22 20:14:39 -0700266 mWindowManagerPolicy = LocalServices.getService(WindowManagerPolicy.class);
Jeff Brown037c33e2014-04-09 00:31:55 -0700267 mBlanker = blanker;
Michael Lentine0839adb2014-07-29 18:47:56 -0700268 mContext = context;
Jeff Brown96307042012-07-27 15:51:34 -0700269
270 final Resources resources = context.getResources();
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700271 final int screenBrightnessSettingMinimum = clampAbsoluteBrightness(resources.getInteger(
272 com.android.internal.R.integer.config_screenBrightnessSettingMinimum));
Jeff Brownb76eebff2012-10-05 22:26:44 -0700273
Jeff Brown26875502014-01-30 21:47:47 -0800274 mScreenBrightnessDozeConfig = clampAbsoluteBrightness(resources.getInteger(
275 com.android.internal.R.integer.config_screenBrightnessDoze));
276
Jeff Brownb76eebff2012-10-05 22:26:44 -0700277 mScreenBrightnessDimConfig = clampAbsoluteBrightness(resources.getInteger(
278 com.android.internal.R.integer.config_screenBrightnessDim));
279
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700280 mScreenBrightnessDarkConfig = clampAbsoluteBrightness(resources.getInteger(
281 com.android.internal.R.integer.config_screenBrightnessDark));
282 if (mScreenBrightnessDarkConfig > mScreenBrightnessDimConfig) {
283 Slog.w(TAG, "Expected config_screenBrightnessDark ("
284 + mScreenBrightnessDarkConfig + ") to be less than or equal to "
285 + "config_screenBrightnessDim (" + mScreenBrightnessDimConfig + ").");
286 }
287 if (mScreenBrightnessDarkConfig > mScreenBrightnessDimConfig) {
288 Slog.w(TAG, "Expected config_screenBrightnessDark ("
289 + mScreenBrightnessDarkConfig + ") to be less than or equal to "
290 + "config_screenBrightnessSettingMinimum ("
291 + screenBrightnessSettingMinimum + ").");
292 }
293
294 int screenBrightnessRangeMinimum = Math.min(Math.min(
295 screenBrightnessSettingMinimum, mScreenBrightnessDimConfig),
296 mScreenBrightnessDarkConfig);
Michael Wright639c8be2014-01-17 18:29:12 -0800297
298 mScreenBrightnessRangeMaximum = PowerManager.BRIGHTNESS_ON;
Jeff Brownb76eebff2012-10-05 22:26:44 -0700299
Jeff Brown96307042012-07-27 15:51:34 -0700300 mUseSoftwareAutoBrightnessConfig = resources.getBoolean(
301 com.android.internal.R.bool.config_automatic_brightness_available);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700302
303 mAllowAutoBrightnessWhileDozingConfig = resources.getBoolean(
304 com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing);
305
Prashant Malani61038dd2016-03-29 16:32:37 -0700306 mBrightnessRampRateFast = resources.getInteger(
307 com.android.internal.R.integer.config_brightness_ramp_rate_fast);
Julius D'souza13edf0a2016-08-05 14:34:38 -0700308 mBrightnessRampRateSlow = resources.getInteger(
309 com.android.internal.R.integer.config_brightness_ramp_rate_slow);
Prashant Malani61038dd2016-03-29 16:32:37 -0700310
Filip Gruszczynskid81ecd12015-02-06 12:38:47 -0800311 int lightSensorRate = resources.getInteger(
312 com.android.internal.R.integer.config_autoBrightnessLightSensorRate);
313 long brighteningLightDebounce = resources.getInteger(
314 com.android.internal.R.integer.config_autoBrightnessBrighteningLightDebounce);
315 long darkeningLightDebounce = resources.getInteger(
316 com.android.internal.R.integer.config_autoBrightnessDarkeningLightDebounce);
317 boolean autoBrightnessResetAmbientLuxAfterWarmUp = resources.getBoolean(
318 com.android.internal.R.bool.config_autoBrightnessResetAmbientLuxAfterWarmUp);
319
Julius D'souza3275a3a2016-08-07 19:08:30 -0700320 int[] brightLevels = resources.getIntArray(
321 com.android.internal.R.array.config_dynamicHysteresisBrightLevels);
322 int[] darkLevels = resources.getIntArray(
323 com.android.internal.R.array.config_dynamicHysteresisDarkLevels);
324 int[] luxLevels = resources.getIntArray(
325 com.android.internal.R.array.config_dynamicHysteresisLuxLevels);
326 HysteresisLevels dynamicHysteresis = new HysteresisLevels(
327 brightLevels, darkLevels, luxLevels);
328
Jeff Brown96307042012-07-27 15:51:34 -0700329 if (mUseSoftwareAutoBrightnessConfig) {
Jeff Brown1a30b552012-08-16 01:31:11 -0700330 int[] lux = resources.getIntArray(
Jeff Brown96307042012-07-27 15:51:34 -0700331 com.android.internal.R.array.config_autoBrightnessLevels);
Jeff Brown1a30b552012-08-16 01:31:11 -0700332 int[] screenBrightness = resources.getIntArray(
Jeff Brown96307042012-07-27 15:51:34 -0700333 com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
Michael Wright639c8be2014-01-17 18:29:12 -0800334 int lightSensorWarmUpTimeConfig = resources.getInteger(
335 com.android.internal.R.integer.config_lightSensorWarmupTime);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700336 final float dozeScaleFactor = resources.getFraction(
337 com.android.internal.R.fraction.config_screenAutoBrightnessDozeScaleFactor,
338 1, 1);
Jeff Brown1a30b552012-08-16 01:31:11 -0700339
Michael Wright639c8be2014-01-17 18:29:12 -0800340 Spline screenAutoBrightnessSpline = createAutoBrightnessSpline(lux, screenBrightness);
341 if (screenAutoBrightnessSpline == null) {
Jeff Brown96307042012-07-27 15:51:34 -0700342 Slog.e(TAG, "Error in config.xml. config_autoBrightnessLcdBacklightValues "
Jeff Brown1a30b552012-08-16 01:31:11 -0700343 + "(size " + screenBrightness.length + ") "
344 + "must be monotic and have exactly one more entry than "
345 + "config_autoBrightnessLevels (size " + lux.length + ") "
346 + "which must be strictly increasing. "
Jeff Brown96307042012-07-27 15:51:34 -0700347 + "Auto-brightness will be disabled.");
348 mUseSoftwareAutoBrightnessConfig = false;
Jeff Brownb76eebff2012-10-05 22:26:44 -0700349 } else {
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700350 int bottom = clampAbsoluteBrightness(screenBrightness[0]);
351 if (mScreenBrightnessDarkConfig > bottom) {
352 Slog.w(TAG, "config_screenBrightnessDark (" + mScreenBrightnessDarkConfig
353 + ") should be less than or equal to the first value of "
354 + "config_autoBrightnessLcdBacklightValues ("
355 + bottom + ").");
356 }
357 if (bottom < screenBrightnessRangeMinimum) {
358 screenBrightnessRangeMinimum = bottom;
Jeff Brownb76eebff2012-10-05 22:26:44 -0700359 }
Jeff Brown131206b2014-04-08 17:27:14 -0700360 mAutomaticBrightnessController = new AutomaticBrightnessController(this,
361 handler.getLooper(), sensorManager, screenAutoBrightnessSpline,
Michael Wright639c8be2014-01-17 18:29:12 -0800362 lightSensorWarmUpTimeConfig, screenBrightnessRangeMinimum,
Filip Gruszczynskid81ecd12015-02-06 12:38:47 -0800363 mScreenBrightnessRangeMaximum, dozeScaleFactor, lightSensorRate,
364 brighteningLightDebounce, darkeningLightDebounce,
Julius D'souza3275a3a2016-08-07 19:08:30 -0700365 autoBrightnessResetAmbientLuxAfterWarmUp, dynamicHysteresis);
Jeff Brown96307042012-07-27 15:51:34 -0700366 }
Jeff Brown96307042012-07-27 15:51:34 -0700367 }
368
Michael Wright639c8be2014-01-17 18:29:12 -0800369 mScreenBrightnessRangeMinimum = screenBrightnessRangeMinimum;
Jeff Brownb76eebff2012-10-05 22:26:44 -0700370
Michael Lentine0839adb2014-07-29 18:47:56 -0700371 mColorFadeFadesConfig = resources.getBoolean(
Jeff Browna52772f2012-10-04 18:38:09 -0700372 com.android.internal.R.bool.config_animateScreenLights);
373
Jeff Brown96307042012-07-27 15:51:34 -0700374 if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) {
375 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
376 if (mProximitySensor != null) {
377 mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(),
378 TYPICAL_PROXIMITY_THRESHOLD);
379 }
380 }
381
Jeff Brown1a30b552012-08-16 01:31:11 -0700382 }
383
Jeff Brown96307042012-07-27 15:51:34 -0700384 /**
385 * Returns true if the proximity sensor screen-off function is available.
386 */
387 public boolean isProximitySensorAvailable() {
388 return mProximitySensor != null;
389 }
390
391 /**
392 * Requests a new power state.
393 * The controller makes a copy of the provided object and then
394 * begins adjusting the power state to match what was requested.
395 *
396 * @param request The requested power state.
397 * @param waitForNegativeProximity If true, issues a request to wait for
398 * negative proximity before turning the screen back on, assuming the screen
399 * was turned off by the proximity sensor.
400 * @return True if display is ready, false if there are important changes that must
401 * be made asynchronously (such as turning the screen on), in which case the caller
Jeff Brown606e4e82014-09-18 15:22:26 -0700402 * should grab a wake lock, watch for {@link DisplayPowerCallbacks#onStateChanged()}
403 * then try the request again later until the state converges.
Jeff Brown96307042012-07-27 15:51:34 -0700404 */
405 public boolean requestPowerState(DisplayPowerRequest request,
406 boolean waitForNegativeProximity) {
407 if (DEBUG) {
408 Slog.d(TAG, "requestPowerState: "
409 + request + ", waitForNegativeProximity=" + waitForNegativeProximity);
410 }
411
412 synchronized (mLock) {
413 boolean changed = false;
414
415 if (waitForNegativeProximity
416 && !mPendingWaitForNegativeProximityLocked) {
417 mPendingWaitForNegativeProximityLocked = true;
418 changed = true;
419 }
420
421 if (mPendingRequestLocked == null) {
422 mPendingRequestLocked = new DisplayPowerRequest(request);
423 changed = true;
424 } else if (!mPendingRequestLocked.equals(request)) {
425 mPendingRequestLocked.copyFrom(request);
426 changed = true;
427 }
428
429 if (changed) {
430 mDisplayReadyLocked = false;
431 }
432
433 if (changed && !mPendingRequestChangedLocked) {
434 mPendingRequestChangedLocked = true;
435 sendUpdatePowerStateLocked();
436 }
437
438 return mDisplayReadyLocked;
439 }
440 }
441
442 private void sendUpdatePowerState() {
443 synchronized (mLock) {
444 sendUpdatePowerStateLocked();
445 }
446 }
447
448 private void sendUpdatePowerStateLocked() {
449 if (!mPendingUpdatePowerStateLocked) {
450 mPendingUpdatePowerStateLocked = true;
451 Message msg = mHandler.obtainMessage(MSG_UPDATE_POWER_STATE);
452 msg.setAsynchronous(true);
453 mHandler.sendMessage(msg);
454 }
455 }
456
457 private void initialize() {
Jeff Brown037c33e2014-04-09 00:31:55 -0700458 // Initialize the power state object for the default display.
459 // In the future, we might manage multiple displays independently.
460 mPowerState = new DisplayPowerState(mBlanker,
Michael Lentine0839adb2014-07-29 18:47:56 -0700461 new ColorFade(Display.DEFAULT_DISPLAY));
Jeff Brown96307042012-07-27 15:51:34 -0700462
Michael Lentine0839adb2014-07-29 18:47:56 -0700463 mColorFadeOnAnimator = ObjectAnimator.ofFloat(
464 mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 0.0f, 1.0f);
465 mColorFadeOnAnimator.setDuration(COLOR_FADE_ON_ANIMATION_DURATION_MILLIS);
466 mColorFadeOnAnimator.addListener(mAnimatorListener);
Jeff Brown96307042012-07-27 15:51:34 -0700467
Michael Lentine0839adb2014-07-29 18:47:56 -0700468 mColorFadeOffAnimator = ObjectAnimator.ofFloat(
469 mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 1.0f, 0.0f);
470 mColorFadeOffAnimator.setDuration(COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS);
471 mColorFadeOffAnimator.addListener(mAnimatorListener);
Jeff Brown96307042012-07-27 15:51:34 -0700472
473 mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>(
474 mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS);
Jeff Brown42558692014-05-20 22:02:46 -0700475 mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800476
Jeff Brown131206b2014-04-08 17:27:14 -0700477 // Initialize screen state for battery stats.
478 try {
Jeff Browne95c3cd2014-05-02 16:59:26 -0700479 mBatteryStats.noteScreenState(mPowerState.getScreenState());
Jeff Brown131206b2014-04-08 17:27:14 -0700480 mBatteryStats.noteScreenBrightness(mPowerState.getScreenBrightness());
481 } catch (RemoteException ex) {
482 // same process
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800483 }
Jeff Brown96307042012-07-27 15:51:34 -0700484 }
485
486 private final Animator.AnimatorListener mAnimatorListener = new Animator.AnimatorListener() {
487 @Override
488 public void onAnimationStart(Animator animation) {
489 }
490 @Override
491 public void onAnimationEnd(Animator animation) {
492 sendUpdatePowerState();
493 }
494 @Override
495 public void onAnimationRepeat(Animator animation) {
496 }
497 @Override
498 public void onAnimationCancel(Animator animation) {
499 }
500 };
501
Jeff Brown42558692014-05-20 22:02:46 -0700502 private final RampAnimator.Listener mRampAnimatorListener = new RampAnimator.Listener() {
503 @Override
504 public void onAnimationEnd() {
505 sendUpdatePowerState();
506 }
507 };
508
Jeff Brown96307042012-07-27 15:51:34 -0700509 private void updatePowerState() {
510 // Update the power state request.
511 final boolean mustNotify;
512 boolean mustInitialize = false;
Adrian Roos6da87ab2014-05-16 23:09:41 +0200513 boolean autoBrightnessAdjustmentChanged = false;
Jeff Brown330560f2012-08-21 22:10:57 -0700514
Jeff Brown96307042012-07-27 15:51:34 -0700515 synchronized (mLock) {
516 mPendingUpdatePowerStateLocked = false;
517 if (mPendingRequestLocked == null) {
518 return; // wait until first actual power request
519 }
520
521 if (mPowerRequest == null) {
522 mPowerRequest = new DisplayPowerRequest(mPendingRequestLocked);
523 mWaitingForNegativeProximity = mPendingWaitForNegativeProximityLocked;
Jeff Brown6307a152012-08-20 13:24:23 -0700524 mPendingWaitForNegativeProximityLocked = false;
Jeff Brown96307042012-07-27 15:51:34 -0700525 mPendingRequestChangedLocked = false;
526 mustInitialize = true;
527 } else if (mPendingRequestChangedLocked) {
Adrian Roos6da87ab2014-05-16 23:09:41 +0200528 autoBrightnessAdjustmentChanged = (mPowerRequest.screenAutoBrightnessAdjustment
529 != mPendingRequestLocked.screenAutoBrightnessAdjustment);
Jeff Brown96307042012-07-27 15:51:34 -0700530 mPowerRequest.copyFrom(mPendingRequestLocked);
531 mWaitingForNegativeProximity |= mPendingWaitForNegativeProximityLocked;
Jeff Brown6307a152012-08-20 13:24:23 -0700532 mPendingWaitForNegativeProximityLocked = false;
Jeff Brown96307042012-07-27 15:51:34 -0700533 mPendingRequestChangedLocked = false;
534 mDisplayReadyLocked = false;
535 }
536
537 mustNotify = !mDisplayReadyLocked;
538 }
539
540 // Initialize things the first time the power state is changed.
541 if (mustInitialize) {
542 initialize();
543 }
544
Jeff Brown970d4132014-07-19 11:33:47 -0700545 // Compute the basic display state using the policy.
546 // We might override this below based on other factors.
547 int state;
548 int brightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700549 boolean performScreenOffTransition = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700550 switch (mPowerRequest.policy) {
551 case DisplayPowerRequest.POLICY_OFF:
552 state = Display.STATE_OFF;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700553 performScreenOffTransition = true;
Jeff Brown970d4132014-07-19 11:33:47 -0700554 break;
555 case DisplayPowerRequest.POLICY_DOZE:
556 if (mPowerRequest.dozeScreenState != Display.STATE_UNKNOWN) {
557 state = mPowerRequest.dozeScreenState;
558 } else {
559 state = Display.STATE_DOZE;
560 }
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700561 if (!mAllowAutoBrightnessWhileDozingConfig) {
562 brightness = mPowerRequest.dozeScreenBrightness;
563 }
Jeff Brown970d4132014-07-19 11:33:47 -0700564 break;
565 case DisplayPowerRequest.POLICY_DIM:
566 case DisplayPowerRequest.POLICY_BRIGHT:
567 default:
568 state = Display.STATE_ON;
569 break;
570 }
Jeff Brown2175e9c2014-09-12 16:11:07 -0700571 assert(state != Display.STATE_UNKNOWN);
Jeff Brown970d4132014-07-19 11:33:47 -0700572
Jeff Brown6307a152012-08-20 13:24:23 -0700573 // Apply the proximity sensor.
Jeff Brown96307042012-07-27 15:51:34 -0700574 if (mProximitySensor != null) {
Jeff Brown970d4132014-07-19 11:33:47 -0700575 if (mPowerRequest.useProximitySensor && state != Display.STATE_OFF) {
Jeff Brown6307a152012-08-20 13:24:23 -0700576 setProximitySensorEnabled(true);
577 if (!mScreenOffBecauseOfProximity
578 && mProximity == PROXIMITY_POSITIVE) {
579 mScreenOffBecauseOfProximity = true;
Jeff Brownec083212013-09-11 20:45:25 -0700580 sendOnProximityPositiveWithWakelock();
Jeff Brown6307a152012-08-20 13:24:23 -0700581 }
582 } else if (mWaitingForNegativeProximity
583 && mScreenOffBecauseOfProximity
584 && mProximity == PROXIMITY_POSITIVE
Jeff Brown970d4132014-07-19 11:33:47 -0700585 && state != Display.STATE_OFF) {
Jeff Brown6307a152012-08-20 13:24:23 -0700586 setProximitySensorEnabled(true);
587 } else {
588 setProximitySensorEnabled(false);
589 mWaitingForNegativeProximity = false;
590 }
591 if (mScreenOffBecauseOfProximity
592 && mProximity != PROXIMITY_POSITIVE) {
Jeff Brown96307042012-07-27 15:51:34 -0700593 mScreenOffBecauseOfProximity = false;
Jeff Brownec083212013-09-11 20:45:25 -0700594 sendOnProximityNegativeWithWakelock();
Jeff Brown96307042012-07-27 15:51:34 -0700595 }
Jeff Brown6307a152012-08-20 13:24:23 -0700596 } else {
597 mWaitingForNegativeProximity = false;
Jeff Brown96307042012-07-27 15:51:34 -0700598 }
Jeff Brown970d4132014-07-19 11:33:47 -0700599 if (mScreenOffBecauseOfProximity) {
600 state = Display.STATE_OFF;
Jeff Brown96307042012-07-27 15:51:34 -0700601 }
602
Jeff Brownbf4e4142014-10-02 13:08:05 -0700603 // Animate the screen state change unless already animating.
604 // The transition may be deferred, so after this point we will use the
605 // actual state instead of the desired one.
606 animateScreenStateChange(state, performScreenOffTransition);
607 state = mPowerState.getScreenState();
608
Jeff Brown970d4132014-07-19 11:33:47 -0700609 // Use zero brightness when screen is off.
610 if (state == Display.STATE_OFF) {
611 brightness = PowerManager.BRIGHTNESS_OFF;
612 }
613
Jeff Brown970d4132014-07-19 11:33:47 -0700614 // Configure auto-brightness.
615 boolean autoBrightnessEnabled = false;
616 if (mAutomaticBrightnessController != null) {
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700617 final boolean autoBrightnessEnabledInDoze = mAllowAutoBrightnessWhileDozingConfig
618 && (state == Display.STATE_DOZE || state == Display.STATE_DOZE_SUSPEND);
Jeff Brown970d4132014-07-19 11:33:47 -0700619 autoBrightnessEnabled = mPowerRequest.useAutoBrightness
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700620 && (state == Display.STATE_ON || autoBrightnessEnabledInDoze)
621 && brightness < 0;
Jeff Browna576b4d2015-04-23 19:58:06 -0700622 final boolean userInitiatedChange = autoBrightnessAdjustmentChanged
623 && mPowerRequest.brightnessSetByUser;
Jeff Brown970d4132014-07-19 11:33:47 -0700624 mAutomaticBrightnessController.configure(autoBrightnessEnabled,
Jeff Browna576b4d2015-04-23 19:58:06 -0700625 mPowerRequest.screenAutoBrightnessAdjustment, state != Display.STATE_ON,
626 userInitiatedChange);
Jeff Brown970d4132014-07-19 11:33:47 -0700627 }
628
Jeff Brown7b5be5e2014-11-12 18:45:31 -0800629 // Apply brightness boost.
630 // We do this here after configuring auto-brightness so that we don't
631 // disable the light sensor during this temporary state. That way when
632 // boost ends we will be able to resume normal auto-brightness behavior
633 // without any delay.
634 if (mPowerRequest.boostScreenBrightness
635 && brightness != PowerManager.BRIGHTNESS_OFF) {
636 brightness = PowerManager.BRIGHTNESS_ON;
637 }
638
Jeff Brown970d4132014-07-19 11:33:47 -0700639 // Apply auto-brightness.
640 boolean slowChange = false;
641 if (brightness < 0) {
642 if (autoBrightnessEnabled) {
643 brightness = mAutomaticBrightnessController.getAutomaticScreenBrightness();
644 }
645 if (brightness >= 0) {
646 // Use current auto-brightness value and slowly adjust to changes.
647 brightness = clampScreenBrightness(brightness);
648 if (mAppliedAutoBrightness && !autoBrightnessAdjustmentChanged) {
649 slowChange = true; // slowly adapt to auto-brightness
650 }
651 mAppliedAutoBrightness = true;
Jeff Brown96307042012-07-27 15:51:34 -0700652 } else {
Jeff Brown970d4132014-07-19 11:33:47 -0700653 mAppliedAutoBrightness = false;
Jeff Brown96307042012-07-27 15:51:34 -0700654 }
655 } else {
Jeff Brown970d4132014-07-19 11:33:47 -0700656 mAppliedAutoBrightness = false;
657 }
658
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700659 // Use default brightness when dozing unless overridden.
660 if (brightness < 0 && (state == Display.STATE_DOZE
661 || state == Display.STATE_DOZE_SUSPEND)) {
662 brightness = mScreenBrightnessDozeConfig;
663 }
664
Jeff Brown970d4132014-07-19 11:33:47 -0700665 // Apply manual brightness.
666 // Use the current brightness setting from the request, which is expected
667 // provide a nominal default value for the case where auto-brightness
668 // is not ready yet.
669 if (brightness < 0) {
670 brightness = clampScreenBrightness(mPowerRequest.screenBrightness);
671 }
672
673 // Apply dimming by at least some minimum amount when user activity
674 // timeout is about to expire.
675 if (mPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) {
Jeff Brown5c8ea082014-07-24 19:30:31 -0700676 if (brightness > mScreenBrightnessRangeMinimum) {
677 brightness = Math.max(Math.min(brightness - SCREEN_DIM_MINIMUM_REDUCTION,
678 mScreenBrightnessDimConfig), mScreenBrightnessRangeMinimum);
679 }
Jeff Brown970d4132014-07-19 11:33:47 -0700680 if (!mAppliedDimming) {
681 slowChange = false;
682 }
683 mAppliedDimming = true;
684 }
685
686 // If low power mode is enabled, cut the brightness level by half
687 // as long as it is above the minimum threshold.
688 if (mPowerRequest.lowPowerMode) {
689 if (brightness > mScreenBrightnessRangeMinimum) {
690 brightness = Math.max(brightness / 2, mScreenBrightnessRangeMinimum);
691 }
692 if (!mAppliedLowPower) {
693 slowChange = false;
694 }
695 mAppliedLowPower = true;
696 }
697
Jeff Brown0a434772014-09-30 14:42:27 -0700698 // Animate the screen brightness when the screen is on or dozing.
699 // Skip the animation when the screen is off or suspended.
Prashant Malani33538242014-11-13 14:04:00 -0800700 if (!mPendingScreenOff) {
701 if (state == Display.STATE_ON || state == Display.STATE_DOZE) {
702 animateScreenBrightness(brightness,
Julius D'souza13edf0a2016-08-05 14:34:38 -0700703 slowChange ? mBrightnessRampRateSlow : mBrightnessRampRateFast);
Prashant Malani33538242014-11-13 14:04:00 -0800704 } else {
705 animateScreenBrightness(brightness, 0);
706 }
Jeff Brown0a434772014-09-30 14:42:27 -0700707 }
708
709 // Determine whether the display is ready for use in the newly requested state.
710 // Note that we do not wait for the brightness ramp animation to complete before
711 // reporting the display is ready because we only need to ensure the screen is in the
712 // right power state even as it continues to converge on the desired brightness.
713 final boolean ready = mPendingScreenOnUnblocker == null
Michael Lentine0839adb2014-07-29 18:47:56 -0700714 && !mColorFadeOnAnimator.isStarted()
715 && !mColorFadeOffAnimator.isStarted()
Jeff Brown0a434772014-09-30 14:42:27 -0700716 && mPowerState.waitUntilClean(mCleanListener);
717 final boolean finished = ready
718 && !mScreenBrightnessRampAnimator.isAnimating();
719
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700720 // Notify policy about screen turned on.
721 if (ready && state != Display.STATE_OFF
722 && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_ON) {
723 mReportedScreenStateToPolicy = REPORTED_TO_POLICY_SCREEN_ON;
724 mWindowManagerPolicy.screenTurnedOn();
725 }
726
Jeff Brown0a434772014-09-30 14:42:27 -0700727 // Grab a wake lock if we have unfinished business.
728 if (!finished && !mUnfinishedBusiness) {
729 if (DEBUG) {
730 Slog.d(TAG, "Unfinished business...");
731 }
732 mCallbacks.acquireSuspendBlocker();
733 mUnfinishedBusiness = true;
734 }
735
736 // Notify the power manager when ready.
737 if (ready && mustNotify) {
738 // Send state change.
Jeff Brown96307042012-07-27 15:51:34 -0700739 synchronized (mLock) {
740 if (!mPendingRequestChangedLocked) {
741 mDisplayReadyLocked = true;
Jeff Brownc38c9be2012-10-04 13:16:19 -0700742
743 if (DEBUG) {
744 Slog.d(TAG, "Display ready!");
745 }
Jeff Brown96307042012-07-27 15:51:34 -0700746 }
747 }
Jeff Brownec083212013-09-11 20:45:25 -0700748 sendOnStateChangedWithWakelock();
Jeff Brown96307042012-07-27 15:51:34 -0700749 }
Jeff Brown0a434772014-09-30 14:42:27 -0700750
751 // Release the wake lock when we have no unfinished business.
752 if (finished && mUnfinishedBusiness) {
753 if (DEBUG) {
754 Slog.d(TAG, "Finished business...");
755 }
756 mUnfinishedBusiness = false;
757 mCallbacks.releaseSuspendBlocker();
758 }
Jeff Brown96307042012-07-27 15:51:34 -0700759 }
760
Michael Wright639c8be2014-01-17 18:29:12 -0800761 @Override
762 public void updateBrightness() {
763 sendUpdatePowerState();
764 }
765
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700766 private void blockScreenOn() {
Jeff Brown3ee549c2014-09-22 20:14:39 -0700767 if (mPendingScreenOnUnblocker == null) {
Jeff Brown3edf5272014-08-14 19:25:14 -0700768 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0);
Jeff Brown3ee549c2014-09-22 20:14:39 -0700769 mPendingScreenOnUnblocker = new ScreenOnUnblocker();
Jeff Brown037c33e2014-04-09 00:31:55 -0700770 mScreenOnBlockStartRealTime = SystemClock.elapsedRealtime();
Jeff Brown3edf5272014-08-14 19:25:14 -0700771 Slog.i(TAG, "Blocking screen on until initial contents have been drawn.");
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700772 }
773 }
774
775 private void unblockScreenOn() {
Jeff Brown3ee549c2014-09-22 20:14:39 -0700776 if (mPendingScreenOnUnblocker != null) {
777 mPendingScreenOnUnblocker = null;
Jeff Brown2d8a3902014-03-11 23:02:35 -0700778 long delay = SystemClock.elapsedRealtime() - mScreenOnBlockStartRealTime;
Jeff Brown3edf5272014-08-14 19:25:14 -0700779 Slog.i(TAG, "Unblocked screen on after " + delay + " ms");
780 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0);
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700781 }
782 }
783
Jeff Brown3ee549c2014-09-22 20:14:39 -0700784 private boolean setScreenState(int state) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700785 if (mPowerState.getScreenState() != state) {
Jeff Brown3ee549c2014-09-22 20:14:39 -0700786 final boolean wasOn = (mPowerState.getScreenState() != Display.STATE_OFF);
Jeff Brown037c33e2014-04-09 00:31:55 -0700787 mPowerState.setScreenState(state);
Jeff Brown3ee549c2014-09-22 20:14:39 -0700788
789 // Tell battery stats about the transition.
Jeff Brown131206b2014-04-08 17:27:14 -0700790 try {
Jeff Browne95c3cd2014-05-02 16:59:26 -0700791 mBatteryStats.noteScreenState(state);
Jeff Brown131206b2014-04-08 17:27:14 -0700792 } catch (RemoteException ex) {
793 // same process
Jeff Brown96307042012-07-27 15:51:34 -0700794 }
795 }
Jeff Browne1633ad2015-06-22 19:24:24 -0700796
797 // Tell the window manager policy when the screen is turned off or on unless it's due
798 // to the proximity sensor. We temporarily block turning the screen on until the
799 // window manager is ready by leaving a black surface covering the screen.
800 // This surface is essentially the final state of the color fade animation and
801 // it is only removed once the window manager tells us that the activity has
802 // finished drawing underneath.
803 final boolean isOff = (state == Display.STATE_OFF);
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700804 if (isOff && mReportedScreenStateToPolicy != REPORTED_TO_POLICY_SCREEN_OFF
805 && !mScreenOffBecauseOfProximity) {
806 mReportedScreenStateToPolicy = REPORTED_TO_POLICY_SCREEN_OFF;
Jeff Browne1633ad2015-06-22 19:24:24 -0700807 unblockScreenOn();
808 mWindowManagerPolicy.screenTurnedOff();
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700809 } else if (!isOff && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_OFF) {
810 mReportedScreenStateToPolicy = REPORTED_TO_POLICY_SCREEN_TURNING_ON;
Jeff Browne1633ad2015-06-22 19:24:24 -0700811 if (mPowerState.getColorFadeLevel() == 0.0f) {
812 blockScreenOn();
813 } else {
814 unblockScreenOn();
815 }
816 mWindowManagerPolicy.screenTurningOn(mPendingScreenOnUnblocker);
817 }
818
819 // Return true if the screen isn't blocked.
Jeff Brown3ee549c2014-09-22 20:14:39 -0700820 return mPendingScreenOnUnblocker == null;
Jeff Brown96307042012-07-27 15:51:34 -0700821 }
822
Jeff Brown330560f2012-08-21 22:10:57 -0700823 private int clampScreenBrightness(int value) {
Michael Wright639c8be2014-01-17 18:29:12 -0800824 return MathUtils.constrain(
825 value, mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum);
Jeff Brown330560f2012-08-21 22:10:57 -0700826 }
827
Jeff Brown96307042012-07-27 15:51:34 -0700828 private void animateScreenBrightness(int target, int rate) {
Jeff Brown0a434772014-09-30 14:42:27 -0700829 if (DEBUG) {
830 Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate);
831 }
Jeff Brown96307042012-07-27 15:51:34 -0700832 if (mScreenBrightnessRampAnimator.animateTo(target, rate)) {
Jeff Brown131206b2014-04-08 17:27:14 -0700833 try {
834 mBatteryStats.noteScreenBrightness(target);
835 } catch (RemoteException ex) {
836 // same process
837 }
Jeff Brown96307042012-07-27 15:51:34 -0700838 }
839 }
840
Jeff Brown606e4e82014-09-18 15:22:26 -0700841 private void animateScreenStateChange(int target, boolean performScreenOffTransition) {
842 // If there is already an animation in progress, don't interfere with it.
843 if (mColorFadeOnAnimator.isStarted()
844 || mColorFadeOffAnimator.isStarted()) {
845 return;
846 }
847
Jeff Brown3ee549c2014-09-22 20:14:39 -0700848 // If we were in the process of turning off the screen but didn't quite
849 // finish. Then finish up now to prevent a jarring transition back
850 // to screen on if we skipped blocking screen on as usual.
851 if (mPendingScreenOff && target != Display.STATE_OFF) {
852 setScreenState(Display.STATE_OFF);
853 mPendingScreenOff = false;
Michael Lentined73854d2015-09-25 10:55:26 -0700854 mPowerState.dismissColorFadeResources();
Jeff Brown606e4e82014-09-18 15:22:26 -0700855 }
856
857 if (target == Display.STATE_ON) {
858 // Want screen on. The contents of the screen may not yet
Jeff Brown3ee549c2014-09-22 20:14:39 -0700859 // be visible if the color fade has not been dismissed because
Jeff Brown606e4e82014-09-18 15:22:26 -0700860 // its last frame of animation is solid black.
Jeff Brown3ee549c2014-09-22 20:14:39 -0700861 if (!setScreenState(Display.STATE_ON)) {
862 return; // screen on blocked
863 }
Jeff Brown606e4e82014-09-18 15:22:26 -0700864 if (USE_COLOR_FADE_ON_ANIMATION && mPowerRequest.isBrightOrDim()) {
865 // Perform screen on animation.
866 if (mPowerState.getColorFadeLevel() == 1.0f) {
867 mPowerState.dismissColorFade();
868 } else if (mPowerState.prepareColorFade(mContext,
869 mColorFadeFadesConfig ?
870 ColorFade.MODE_FADE :
871 ColorFade.MODE_WARM_UP)) {
872 mColorFadeOnAnimator.start();
873 } else {
874 mColorFadeOnAnimator.end();
875 }
876 } else {
877 // Skip screen on animation.
878 mPowerState.setColorFadeLevel(1.0f);
879 mPowerState.dismissColorFade();
880 }
881 } else if (target == Display.STATE_DOZE) {
882 // Want screen dozing.
883 // Wait for brightness animation to complete beforehand when entering doze
884 // from screen on to prevent a perceptible jump because brightness may operate
885 // differently when the display is configured for dozing.
886 if (mScreenBrightnessRampAnimator.isAnimating()
887 && mPowerState.getScreenState() == Display.STATE_ON) {
888 return;
889 }
890
Jeff Brown3ee549c2014-09-22 20:14:39 -0700891 // Set screen state.
892 if (!setScreenState(Display.STATE_DOZE)) {
893 return; // screen on blocked
894 }
895
896 // Dismiss the black surface without fanfare.
Jeff Brown606e4e82014-09-18 15:22:26 -0700897 mPowerState.setColorFadeLevel(1.0f);
898 mPowerState.dismissColorFade();
899 } else if (target == Display.STATE_DOZE_SUSPEND) {
900 // Want screen dozing and suspended.
901 // Wait for brightness animation to complete beforehand unless already
902 // suspended because we may not be able to change it after suspension.
903 if (mScreenBrightnessRampAnimator.isAnimating()
904 && mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) {
905 return;
906 }
907
Jeff Brown3ee549c2014-09-22 20:14:39 -0700908 // If not already suspending, temporarily set the state to doze until the
909 // screen on is unblocked, then suspend.
910 if (mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) {
911 if (!setScreenState(Display.STATE_DOZE)) {
912 return; // screen on blocked
913 }
914 setScreenState(Display.STATE_DOZE_SUSPEND); // already on so can't block
915 }
916
917 // Dismiss the black surface without fanfare.
Jeff Brown606e4e82014-09-18 15:22:26 -0700918 mPowerState.setColorFadeLevel(1.0f);
919 mPowerState.dismissColorFade();
920 } else {
921 // Want screen off.
Jeff Brown3ee549c2014-09-22 20:14:39 -0700922 mPendingScreenOff = true;
Jeff Brown606e4e82014-09-18 15:22:26 -0700923 if (mPowerState.getColorFadeLevel() == 0.0f) {
924 // Turn the screen off.
925 // A black surface is already hiding the contents of the screen.
926 setScreenState(Display.STATE_OFF);
Jeff Brown3ee549c2014-09-22 20:14:39 -0700927 mPendingScreenOff = false;
Michael Lentined73854d2015-09-25 10:55:26 -0700928 mPowerState.dismissColorFadeResources();
Jeff Brown606e4e82014-09-18 15:22:26 -0700929 } else if (performScreenOffTransition
930 && mPowerState.prepareColorFade(mContext,
931 mColorFadeFadesConfig ?
932 ColorFade.MODE_FADE : ColorFade.MODE_COOL_DOWN)
933 && mPowerState.getScreenState() != Display.STATE_OFF) {
934 // Perform the screen off animation.
935 mColorFadeOffAnimator.start();
936 } else {
937 // Skip the screen off animation and add a black surface to hide the
938 // contents of the screen.
939 mColorFadeOffAnimator.end();
940 }
941 }
942 }
943
Jeff Brown96307042012-07-27 15:51:34 -0700944 private final Runnable mCleanListener = new Runnable() {
945 @Override
946 public void run() {
947 sendUpdatePowerState();
948 }
949 };
950
951 private void setProximitySensorEnabled(boolean enable) {
952 if (enable) {
953 if (!mProximitySensorEnabled) {
Jeff Brownec083212013-09-11 20:45:25 -0700954 // Register the listener.
955 // Proximity sensor state already cleared initially.
Jeff Brown96307042012-07-27 15:51:34 -0700956 mProximitySensorEnabled = true;
Jeff Brown96307042012-07-27 15:51:34 -0700957 mSensorManager.registerListener(mProximitySensorListener, mProximitySensor,
958 SensorManager.SENSOR_DELAY_NORMAL, mHandler);
959 }
960 } else {
961 if (mProximitySensorEnabled) {
Jeff Brownec083212013-09-11 20:45:25 -0700962 // Unregister the listener.
963 // Clear the proximity sensor state for next time.
Jeff Brown96307042012-07-27 15:51:34 -0700964 mProximitySensorEnabled = false;
965 mProximity = PROXIMITY_UNKNOWN;
Jeff Brownec083212013-09-11 20:45:25 -0700966 mPendingProximity = PROXIMITY_UNKNOWN;
Jeff Brown96307042012-07-27 15:51:34 -0700967 mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
968 mSensorManager.unregisterListener(mProximitySensorListener);
Jeff Brownec083212013-09-11 20:45:25 -0700969 clearPendingProximityDebounceTime(); // release wake lock (must be last)
Jeff Brown96307042012-07-27 15:51:34 -0700970 }
971 }
972 }
973
974 private void handleProximitySensorEvent(long time, boolean positive) {
Jeff Brownec083212013-09-11 20:45:25 -0700975 if (mProximitySensorEnabled) {
976 if (mPendingProximity == PROXIMITY_NEGATIVE && !positive) {
977 return; // no change
978 }
979 if (mPendingProximity == PROXIMITY_POSITIVE && positive) {
980 return; // no change
981 }
Jeff Brown96307042012-07-27 15:51:34 -0700982
Jeff Brownec083212013-09-11 20:45:25 -0700983 // Only accept a proximity sensor reading if it remains
984 // stable for the entire debounce delay. We hold a wake lock while
985 // debouncing the sensor.
986 mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
987 if (positive) {
988 mPendingProximity = PROXIMITY_POSITIVE;
989 setPendingProximityDebounceTime(
990 time + PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY); // acquire wake lock
991 } else {
992 mPendingProximity = PROXIMITY_NEGATIVE;
993 setPendingProximityDebounceTime(
994 time + PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY); // acquire wake lock
995 }
996
997 // Debounce the new sensor reading.
998 debounceProximitySensor();
Jeff Brown93cbbb22012-10-04 13:18:36 -0700999 }
Jeff Brown96307042012-07-27 15:51:34 -07001000 }
1001
1002 private void debounceProximitySensor() {
Jeff Brownec083212013-09-11 20:45:25 -07001003 if (mProximitySensorEnabled
1004 && mPendingProximity != PROXIMITY_UNKNOWN
1005 && mPendingProximityDebounceTime >= 0) {
Jeff Brown96307042012-07-27 15:51:34 -07001006 final long now = SystemClock.uptimeMillis();
1007 if (mPendingProximityDebounceTime <= now) {
Jeff Brownec083212013-09-11 20:45:25 -07001008 // Sensor reading accepted. Apply the change then release the wake lock.
Jeff Brown96307042012-07-27 15:51:34 -07001009 mProximity = mPendingProximity;
Jeff Brownec083212013-09-11 20:45:25 -07001010 updatePowerState();
1011 clearPendingProximityDebounceTime(); // release wake lock (must be last)
Jeff Brown96307042012-07-27 15:51:34 -07001012 } else {
Jeff Brownec083212013-09-11 20:45:25 -07001013 // Need to wait a little longer.
1014 // Debounce again later. We continue holding a wake lock while waiting.
Jeff Brown96307042012-07-27 15:51:34 -07001015 Message msg = mHandler.obtainMessage(MSG_PROXIMITY_SENSOR_DEBOUNCED);
1016 msg.setAsynchronous(true);
1017 mHandler.sendMessageAtTime(msg, mPendingProximityDebounceTime);
1018 }
1019 }
1020 }
1021
Jeff Brownec083212013-09-11 20:45:25 -07001022 private void clearPendingProximityDebounceTime() {
1023 if (mPendingProximityDebounceTime >= 0) {
1024 mPendingProximityDebounceTime = -1;
Jeff Brown131206b2014-04-08 17:27:14 -07001025 mCallbacks.releaseSuspendBlocker(); // release wake lock
Jeff Brownec083212013-09-11 20:45:25 -07001026 }
1027 }
1028
1029 private void setPendingProximityDebounceTime(long debounceTime) {
1030 if (mPendingProximityDebounceTime < 0) {
Jeff Brown131206b2014-04-08 17:27:14 -07001031 mCallbacks.acquireSuspendBlocker(); // acquire wake lock
Jeff Brownec083212013-09-11 20:45:25 -07001032 }
1033 mPendingProximityDebounceTime = debounceTime;
1034 }
1035
Jeff Brownec083212013-09-11 20:45:25 -07001036 private void sendOnStateChangedWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001037 mCallbacks.acquireSuspendBlocker();
1038 mHandler.post(mOnStateChangedRunnable);
Jeff Brown96307042012-07-27 15:51:34 -07001039 }
1040
1041 private final Runnable mOnStateChangedRunnable = new Runnable() {
1042 @Override
1043 public void run() {
1044 mCallbacks.onStateChanged();
Jeff Brown131206b2014-04-08 17:27:14 -07001045 mCallbacks.releaseSuspendBlocker();
Jeff Brown96307042012-07-27 15:51:34 -07001046 }
1047 };
1048
Jeff Brownec083212013-09-11 20:45:25 -07001049 private void sendOnProximityPositiveWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001050 mCallbacks.acquireSuspendBlocker();
1051 mHandler.post(mOnProximityPositiveRunnable);
Jeff Brown93cbbb22012-10-04 13:18:36 -07001052 }
1053
1054 private final Runnable mOnProximityPositiveRunnable = new Runnable() {
1055 @Override
1056 public void run() {
1057 mCallbacks.onProximityPositive();
Jeff Brown131206b2014-04-08 17:27:14 -07001058 mCallbacks.releaseSuspendBlocker();
Jeff Brown93cbbb22012-10-04 13:18:36 -07001059 }
1060 };
1061
Jeff Brownec083212013-09-11 20:45:25 -07001062 private void sendOnProximityNegativeWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001063 mCallbacks.acquireSuspendBlocker();
1064 mHandler.post(mOnProximityNegativeRunnable);
Jeff Brown96307042012-07-27 15:51:34 -07001065 }
1066
1067 private final Runnable mOnProximityNegativeRunnable = new Runnable() {
1068 @Override
1069 public void run() {
1070 mCallbacks.onProximityNegative();
Jeff Brown131206b2014-04-08 17:27:14 -07001071 mCallbacks.releaseSuspendBlocker();
Jeff Brown96307042012-07-27 15:51:34 -07001072 }
1073 };
1074
Jeff Brownbd6e1502012-08-28 03:27:37 -07001075 public void dump(final PrintWriter pw) {
Jeff Brown96307042012-07-27 15:51:34 -07001076 synchronized (mLock) {
1077 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001078 pw.println("Display Power Controller Locked State:");
Jeff Brown96307042012-07-27 15:51:34 -07001079 pw.println(" mDisplayReadyLocked=" + mDisplayReadyLocked);
1080 pw.println(" mPendingRequestLocked=" + mPendingRequestLocked);
1081 pw.println(" mPendingRequestChangedLocked=" + mPendingRequestChangedLocked);
1082 pw.println(" mPendingWaitForNegativeProximityLocked="
1083 + mPendingWaitForNegativeProximityLocked);
1084 pw.println(" mPendingUpdatePowerStateLocked=" + mPendingUpdatePowerStateLocked);
1085 }
1086
1087 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001088 pw.println("Display Power Controller Configuration:");
Jeff Brown26875502014-01-30 21:47:47 -08001089 pw.println(" mScreenBrightnessDozeConfig=" + mScreenBrightnessDozeConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001090 pw.println(" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig);
Jeff Brown1bfd0f42014-08-22 01:59:06 -07001091 pw.println(" mScreenBrightnessDarkConfig=" + mScreenBrightnessDarkConfig);
Jeff Brownb76eebff2012-10-05 22:26:44 -07001092 pw.println(" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum);
1093 pw.println(" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum);
Jeff Brown2175e9c2014-09-12 16:11:07 -07001094 pw.println(" mUseSoftwareAutoBrightnessConfig=" + mUseSoftwareAutoBrightnessConfig);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -07001095 pw.println(" mAllowAutoBrightnessWhileDozingConfig=" +
1096 mAllowAutoBrightnessWhileDozingConfig);
Jeff Brown2175e9c2014-09-12 16:11:07 -07001097 pw.println(" mColorFadeFadesConfig=" + mColorFadeFadesConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001098
Jeff Brownbd6e1502012-08-28 03:27:37 -07001099 mHandler.runWithScissors(new Runnable() {
1100 @Override
1101 public void run() {
1102 dumpLocal(pw);
Jeff Brown96307042012-07-27 15:51:34 -07001103 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001104 }, 1000);
Jeff Brown96307042012-07-27 15:51:34 -07001105 }
1106
1107 private void dumpLocal(PrintWriter pw) {
1108 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001109 pw.println("Display Power Controller Thread State:");
Jeff Brown96307042012-07-27 15:51:34 -07001110 pw.println(" mPowerRequest=" + mPowerRequest);
1111 pw.println(" mWaitingForNegativeProximity=" + mWaitingForNegativeProximity);
1112
1113 pw.println(" mProximitySensor=" + mProximitySensor);
1114 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1115 pw.println(" mProximityThreshold=" + mProximityThreshold);
1116 pw.println(" mProximity=" + proximityToString(mProximity));
1117 pw.println(" mPendingProximity=" + proximityToString(mPendingProximity));
1118 pw.println(" mPendingProximityDebounceTime="
1119 + TimeUtils.formatUptime(mPendingProximityDebounceTime));
1120 pw.println(" mScreenOffBecauseOfProximity=" + mScreenOffBecauseOfProximity);
Jeff Brown970d4132014-07-19 11:33:47 -07001121 pw.println(" mAppliedAutoBrightness=" + mAppliedAutoBrightness);
1122 pw.println(" mAppliedDimming=" + mAppliedDimming);
1123 pw.println(" mAppliedLowPower=" + mAppliedLowPower);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001124 pw.println(" mPendingScreenOnUnblocker=" + mPendingScreenOnUnblocker);
1125 pw.println(" mPendingScreenOff=" + mPendingScreenOff);
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001126 pw.println(" mReportedToPolicy=" + reportedToPolicyToString(mReportedScreenStateToPolicy));
Jeff Brown96307042012-07-27 15:51:34 -07001127
Jeff Brown42558692014-05-20 22:02:46 -07001128 pw.println(" mScreenBrightnessRampAnimator.isAnimating()=" +
1129 mScreenBrightnessRampAnimator.isAnimating());
1130
Michael Lentine0839adb2014-07-29 18:47:56 -07001131 if (mColorFadeOnAnimator != null) {
1132 pw.println(" mColorFadeOnAnimator.isStarted()=" +
1133 mColorFadeOnAnimator.isStarted());
Jeff Brown96307042012-07-27 15:51:34 -07001134 }
Michael Lentine0839adb2014-07-29 18:47:56 -07001135 if (mColorFadeOffAnimator != null) {
1136 pw.println(" mColorFadeOffAnimator.isStarted()=" +
1137 mColorFadeOffAnimator.isStarted());
Jeff Brown96307042012-07-27 15:51:34 -07001138 }
1139
1140 if (mPowerState != null) {
1141 mPowerState.dump(pw);
1142 }
Michael Wright639c8be2014-01-17 18:29:12 -08001143
1144 if (mAutomaticBrightnessController != null) {
1145 mAutomaticBrightnessController.dump(pw);
1146 }
1147
Jeff Brown96307042012-07-27 15:51:34 -07001148 }
1149
1150 private static String proximityToString(int state) {
1151 switch (state) {
1152 case PROXIMITY_UNKNOWN:
1153 return "Unknown";
1154 case PROXIMITY_NEGATIVE:
1155 return "Negative";
1156 case PROXIMITY_POSITIVE:
1157 return "Positive";
1158 default:
1159 return Integer.toString(state);
1160 }
1161 }
1162
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001163 private static String reportedToPolicyToString(int state) {
1164 switch (state) {
1165 case REPORTED_TO_POLICY_SCREEN_OFF:
1166 return "REPORTED_TO_POLICY_SCREEN_OFF";
1167 case REPORTED_TO_POLICY_SCREEN_TURNING_ON:
1168 return "REPORTED_TO_POLICY_SCREEN_TURNING_ON";
1169 case REPORTED_TO_POLICY_SCREEN_ON:
1170 return "REPORTED_TO_POLICY_SCREEN_ON";
1171 default:
1172 return Integer.toString(state);
1173 }
1174 }
1175
Michael Wright639c8be2014-01-17 18:29:12 -08001176 private static Spline createAutoBrightnessSpline(int[] lux, int[] brightness) {
1177 try {
1178 final int n = brightness.length;
1179 float[] x = new float[n];
1180 float[] y = new float[n];
1181 y[0] = normalizeAbsoluteBrightness(brightness[0]);
1182 for (int i = 1; i < n; i++) {
1183 x[i] = lux[i - 1];
1184 y[i] = normalizeAbsoluteBrightness(brightness[i]);
1185 }
1186
Michael Wright3e9a1342014-08-28 17:42:16 -07001187 Spline spline = Spline.createSpline(x, y);
Michael Wright639c8be2014-01-17 18:29:12 -08001188 if (DEBUG) {
1189 Slog.d(TAG, "Auto-brightness spline: " + spline);
1190 for (float v = 1f; v < lux[lux.length - 1] * 1.25f; v *= 1.25f) {
1191 Slog.d(TAG, String.format(" %7.1f: %7.1f", v, spline.interpolate(v)));
1192 }
1193 }
1194 return spline;
1195 } catch (IllegalArgumentException ex) {
1196 Slog.e(TAG, "Could not create auto-brightness spline.", ex);
1197 return null;
Jeff Brown96307042012-07-27 15:51:34 -07001198 }
Michael Wright639c8be2014-01-17 18:29:12 -08001199 }
1200
1201 private static float normalizeAbsoluteBrightness(int value) {
1202 return (float)clampAbsoluteBrightness(value) / PowerManager.BRIGHTNESS_ON;
1203 }
1204
1205 private static int clampAbsoluteBrightness(int value) {
1206 return MathUtils.constrain(value, PowerManager.BRIGHTNESS_OFF, PowerManager.BRIGHTNESS_ON);
Jeff Brown96307042012-07-27 15:51:34 -07001207 }
1208
Jeff Brown96307042012-07-27 15:51:34 -07001209 private final class DisplayControllerHandler extends Handler {
1210 public DisplayControllerHandler(Looper looper) {
Jeff Browna2910d02012-08-25 12:29:46 -07001211 super(looper, null, true /*async*/);
Jeff Brown96307042012-07-27 15:51:34 -07001212 }
1213
1214 @Override
1215 public void handleMessage(Message msg) {
1216 switch (msg.what) {
1217 case MSG_UPDATE_POWER_STATE:
1218 updatePowerState();
1219 break;
1220
1221 case MSG_PROXIMITY_SENSOR_DEBOUNCED:
1222 debounceProximitySensor();
1223 break;
Jeff Brown3ee549c2014-09-22 20:14:39 -07001224
1225 case MSG_SCREEN_ON_UNBLOCKED:
1226 if (mPendingScreenOnUnblocker == msg.obj) {
1227 unblockScreenOn();
1228 updatePowerState();
1229 }
1230 break;
Jeff Brown96307042012-07-27 15:51:34 -07001231 }
1232 }
1233 }
1234
1235 private final SensorEventListener mProximitySensorListener = new SensorEventListener() {
1236 @Override
1237 public void onSensorChanged(SensorEvent event) {
1238 if (mProximitySensorEnabled) {
1239 final long time = SystemClock.uptimeMillis();
1240 final float distance = event.values[0];
1241 boolean positive = distance >= 0.0f && distance < mProximityThreshold;
1242 handleProximitySensorEvent(time, positive);
1243 }
1244 }
1245
1246 @Override
1247 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1248 // Not used.
1249 }
1250 };
Jeff Brown3ee549c2014-09-22 20:14:39 -07001251
1252 private final class ScreenOnUnblocker implements WindowManagerPolicy.ScreenOnListener {
1253 @Override
1254 public void onScreenOn() {
1255 Message msg = mHandler.obtainMessage(MSG_SCREEN_ON_UNBLOCKED, this);
1256 msg.setAsynchronous(true);
1257 mHandler.sendMessage(msg);
1258 }
1259 }
Jeff Brown96307042012-07-27 15:51:34 -07001260}