blob: 8f8afd5615f3326136e217e7aa87c7da28e2f63c [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
Michael Wright639c8be2014-01-17 18:29:12 -080075 private static 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
Jeff Brown96307042012-07-27 15:51:34 -0700105 // Brightness animation ramp rate in brightness units per second.
Jeff Browne941b1e2012-10-22 16:50:25 -0700106 private static final int BRIGHTNESS_RAMP_RATE_SLOW = 40;
Jeff Brown96307042012-07-27 15:51:34 -0700107
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700108 private static final int REPORTED_TO_POLICY_SCREEN_OFF = 0;
109 private static final int REPORTED_TO_POLICY_SCREEN_TURNING_ON = 1;
110 private static final int REPORTED_TO_POLICY_SCREEN_ON = 2;
111
Jeff Brown96307042012-07-27 15:51:34 -0700112 private final Object mLock = new Object();
113
Michael Lentine0839adb2014-07-29 18:47:56 -0700114 private final Context mContext;
115
Jeff Brown96307042012-07-27 15:51:34 -0700116 // Our handler.
117 private final DisplayControllerHandler mHandler;
118
119 // Asynchronous callbacks into the power manager service.
120 // Only invoked from the handler thread while no locks are held.
Jeff Brown131206b2014-04-08 17:27:14 -0700121 private final DisplayPowerCallbacks mCallbacks;
122
123 // Battery stats.
124 private final IBatteryStats mBatteryStats;
Jeff Brown96307042012-07-27 15:51:34 -0700125
Jeff Brown96307042012-07-27 15:51:34 -0700126 // The sensor manager.
127 private final SensorManager mSensorManager;
128
Jeff Brown3ee549c2014-09-22 20:14:39 -0700129 // The window manager policy.
130 private final WindowManagerPolicy mWindowManagerPolicy;
131
Jeff Brown037c33e2014-04-09 00:31:55 -0700132 // The display blanker.
133 private final DisplayBlanker mBlanker;
134
Jeff Brown96307042012-07-27 15:51:34 -0700135 // The proximity sensor, or null if not available or needed.
136 private Sensor mProximitySensor;
137
Jeff Brown26875502014-01-30 21:47:47 -0800138 // The doze screen brightness.
139 private final int mScreenBrightnessDozeConfig;
140
Jeff Brown96307042012-07-27 15:51:34 -0700141 // The dim screen brightness.
142 private final int mScreenBrightnessDimConfig;
143
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700144 // The minimum screen brightness to use in a very dark room.
145 private final int mScreenBrightnessDarkConfig;
146
Jeff Brownb76eebff2012-10-05 22:26:44 -0700147 // The minimum allowed brightness.
148 private final int mScreenBrightnessRangeMinimum;
149
150 // The maximum allowed brightness.
151 private final int mScreenBrightnessRangeMaximum;
152
Jeff Brown330560f2012-08-21 22:10:57 -0700153 // True if auto-brightness should be used.
Jeff Brown96307042012-07-27 15:51:34 -0700154 private boolean mUseSoftwareAutoBrightnessConfig;
Jeff Brown330560f2012-08-21 22:10:57 -0700155
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700156 // True if should use light sensor to automatically determine doze screen brightness.
157 private final boolean mAllowAutoBrightnessWhileDozingConfig;
158
Jeff Brown252c2062012-10-08 16:21:01 -0700159 // True if we should fade the screen while turning it off, false if we should play
Jeff Brown3ee549c2014-09-22 20:14:39 -0700160 // a stylish color fade animation instead.
Michael Lentine0839adb2014-07-29 18:47:56 -0700161 private boolean mColorFadeFadesConfig;
Jeff Browna52772f2012-10-04 18:38:09 -0700162
Jeff Brown96307042012-07-27 15:51:34 -0700163 // The pending power request.
164 // Initially null until the first call to requestPowerState.
165 // Guarded by mLock.
166 private DisplayPowerRequest mPendingRequestLocked;
167
168 // True if a request has been made to wait for the proximity sensor to go negative.
169 // Guarded by mLock.
170 private boolean mPendingWaitForNegativeProximityLocked;
171
172 // True if the pending power request or wait for negative proximity flag
173 // has been changed since the last update occurred.
174 // Guarded by mLock.
175 private boolean mPendingRequestChangedLocked;
176
177 // Set to true when the important parts of the pending power request have been applied.
178 // The important parts are mainly the screen state. Brightness changes may occur
179 // concurrently.
180 // Guarded by mLock.
181 private boolean mDisplayReadyLocked;
182
183 // Set to true if a power state update is required.
184 // Guarded by mLock.
185 private boolean mPendingUpdatePowerStateLocked;
186
187 /* The following state must only be accessed by the handler thread. */
188
189 // The currently requested power state.
190 // The power controller will progressively update its internal state to match
191 // the requested power state. Initially null until the first update.
192 private DisplayPowerRequest mPowerRequest;
193
194 // The current power state.
195 // Must only be accessed on the handler thread.
196 private DisplayPowerState mPowerState;
197
198 // True if the device should wait for negative proximity sensor before
199 // waking up the screen. This is set to false as soon as a negative
200 // proximity sensor measurement is observed or when the device is forced to
201 // go to sleep by the user. While true, the screen remains off.
202 private boolean mWaitingForNegativeProximity;
203
204 // The actual proximity sensor threshold value.
205 private float mProximityThreshold;
206
207 // Set to true if the proximity sensor listener has been registered
208 // with the sensor manager.
209 private boolean mProximitySensorEnabled;
210
211 // The debounced proximity sensor state.
212 private int mProximity = PROXIMITY_UNKNOWN;
213
214 // The raw non-debounced proximity sensor state.
215 private int mPendingProximity = PROXIMITY_UNKNOWN;
Jeff Brownec083212013-09-11 20:45:25 -0700216 private long mPendingProximityDebounceTime = -1; // -1 if fully debounced
Jeff Brown96307042012-07-27 15:51:34 -0700217
218 // True if the screen was turned off because of the proximity sensor.
219 // When the screen turns on again, we report user activity to the power manager.
220 private boolean mScreenOffBecauseOfProximity;
221
Jeff Brown3ee549c2014-09-22 20:14:39 -0700222 // The currently active screen on unblocker. This field is non-null whenever
223 // we are waiting for a callback to release it and unblock the screen.
224 private ScreenOnUnblocker mPendingScreenOnUnblocker;
225
226 // True if we were in the process of turning off the screen.
227 // This allows us to recover more gracefully from situations where we abort
228 // turning off the screen.
229 private boolean mPendingScreenOff;
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700230
Jeff Brown0a434772014-09-30 14:42:27 -0700231 // True if we have unfinished business and are holding a suspend blocker.
232 private boolean mUnfinishedBusiness;
233
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700234 // The elapsed real time when the screen on was blocked.
235 private long mScreenOnBlockStartRealTime;
236
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700237 // Screen state we reported to policy. Must be one of REPORTED_TO_POLICY_SCREEN_* fields.
238 private int mReportedScreenStateToPolicy;
Jeff Browne1633ad2015-06-22 19:24:24 -0700239
Jeff Brown970d4132014-07-19 11:33:47 -0700240 // Remembers whether certain kinds of brightness adjustments
241 // were recently applied so that we can decide how to transition.
242 private boolean mAppliedAutoBrightness;
243 private boolean mAppliedDimming;
244 private boolean mAppliedLowPower;
Jeff Brown96307042012-07-27 15:51:34 -0700245
Prashant Malani99e6d432016-03-29 16:32:37 -0700246 // Brightness ramp rate fast.
247 private final int mBrightnessRampRateFast;
248
Michael Wright639c8be2014-01-17 18:29:12 -0800249 // The controller for the automatic brightness level.
250 private AutomaticBrightnessController mAutomaticBrightnessController;
251
Jeff Brown96307042012-07-27 15:51:34 -0700252 // Animators.
Michael Lentine0839adb2014-07-29 18:47:56 -0700253 private ObjectAnimator mColorFadeOnAnimator;
254 private ObjectAnimator mColorFadeOffAnimator;
Jeff Brown96307042012-07-27 15:51:34 -0700255 private RampAnimator<DisplayPowerState> mScreenBrightnessRampAnimator;
256
257 /**
258 * Creates the display power controller.
259 */
Jeff Brown131206b2014-04-08 17:27:14 -0700260 public DisplayPowerController(Context context,
Jeff Brown037c33e2014-04-09 00:31:55 -0700261 DisplayPowerCallbacks callbacks, Handler handler,
262 SensorManager sensorManager, DisplayBlanker blanker) {
Jeff Brown131206b2014-04-08 17:27:14 -0700263 mHandler = new DisplayControllerHandler(handler.getLooper());
Jeff Brown96307042012-07-27 15:51:34 -0700264 mCallbacks = callbacks;
Jeff Brown96307042012-07-27 15:51:34 -0700265
Jeff Brown131206b2014-04-08 17:27:14 -0700266 mBatteryStats = BatteryStatsService.getService();
Jeff Brown3b971592013-01-09 18:46:37 -0800267 mSensorManager = sensorManager;
Jeff Brown3ee549c2014-09-22 20:14:39 -0700268 mWindowManagerPolicy = LocalServices.getService(WindowManagerPolicy.class);
Jeff Brown037c33e2014-04-09 00:31:55 -0700269 mBlanker = blanker;
Michael Lentine0839adb2014-07-29 18:47:56 -0700270 mContext = context;
Jeff Brown96307042012-07-27 15:51:34 -0700271
272 final Resources resources = context.getResources();
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700273 final int screenBrightnessSettingMinimum = clampAbsoluteBrightness(resources.getInteger(
274 com.android.internal.R.integer.config_screenBrightnessSettingMinimum));
Jeff Brownb76eebff2012-10-05 22:26:44 -0700275
Jeff Brown26875502014-01-30 21:47:47 -0800276 mScreenBrightnessDozeConfig = clampAbsoluteBrightness(resources.getInteger(
277 com.android.internal.R.integer.config_screenBrightnessDoze));
278
Jeff Brownb76eebff2012-10-05 22:26:44 -0700279 mScreenBrightnessDimConfig = clampAbsoluteBrightness(resources.getInteger(
280 com.android.internal.R.integer.config_screenBrightnessDim));
281
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700282 mScreenBrightnessDarkConfig = clampAbsoluteBrightness(resources.getInteger(
283 com.android.internal.R.integer.config_screenBrightnessDark));
284 if (mScreenBrightnessDarkConfig > mScreenBrightnessDimConfig) {
285 Slog.w(TAG, "Expected config_screenBrightnessDark ("
286 + mScreenBrightnessDarkConfig + ") to be less than or equal to "
287 + "config_screenBrightnessDim (" + mScreenBrightnessDimConfig + ").");
288 }
289 if (mScreenBrightnessDarkConfig > mScreenBrightnessDimConfig) {
290 Slog.w(TAG, "Expected config_screenBrightnessDark ("
291 + mScreenBrightnessDarkConfig + ") to be less than or equal to "
292 + "config_screenBrightnessSettingMinimum ("
293 + screenBrightnessSettingMinimum + ").");
294 }
295
296 int screenBrightnessRangeMinimum = Math.min(Math.min(
297 screenBrightnessSettingMinimum, mScreenBrightnessDimConfig),
298 mScreenBrightnessDarkConfig);
Michael Wright639c8be2014-01-17 18:29:12 -0800299
300 mScreenBrightnessRangeMaximum = PowerManager.BRIGHTNESS_ON;
Jeff Brownb76eebff2012-10-05 22:26:44 -0700301
Jeff Brown96307042012-07-27 15:51:34 -0700302 mUseSoftwareAutoBrightnessConfig = resources.getBoolean(
303 com.android.internal.R.bool.config_automatic_brightness_available);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700304
305 mAllowAutoBrightnessWhileDozingConfig = resources.getBoolean(
306 com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing);
307
Prashant Malani99e6d432016-03-29 16:32:37 -0700308 mBrightnessRampRateFast = resources.getInteger(
309 com.android.internal.R.integer.config_brightness_ramp_rate_fast);
310
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);
Zoran Jovanovic6fc42f52015-12-10 17:01:16 +0100319 int ambientLightHorizon = resources.getInteger(
320 com.android.internal.R.integer.config_autoBrightnessAmbientLightHorizon);
321 float autoBrightnessAdjustmentMaxGamma = resources.getFraction(
322 com.android.internal.R.fraction.config_autoBrightnessAdjustmentMaxGamma,
323 1, 1);
Filip Gruszczynskid81ecd12015-02-06 12:38:47 -0800324
Jeff Brown96307042012-07-27 15:51:34 -0700325 if (mUseSoftwareAutoBrightnessConfig) {
Jeff Brown1a30b552012-08-16 01:31:11 -0700326 int[] lux = resources.getIntArray(
Jeff Brown96307042012-07-27 15:51:34 -0700327 com.android.internal.R.array.config_autoBrightnessLevels);
Jeff Brown1a30b552012-08-16 01:31:11 -0700328 int[] screenBrightness = resources.getIntArray(
Jeff Brown96307042012-07-27 15:51:34 -0700329 com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
Michael Wright639c8be2014-01-17 18:29:12 -0800330 int lightSensorWarmUpTimeConfig = resources.getInteger(
331 com.android.internal.R.integer.config_lightSensorWarmupTime);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700332 final float dozeScaleFactor = resources.getFraction(
333 com.android.internal.R.fraction.config_screenAutoBrightnessDozeScaleFactor,
334 1, 1);
Jeff Brown1a30b552012-08-16 01:31:11 -0700335
Michael Wright639c8be2014-01-17 18:29:12 -0800336 Spline screenAutoBrightnessSpline = createAutoBrightnessSpline(lux, screenBrightness);
337 if (screenAutoBrightnessSpline == null) {
Jeff Brown96307042012-07-27 15:51:34 -0700338 Slog.e(TAG, "Error in config.xml. config_autoBrightnessLcdBacklightValues "
Jeff Brown1a30b552012-08-16 01:31:11 -0700339 + "(size " + screenBrightness.length + ") "
340 + "must be monotic and have exactly one more entry than "
341 + "config_autoBrightnessLevels (size " + lux.length + ") "
342 + "which must be strictly increasing. "
Jeff Brown96307042012-07-27 15:51:34 -0700343 + "Auto-brightness will be disabled.");
344 mUseSoftwareAutoBrightnessConfig = false;
Jeff Brownb76eebff2012-10-05 22:26:44 -0700345 } else {
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700346 int bottom = clampAbsoluteBrightness(screenBrightness[0]);
347 if (mScreenBrightnessDarkConfig > bottom) {
348 Slog.w(TAG, "config_screenBrightnessDark (" + mScreenBrightnessDarkConfig
349 + ") should be less than or equal to the first value of "
350 + "config_autoBrightnessLcdBacklightValues ("
351 + bottom + ").");
352 }
353 if (bottom < screenBrightnessRangeMinimum) {
354 screenBrightnessRangeMinimum = bottom;
Jeff Brownb76eebff2012-10-05 22:26:44 -0700355 }
Jeff Brown131206b2014-04-08 17:27:14 -0700356 mAutomaticBrightnessController = new AutomaticBrightnessController(this,
357 handler.getLooper(), sensorManager, screenAutoBrightnessSpline,
Michael Wright639c8be2014-01-17 18:29:12 -0800358 lightSensorWarmUpTimeConfig, screenBrightnessRangeMinimum,
Filip Gruszczynskid81ecd12015-02-06 12:38:47 -0800359 mScreenBrightnessRangeMaximum, dozeScaleFactor, lightSensorRate,
360 brighteningLightDebounce, darkeningLightDebounce,
Zoran Jovanovic6fc42f52015-12-10 17:01:16 +0100361 autoBrightnessResetAmbientLuxAfterWarmUp,
362 ambientLightHorizon, autoBrightnessAdjustmentMaxGamma);
Jeff Brown96307042012-07-27 15:51:34 -0700363 }
Jeff Brown96307042012-07-27 15:51:34 -0700364 }
365
Michael Wright639c8be2014-01-17 18:29:12 -0800366 mScreenBrightnessRangeMinimum = screenBrightnessRangeMinimum;
Jeff Brownb76eebff2012-10-05 22:26:44 -0700367
Michael Lentine0839adb2014-07-29 18:47:56 -0700368 mColorFadeFadesConfig = resources.getBoolean(
Jeff Browna52772f2012-10-04 18:38:09 -0700369 com.android.internal.R.bool.config_animateScreenLights);
370
Jeff Brown96307042012-07-27 15:51:34 -0700371 if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) {
372 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
373 if (mProximitySensor != null) {
374 mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(),
375 TYPICAL_PROXIMITY_THRESHOLD);
376 }
377 }
378
Jeff Brown1a30b552012-08-16 01:31:11 -0700379 }
380
Jeff Brown96307042012-07-27 15:51:34 -0700381 /**
382 * Returns true if the proximity sensor screen-off function is available.
383 */
384 public boolean isProximitySensorAvailable() {
385 return mProximitySensor != null;
386 }
387
388 /**
389 * Requests a new power state.
390 * The controller makes a copy of the provided object and then
391 * begins adjusting the power state to match what was requested.
392 *
393 * @param request The requested power state.
394 * @param waitForNegativeProximity If true, issues a request to wait for
395 * negative proximity before turning the screen back on, assuming the screen
396 * was turned off by the proximity sensor.
397 * @return True if display is ready, false if there are important changes that must
398 * be made asynchronously (such as turning the screen on), in which case the caller
Jeff Brown606e4e82014-09-18 15:22:26 -0700399 * should grab a wake lock, watch for {@link DisplayPowerCallbacks#onStateChanged()}
400 * then try the request again later until the state converges.
Jeff Brown96307042012-07-27 15:51:34 -0700401 */
402 public boolean requestPowerState(DisplayPowerRequest request,
403 boolean waitForNegativeProximity) {
404 if (DEBUG) {
405 Slog.d(TAG, "requestPowerState: "
406 + request + ", waitForNegativeProximity=" + waitForNegativeProximity);
407 }
408
409 synchronized (mLock) {
410 boolean changed = false;
411
412 if (waitForNegativeProximity
413 && !mPendingWaitForNegativeProximityLocked) {
414 mPendingWaitForNegativeProximityLocked = true;
415 changed = true;
416 }
417
418 if (mPendingRequestLocked == null) {
419 mPendingRequestLocked = new DisplayPowerRequest(request);
420 changed = true;
421 } else if (!mPendingRequestLocked.equals(request)) {
422 mPendingRequestLocked.copyFrom(request);
423 changed = true;
424 }
425
426 if (changed) {
427 mDisplayReadyLocked = false;
428 }
429
430 if (changed && !mPendingRequestChangedLocked) {
431 mPendingRequestChangedLocked = true;
432 sendUpdatePowerStateLocked();
433 }
434
435 return mDisplayReadyLocked;
436 }
437 }
438
439 private void sendUpdatePowerState() {
440 synchronized (mLock) {
441 sendUpdatePowerStateLocked();
442 }
443 }
444
445 private void sendUpdatePowerStateLocked() {
446 if (!mPendingUpdatePowerStateLocked) {
447 mPendingUpdatePowerStateLocked = true;
448 Message msg = mHandler.obtainMessage(MSG_UPDATE_POWER_STATE);
449 msg.setAsynchronous(true);
450 mHandler.sendMessage(msg);
451 }
452 }
453
454 private void initialize() {
Jeff Brown037c33e2014-04-09 00:31:55 -0700455 // Initialize the power state object for the default display.
456 // In the future, we might manage multiple displays independently.
457 mPowerState = new DisplayPowerState(mBlanker,
Michael Lentine0839adb2014-07-29 18:47:56 -0700458 new ColorFade(Display.DEFAULT_DISPLAY));
Jeff Brown96307042012-07-27 15:51:34 -0700459
Michael Lentine0839adb2014-07-29 18:47:56 -0700460 mColorFadeOnAnimator = ObjectAnimator.ofFloat(
461 mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 0.0f, 1.0f);
462 mColorFadeOnAnimator.setDuration(COLOR_FADE_ON_ANIMATION_DURATION_MILLIS);
463 mColorFadeOnAnimator.addListener(mAnimatorListener);
Jeff Brown96307042012-07-27 15:51:34 -0700464
Michael Lentine0839adb2014-07-29 18:47:56 -0700465 mColorFadeOffAnimator = ObjectAnimator.ofFloat(
466 mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 1.0f, 0.0f);
467 mColorFadeOffAnimator.setDuration(COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS);
468 mColorFadeOffAnimator.addListener(mAnimatorListener);
Jeff Brown96307042012-07-27 15:51:34 -0700469
470 mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>(
471 mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS);
Jeff Brown42558692014-05-20 22:02:46 -0700472 mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800473
Jeff Brown131206b2014-04-08 17:27:14 -0700474 // Initialize screen state for battery stats.
475 try {
Jeff Browne95c3cd2014-05-02 16:59:26 -0700476 mBatteryStats.noteScreenState(mPowerState.getScreenState());
Jeff Brown131206b2014-04-08 17:27:14 -0700477 mBatteryStats.noteScreenBrightness(mPowerState.getScreenBrightness());
478 } catch (RemoteException ex) {
479 // same process
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800480 }
Jeff Brown96307042012-07-27 15:51:34 -0700481 }
482
483 private final Animator.AnimatorListener mAnimatorListener = new Animator.AnimatorListener() {
484 @Override
485 public void onAnimationStart(Animator animation) {
486 }
487 @Override
488 public void onAnimationEnd(Animator animation) {
489 sendUpdatePowerState();
490 }
491 @Override
492 public void onAnimationRepeat(Animator animation) {
493 }
494 @Override
495 public void onAnimationCancel(Animator animation) {
496 }
497 };
498
Jeff Brown42558692014-05-20 22:02:46 -0700499 private final RampAnimator.Listener mRampAnimatorListener = new RampAnimator.Listener() {
500 @Override
501 public void onAnimationEnd() {
502 sendUpdatePowerState();
503 }
504 };
505
Jeff Brown96307042012-07-27 15:51:34 -0700506 private void updatePowerState() {
507 // Update the power state request.
508 final boolean mustNotify;
509 boolean mustInitialize = false;
Adrian Roos6da87ab2014-05-16 23:09:41 +0200510 boolean autoBrightnessAdjustmentChanged = false;
Jeff Brown330560f2012-08-21 22:10:57 -0700511
Jeff Brown96307042012-07-27 15:51:34 -0700512 synchronized (mLock) {
513 mPendingUpdatePowerStateLocked = false;
514 if (mPendingRequestLocked == null) {
515 return; // wait until first actual power request
516 }
517
518 if (mPowerRequest == null) {
519 mPowerRequest = new DisplayPowerRequest(mPendingRequestLocked);
520 mWaitingForNegativeProximity = mPendingWaitForNegativeProximityLocked;
Jeff Brown6307a152012-08-20 13:24:23 -0700521 mPendingWaitForNegativeProximityLocked = false;
Jeff Brown96307042012-07-27 15:51:34 -0700522 mPendingRequestChangedLocked = false;
523 mustInitialize = true;
524 } else if (mPendingRequestChangedLocked) {
Adrian Roos6da87ab2014-05-16 23:09:41 +0200525 autoBrightnessAdjustmentChanged = (mPowerRequest.screenAutoBrightnessAdjustment
526 != mPendingRequestLocked.screenAutoBrightnessAdjustment);
Jeff Brown96307042012-07-27 15:51:34 -0700527 mPowerRequest.copyFrom(mPendingRequestLocked);
528 mWaitingForNegativeProximity |= mPendingWaitForNegativeProximityLocked;
Jeff Brown6307a152012-08-20 13:24:23 -0700529 mPendingWaitForNegativeProximityLocked = false;
Jeff Brown96307042012-07-27 15:51:34 -0700530 mPendingRequestChangedLocked = false;
531 mDisplayReadyLocked = false;
532 }
533
534 mustNotify = !mDisplayReadyLocked;
535 }
536
537 // Initialize things the first time the power state is changed.
538 if (mustInitialize) {
539 initialize();
540 }
541
Jeff Brown970d4132014-07-19 11:33:47 -0700542 // Compute the basic display state using the policy.
543 // We might override this below based on other factors.
544 int state;
545 int brightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700546 boolean performScreenOffTransition = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700547 switch (mPowerRequest.policy) {
548 case DisplayPowerRequest.POLICY_OFF:
549 state = Display.STATE_OFF;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700550 performScreenOffTransition = true;
Jeff Brown970d4132014-07-19 11:33:47 -0700551 break;
552 case DisplayPowerRequest.POLICY_DOZE:
553 if (mPowerRequest.dozeScreenState != Display.STATE_UNKNOWN) {
554 state = mPowerRequest.dozeScreenState;
555 } else {
556 state = Display.STATE_DOZE;
557 }
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700558 if (!mAllowAutoBrightnessWhileDozingConfig) {
559 brightness = mPowerRequest.dozeScreenBrightness;
560 }
Jeff Brown970d4132014-07-19 11:33:47 -0700561 break;
562 case DisplayPowerRequest.POLICY_DIM:
563 case DisplayPowerRequest.POLICY_BRIGHT:
564 default:
565 state = Display.STATE_ON;
566 break;
567 }
Jeff Brown2175e9c2014-09-12 16:11:07 -0700568 assert(state != Display.STATE_UNKNOWN);
Jeff Brown970d4132014-07-19 11:33:47 -0700569
Jeff Brown6307a152012-08-20 13:24:23 -0700570 // Apply the proximity sensor.
Jeff Brown96307042012-07-27 15:51:34 -0700571 if (mProximitySensor != null) {
Jeff Brown970d4132014-07-19 11:33:47 -0700572 if (mPowerRequest.useProximitySensor && state != Display.STATE_OFF) {
Jeff Brown6307a152012-08-20 13:24:23 -0700573 setProximitySensorEnabled(true);
574 if (!mScreenOffBecauseOfProximity
575 && mProximity == PROXIMITY_POSITIVE) {
576 mScreenOffBecauseOfProximity = true;
Jeff Brownec083212013-09-11 20:45:25 -0700577 sendOnProximityPositiveWithWakelock();
Jeff Brown6307a152012-08-20 13:24:23 -0700578 }
579 } else if (mWaitingForNegativeProximity
580 && mScreenOffBecauseOfProximity
581 && mProximity == PROXIMITY_POSITIVE
Jeff Brown970d4132014-07-19 11:33:47 -0700582 && state != Display.STATE_OFF) {
Jeff Brown6307a152012-08-20 13:24:23 -0700583 setProximitySensorEnabled(true);
584 } else {
585 setProximitySensorEnabled(false);
586 mWaitingForNegativeProximity = false;
587 }
588 if (mScreenOffBecauseOfProximity
589 && mProximity != PROXIMITY_POSITIVE) {
Jeff Brown96307042012-07-27 15:51:34 -0700590 mScreenOffBecauseOfProximity = false;
Jeff Brownec083212013-09-11 20:45:25 -0700591 sendOnProximityNegativeWithWakelock();
Jeff Brown96307042012-07-27 15:51:34 -0700592 }
Jeff Brown6307a152012-08-20 13:24:23 -0700593 } else {
594 mWaitingForNegativeProximity = false;
Jeff Brown96307042012-07-27 15:51:34 -0700595 }
Jeff Brown970d4132014-07-19 11:33:47 -0700596 if (mScreenOffBecauseOfProximity) {
597 state = Display.STATE_OFF;
Jeff Brown96307042012-07-27 15:51:34 -0700598 }
599
Jeff Brownbf4e4142014-10-02 13:08:05 -0700600 // Animate the screen state change unless already animating.
601 // The transition may be deferred, so after this point we will use the
602 // actual state instead of the desired one.
603 animateScreenStateChange(state, performScreenOffTransition);
604 state = mPowerState.getScreenState();
605
Jeff Brown970d4132014-07-19 11:33:47 -0700606 // Use zero brightness when screen is off.
607 if (state == Display.STATE_OFF) {
608 brightness = PowerManager.BRIGHTNESS_OFF;
609 }
610
Jeff Brown970d4132014-07-19 11:33:47 -0700611 // Configure auto-brightness.
612 boolean autoBrightnessEnabled = false;
613 if (mAutomaticBrightnessController != null) {
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700614 final boolean autoBrightnessEnabledInDoze = mAllowAutoBrightnessWhileDozingConfig
615 && (state == Display.STATE_DOZE || state == Display.STATE_DOZE_SUSPEND);
Jeff Brown970d4132014-07-19 11:33:47 -0700616 autoBrightnessEnabled = mPowerRequest.useAutoBrightness
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700617 && (state == Display.STATE_ON || autoBrightnessEnabledInDoze)
618 && brightness < 0;
Jeff Browna576b4d2015-04-23 19:58:06 -0700619 final boolean userInitiatedChange = autoBrightnessAdjustmentChanged
620 && mPowerRequest.brightnessSetByUser;
Jeff Brown970d4132014-07-19 11:33:47 -0700621 mAutomaticBrightnessController.configure(autoBrightnessEnabled,
Jeff Browna576b4d2015-04-23 19:58:06 -0700622 mPowerRequest.screenAutoBrightnessAdjustment, state != Display.STATE_ON,
Jason Monk5dbd4aa2016-02-07 13:13:39 -0500623 userInitiatedChange, mPowerRequest.useTwilight);
Jeff Brown970d4132014-07-19 11:33:47 -0700624 }
625
Jeff Brown7b5be5e2014-11-12 18:45:31 -0800626 // Apply brightness boost.
627 // We do this here after configuring auto-brightness so that we don't
628 // disable the light sensor during this temporary state. That way when
629 // boost ends we will be able to resume normal auto-brightness behavior
630 // without any delay.
631 if (mPowerRequest.boostScreenBrightness
632 && brightness != PowerManager.BRIGHTNESS_OFF) {
633 brightness = PowerManager.BRIGHTNESS_ON;
634 }
635
Jeff Brown970d4132014-07-19 11:33:47 -0700636 // Apply auto-brightness.
637 boolean slowChange = false;
638 if (brightness < 0) {
639 if (autoBrightnessEnabled) {
640 brightness = mAutomaticBrightnessController.getAutomaticScreenBrightness();
641 }
642 if (brightness >= 0) {
643 // Use current auto-brightness value and slowly adjust to changes.
644 brightness = clampScreenBrightness(brightness);
645 if (mAppliedAutoBrightness && !autoBrightnessAdjustmentChanged) {
646 slowChange = true; // slowly adapt to auto-brightness
647 }
648 mAppliedAutoBrightness = true;
Jeff Brown96307042012-07-27 15:51:34 -0700649 } else {
Jeff Brown970d4132014-07-19 11:33:47 -0700650 mAppliedAutoBrightness = false;
Jeff Brown96307042012-07-27 15:51:34 -0700651 }
652 } else {
Jeff Brown970d4132014-07-19 11:33:47 -0700653 mAppliedAutoBrightness = false;
654 }
655
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700656 // Use default brightness when dozing unless overridden.
657 if (brightness < 0 && (state == Display.STATE_DOZE
658 || state == Display.STATE_DOZE_SUSPEND)) {
659 brightness = mScreenBrightnessDozeConfig;
660 }
661
Jeff Brown970d4132014-07-19 11:33:47 -0700662 // Apply manual brightness.
663 // Use the current brightness setting from the request, which is expected
664 // provide a nominal default value for the case where auto-brightness
665 // is not ready yet.
666 if (brightness < 0) {
667 brightness = clampScreenBrightness(mPowerRequest.screenBrightness);
668 }
669
670 // Apply dimming by at least some minimum amount when user activity
671 // timeout is about to expire.
672 if (mPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) {
Jeff Brown5c8ea082014-07-24 19:30:31 -0700673 if (brightness > mScreenBrightnessRangeMinimum) {
674 brightness = Math.max(Math.min(brightness - SCREEN_DIM_MINIMUM_REDUCTION,
675 mScreenBrightnessDimConfig), mScreenBrightnessRangeMinimum);
676 }
Jeff Brown970d4132014-07-19 11:33:47 -0700677 if (!mAppliedDimming) {
678 slowChange = false;
679 }
680 mAppliedDimming = true;
mochangmingf0cb46c2015-12-29 13:55:24 +0800681 } else if (mAppliedDimming) {
682 slowChange = false;
683 mAppliedDimming = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700684 }
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;
mochangmingf0cb46c2015-12-29 13:55:24 +0800696 } else if (mAppliedLowPower) {
697 slowChange = false;
698 mAppliedLowPower = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700699 }
700
Jeff Brown0a434772014-09-30 14:42:27 -0700701 // Animate the screen brightness when the screen is on or dozing.
702 // Skip the animation when the screen is off or suspended.
Prashant Malani33538242014-11-13 14:04:00 -0800703 if (!mPendingScreenOff) {
704 if (state == Display.STATE_ON || state == Display.STATE_DOZE) {
705 animateScreenBrightness(brightness,
Prashant Malani99e6d432016-03-29 16:32:37 -0700706 slowChange ? BRIGHTNESS_RAMP_RATE_SLOW : mBrightnessRampRateFast);
Prashant Malani33538242014-11-13 14:04:00 -0800707 } else {
708 animateScreenBrightness(brightness, 0);
709 }
Jeff Brown0a434772014-09-30 14:42:27 -0700710 }
711
712 // Determine whether the display is ready for use in the newly requested state.
713 // Note that we do not wait for the brightness ramp animation to complete before
714 // reporting the display is ready because we only need to ensure the screen is in the
715 // right power state even as it continues to converge on the desired brightness.
716 final boolean ready = mPendingScreenOnUnblocker == null
Michael Lentine0839adb2014-07-29 18:47:56 -0700717 && !mColorFadeOnAnimator.isStarted()
718 && !mColorFadeOffAnimator.isStarted()
Jeff Brown0a434772014-09-30 14:42:27 -0700719 && mPowerState.waitUntilClean(mCleanListener);
720 final boolean finished = ready
721 && !mScreenBrightnessRampAnimator.isAnimating();
722
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700723 // Notify policy about screen turned on.
724 if (ready && state != Display.STATE_OFF
725 && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_ON) {
726 mReportedScreenStateToPolicy = REPORTED_TO_POLICY_SCREEN_ON;
727 mWindowManagerPolicy.screenTurnedOn();
728 }
729
Jeff Brown0a434772014-09-30 14:42:27 -0700730 // Grab a wake lock if we have unfinished business.
731 if (!finished && !mUnfinishedBusiness) {
732 if (DEBUG) {
733 Slog.d(TAG, "Unfinished business...");
734 }
735 mCallbacks.acquireSuspendBlocker();
736 mUnfinishedBusiness = true;
737 }
738
739 // Notify the power manager when ready.
740 if (ready && mustNotify) {
741 // Send state change.
Jeff Brown96307042012-07-27 15:51:34 -0700742 synchronized (mLock) {
743 if (!mPendingRequestChangedLocked) {
744 mDisplayReadyLocked = true;
Jeff Brownc38c9be2012-10-04 13:16:19 -0700745
746 if (DEBUG) {
747 Slog.d(TAG, "Display ready!");
748 }
Jeff Brown96307042012-07-27 15:51:34 -0700749 }
750 }
Jeff Brownec083212013-09-11 20:45:25 -0700751 sendOnStateChangedWithWakelock();
Jeff Brown96307042012-07-27 15:51:34 -0700752 }
Jeff Brown0a434772014-09-30 14:42:27 -0700753
754 // Release the wake lock when we have no unfinished business.
755 if (finished && mUnfinishedBusiness) {
756 if (DEBUG) {
757 Slog.d(TAG, "Finished business...");
758 }
759 mUnfinishedBusiness = false;
760 mCallbacks.releaseSuspendBlocker();
761 }
Jeff Brown96307042012-07-27 15:51:34 -0700762 }
763
Michael Wright639c8be2014-01-17 18:29:12 -0800764 @Override
765 public void updateBrightness() {
766 sendUpdatePowerState();
767 }
768
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700769 private void blockScreenOn() {
Jeff Brown3ee549c2014-09-22 20:14:39 -0700770 if (mPendingScreenOnUnblocker == null) {
Jeff Brown3edf5272014-08-14 19:25:14 -0700771 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0);
Jeff Brown3ee549c2014-09-22 20:14:39 -0700772 mPendingScreenOnUnblocker = new ScreenOnUnblocker();
Jeff Brown037c33e2014-04-09 00:31:55 -0700773 mScreenOnBlockStartRealTime = SystemClock.elapsedRealtime();
Jeff Brown3edf5272014-08-14 19:25:14 -0700774 Slog.i(TAG, "Blocking screen on until initial contents have been drawn.");
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700775 }
776 }
777
778 private void unblockScreenOn() {
Jeff Brown3ee549c2014-09-22 20:14:39 -0700779 if (mPendingScreenOnUnblocker != null) {
780 mPendingScreenOnUnblocker = null;
Jeff Brown2d8a3902014-03-11 23:02:35 -0700781 long delay = SystemClock.elapsedRealtime() - mScreenOnBlockStartRealTime;
Jeff Brown3edf5272014-08-14 19:25:14 -0700782 Slog.i(TAG, "Unblocked screen on after " + delay + " ms");
783 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0);
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700784 }
785 }
786
Jeff Brown3ee549c2014-09-22 20:14:39 -0700787 private boolean setScreenState(int state) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700788 if (mPowerState.getScreenState() != state) {
Jeff Brown3ee549c2014-09-22 20:14:39 -0700789 final boolean wasOn = (mPowerState.getScreenState() != Display.STATE_OFF);
Jeff Brown037c33e2014-04-09 00:31:55 -0700790 mPowerState.setScreenState(state);
Jeff Brown3ee549c2014-09-22 20:14:39 -0700791
792 // Tell battery stats about the transition.
Jeff Brown131206b2014-04-08 17:27:14 -0700793 try {
Jeff Browne95c3cd2014-05-02 16:59:26 -0700794 mBatteryStats.noteScreenState(state);
Jeff Brown131206b2014-04-08 17:27:14 -0700795 } catch (RemoteException ex) {
796 // same process
Jeff Brown96307042012-07-27 15:51:34 -0700797 }
798 }
Jeff Browne1633ad2015-06-22 19:24:24 -0700799
800 // Tell the window manager policy when the screen is turned off or on unless it's due
801 // to the proximity sensor. We temporarily block turning the screen on until the
802 // window manager is ready by leaving a black surface covering the screen.
803 // This surface is essentially the final state of the color fade animation and
804 // it is only removed once the window manager tells us that the activity has
805 // finished drawing underneath.
806 final boolean isOff = (state == Display.STATE_OFF);
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700807 if (isOff && mReportedScreenStateToPolicy != REPORTED_TO_POLICY_SCREEN_OFF
808 && !mScreenOffBecauseOfProximity) {
809 mReportedScreenStateToPolicy = REPORTED_TO_POLICY_SCREEN_OFF;
Jeff Browne1633ad2015-06-22 19:24:24 -0700810 unblockScreenOn();
811 mWindowManagerPolicy.screenTurnedOff();
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700812 } else if (!isOff && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_OFF) {
813 mReportedScreenStateToPolicy = REPORTED_TO_POLICY_SCREEN_TURNING_ON;
Jeff Browne1633ad2015-06-22 19:24:24 -0700814 if (mPowerState.getColorFadeLevel() == 0.0f) {
815 blockScreenOn();
816 } else {
817 unblockScreenOn();
818 }
819 mWindowManagerPolicy.screenTurningOn(mPendingScreenOnUnblocker);
820 }
821
822 // Return true if the screen isn't blocked.
Jeff Brown3ee549c2014-09-22 20:14:39 -0700823 return mPendingScreenOnUnblocker == null;
Jeff Brown96307042012-07-27 15:51:34 -0700824 }
825
Jeff Brown330560f2012-08-21 22:10:57 -0700826 private int clampScreenBrightness(int value) {
Michael Wright639c8be2014-01-17 18:29:12 -0800827 return MathUtils.constrain(
828 value, mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum);
Jeff Brown330560f2012-08-21 22:10:57 -0700829 }
830
Jeff Brown96307042012-07-27 15:51:34 -0700831 private void animateScreenBrightness(int target, int rate) {
Jeff Brown0a434772014-09-30 14:42:27 -0700832 if (DEBUG) {
833 Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate);
834 }
Jeff Brown96307042012-07-27 15:51:34 -0700835 if (mScreenBrightnessRampAnimator.animateTo(target, rate)) {
Jeff Brown131206b2014-04-08 17:27:14 -0700836 try {
837 mBatteryStats.noteScreenBrightness(target);
838 } catch (RemoteException ex) {
839 // same process
840 }
Jeff Brown96307042012-07-27 15:51:34 -0700841 }
842 }
843
Jeff Brown606e4e82014-09-18 15:22:26 -0700844 private void animateScreenStateChange(int target, boolean performScreenOffTransition) {
845 // If there is already an animation in progress, don't interfere with it.
846 if (mColorFadeOnAnimator.isStarted()
847 || mColorFadeOffAnimator.isStarted()) {
Chong Zhangb131c702016-02-16 16:31:48 -0800848 if (target != Display.STATE_ON) {
849 return;
850 }
851 // If display state changed to on, proceed and stop the color fade and turn screen on.
852 mPendingScreenOff = false;
Jeff Brown606e4e82014-09-18 15:22:26 -0700853 }
854
Jeff Brown3ee549c2014-09-22 20:14:39 -0700855 // If we were in the process of turning off the screen but didn't quite
856 // finish. Then finish up now to prevent a jarring transition back
857 // to screen on if we skipped blocking screen on as usual.
858 if (mPendingScreenOff && target != Display.STATE_OFF) {
859 setScreenState(Display.STATE_OFF);
860 mPendingScreenOff = false;
Michael Lentined73854d2015-09-25 10:55:26 -0700861 mPowerState.dismissColorFadeResources();
Jeff Brown606e4e82014-09-18 15:22:26 -0700862 }
863
864 if (target == Display.STATE_ON) {
865 // Want screen on. The contents of the screen may not yet
Jeff Brown3ee549c2014-09-22 20:14:39 -0700866 // be visible if the color fade has not been dismissed because
Jeff Brown606e4e82014-09-18 15:22:26 -0700867 // its last frame of animation is solid black.
Jeff Brown3ee549c2014-09-22 20:14:39 -0700868 if (!setScreenState(Display.STATE_ON)) {
869 return; // screen on blocked
870 }
Jeff Brown606e4e82014-09-18 15:22:26 -0700871 if (USE_COLOR_FADE_ON_ANIMATION && mPowerRequest.isBrightOrDim()) {
872 // Perform screen on animation.
873 if (mPowerState.getColorFadeLevel() == 1.0f) {
874 mPowerState.dismissColorFade();
875 } else if (mPowerState.prepareColorFade(mContext,
876 mColorFadeFadesConfig ?
877 ColorFade.MODE_FADE :
878 ColorFade.MODE_WARM_UP)) {
879 mColorFadeOnAnimator.start();
880 } else {
881 mColorFadeOnAnimator.end();
882 }
883 } else {
884 // Skip screen on animation.
885 mPowerState.setColorFadeLevel(1.0f);
886 mPowerState.dismissColorFade();
887 }
888 } else if (target == Display.STATE_DOZE) {
889 // Want screen dozing.
890 // Wait for brightness animation to complete beforehand when entering doze
891 // from screen on to prevent a perceptible jump because brightness may operate
892 // differently when the display is configured for dozing.
893 if (mScreenBrightnessRampAnimator.isAnimating()
894 && mPowerState.getScreenState() == Display.STATE_ON) {
895 return;
896 }
897
Jeff Brown3ee549c2014-09-22 20:14:39 -0700898 // Set screen state.
899 if (!setScreenState(Display.STATE_DOZE)) {
900 return; // screen on blocked
901 }
902
903 // Dismiss the black surface without fanfare.
Jeff Brown606e4e82014-09-18 15:22:26 -0700904 mPowerState.setColorFadeLevel(1.0f);
905 mPowerState.dismissColorFade();
906 } else if (target == Display.STATE_DOZE_SUSPEND) {
907 // Want screen dozing and suspended.
908 // Wait for brightness animation to complete beforehand unless already
909 // suspended because we may not be able to change it after suspension.
910 if (mScreenBrightnessRampAnimator.isAnimating()
911 && mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) {
912 return;
913 }
914
Jeff Brown3ee549c2014-09-22 20:14:39 -0700915 // If not already suspending, temporarily set the state to doze until the
916 // screen on is unblocked, then suspend.
917 if (mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) {
918 if (!setScreenState(Display.STATE_DOZE)) {
919 return; // screen on blocked
920 }
921 setScreenState(Display.STATE_DOZE_SUSPEND); // already on so can't block
922 }
923
924 // Dismiss the black surface without fanfare.
Jeff Brown606e4e82014-09-18 15:22:26 -0700925 mPowerState.setColorFadeLevel(1.0f);
926 mPowerState.dismissColorFade();
927 } else {
928 // Want screen off.
Jeff Brown3ee549c2014-09-22 20:14:39 -0700929 mPendingScreenOff = true;
Jeff Brown606e4e82014-09-18 15:22:26 -0700930 if (mPowerState.getColorFadeLevel() == 0.0f) {
931 // Turn the screen off.
932 // A black surface is already hiding the contents of the screen.
933 setScreenState(Display.STATE_OFF);
Jeff Brown3ee549c2014-09-22 20:14:39 -0700934 mPendingScreenOff = false;
Michael Lentined73854d2015-09-25 10:55:26 -0700935 mPowerState.dismissColorFadeResources();
Jeff Brown606e4e82014-09-18 15:22:26 -0700936 } else if (performScreenOffTransition
937 && mPowerState.prepareColorFade(mContext,
938 mColorFadeFadesConfig ?
939 ColorFade.MODE_FADE : ColorFade.MODE_COOL_DOWN)
940 && mPowerState.getScreenState() != Display.STATE_OFF) {
941 // Perform the screen off animation.
942 mColorFadeOffAnimator.start();
943 } else {
944 // Skip the screen off animation and add a black surface to hide the
945 // contents of the screen.
946 mColorFadeOffAnimator.end();
947 }
948 }
949 }
950
Jeff Brown96307042012-07-27 15:51:34 -0700951 private final Runnable mCleanListener = new Runnable() {
952 @Override
953 public void run() {
954 sendUpdatePowerState();
955 }
956 };
957
958 private void setProximitySensorEnabled(boolean enable) {
959 if (enable) {
960 if (!mProximitySensorEnabled) {
Jeff Brownec083212013-09-11 20:45:25 -0700961 // Register the listener.
962 // Proximity sensor state already cleared initially.
Jeff Brown96307042012-07-27 15:51:34 -0700963 mProximitySensorEnabled = true;
Jeff Brown96307042012-07-27 15:51:34 -0700964 mSensorManager.registerListener(mProximitySensorListener, mProximitySensor,
965 SensorManager.SENSOR_DELAY_NORMAL, mHandler);
966 }
967 } else {
968 if (mProximitySensorEnabled) {
Jeff Brownec083212013-09-11 20:45:25 -0700969 // Unregister the listener.
970 // Clear the proximity sensor state for next time.
Jeff Brown96307042012-07-27 15:51:34 -0700971 mProximitySensorEnabled = false;
972 mProximity = PROXIMITY_UNKNOWN;
Jeff Brownec083212013-09-11 20:45:25 -0700973 mPendingProximity = PROXIMITY_UNKNOWN;
Jeff Brown96307042012-07-27 15:51:34 -0700974 mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
975 mSensorManager.unregisterListener(mProximitySensorListener);
Jeff Brownec083212013-09-11 20:45:25 -0700976 clearPendingProximityDebounceTime(); // release wake lock (must be last)
Jeff Brown96307042012-07-27 15:51:34 -0700977 }
978 }
979 }
980
981 private void handleProximitySensorEvent(long time, boolean positive) {
Jeff Brownec083212013-09-11 20:45:25 -0700982 if (mProximitySensorEnabled) {
983 if (mPendingProximity == PROXIMITY_NEGATIVE && !positive) {
984 return; // no change
985 }
986 if (mPendingProximity == PROXIMITY_POSITIVE && positive) {
987 return; // no change
988 }
Jeff Brown96307042012-07-27 15:51:34 -0700989
Jeff Brownec083212013-09-11 20:45:25 -0700990 // Only accept a proximity sensor reading if it remains
991 // stable for the entire debounce delay. We hold a wake lock while
992 // debouncing the sensor.
993 mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
994 if (positive) {
995 mPendingProximity = PROXIMITY_POSITIVE;
996 setPendingProximityDebounceTime(
997 time + PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY); // acquire wake lock
998 } else {
999 mPendingProximity = PROXIMITY_NEGATIVE;
1000 setPendingProximityDebounceTime(
1001 time + PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY); // acquire wake lock
1002 }
1003
1004 // Debounce the new sensor reading.
1005 debounceProximitySensor();
Jeff Brown93cbbb22012-10-04 13:18:36 -07001006 }
Jeff Brown96307042012-07-27 15:51:34 -07001007 }
1008
1009 private void debounceProximitySensor() {
Jeff Brownec083212013-09-11 20:45:25 -07001010 if (mProximitySensorEnabled
1011 && mPendingProximity != PROXIMITY_UNKNOWN
1012 && mPendingProximityDebounceTime >= 0) {
Jeff Brown96307042012-07-27 15:51:34 -07001013 final long now = SystemClock.uptimeMillis();
1014 if (mPendingProximityDebounceTime <= now) {
Jeff Brownec083212013-09-11 20:45:25 -07001015 // Sensor reading accepted. Apply the change then release the wake lock.
Jeff Brown96307042012-07-27 15:51:34 -07001016 mProximity = mPendingProximity;
Jeff Brownec083212013-09-11 20:45:25 -07001017 updatePowerState();
1018 clearPendingProximityDebounceTime(); // release wake lock (must be last)
Jeff Brown96307042012-07-27 15:51:34 -07001019 } else {
Jeff Brownec083212013-09-11 20:45:25 -07001020 // Need to wait a little longer.
1021 // Debounce again later. We continue holding a wake lock while waiting.
Jeff Brown96307042012-07-27 15:51:34 -07001022 Message msg = mHandler.obtainMessage(MSG_PROXIMITY_SENSOR_DEBOUNCED);
1023 msg.setAsynchronous(true);
1024 mHandler.sendMessageAtTime(msg, mPendingProximityDebounceTime);
1025 }
1026 }
1027 }
1028
Jeff Brownec083212013-09-11 20:45:25 -07001029 private void clearPendingProximityDebounceTime() {
1030 if (mPendingProximityDebounceTime >= 0) {
1031 mPendingProximityDebounceTime = -1;
Jeff Brown131206b2014-04-08 17:27:14 -07001032 mCallbacks.releaseSuspendBlocker(); // release wake lock
Jeff Brownec083212013-09-11 20:45:25 -07001033 }
1034 }
1035
1036 private void setPendingProximityDebounceTime(long debounceTime) {
1037 if (mPendingProximityDebounceTime < 0) {
Jeff Brown131206b2014-04-08 17:27:14 -07001038 mCallbacks.acquireSuspendBlocker(); // acquire wake lock
Jeff Brownec083212013-09-11 20:45:25 -07001039 }
1040 mPendingProximityDebounceTime = debounceTime;
1041 }
1042
Jeff Brownec083212013-09-11 20:45:25 -07001043 private void sendOnStateChangedWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001044 mCallbacks.acquireSuspendBlocker();
1045 mHandler.post(mOnStateChangedRunnable);
Jeff Brown96307042012-07-27 15:51:34 -07001046 }
1047
1048 private final Runnable mOnStateChangedRunnable = new Runnable() {
1049 @Override
1050 public void run() {
1051 mCallbacks.onStateChanged();
Jeff Brown131206b2014-04-08 17:27:14 -07001052 mCallbacks.releaseSuspendBlocker();
Jeff Brown96307042012-07-27 15:51:34 -07001053 }
1054 };
1055
Jeff Brownec083212013-09-11 20:45:25 -07001056 private void sendOnProximityPositiveWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001057 mCallbacks.acquireSuspendBlocker();
1058 mHandler.post(mOnProximityPositiveRunnable);
Jeff Brown93cbbb22012-10-04 13:18:36 -07001059 }
1060
1061 private final Runnable mOnProximityPositiveRunnable = new Runnable() {
1062 @Override
1063 public void run() {
1064 mCallbacks.onProximityPositive();
Jeff Brown131206b2014-04-08 17:27:14 -07001065 mCallbacks.releaseSuspendBlocker();
Jeff Brown93cbbb22012-10-04 13:18:36 -07001066 }
1067 };
1068
Jeff Brownec083212013-09-11 20:45:25 -07001069 private void sendOnProximityNegativeWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001070 mCallbacks.acquireSuspendBlocker();
1071 mHandler.post(mOnProximityNegativeRunnable);
Jeff Brown96307042012-07-27 15:51:34 -07001072 }
1073
1074 private final Runnable mOnProximityNegativeRunnable = new Runnable() {
1075 @Override
1076 public void run() {
1077 mCallbacks.onProximityNegative();
Jeff Brown131206b2014-04-08 17:27:14 -07001078 mCallbacks.releaseSuspendBlocker();
Jeff Brown96307042012-07-27 15:51:34 -07001079 }
1080 };
1081
Jeff Brownbd6e1502012-08-28 03:27:37 -07001082 public void dump(final PrintWriter pw) {
Jeff Brown96307042012-07-27 15:51:34 -07001083 synchronized (mLock) {
1084 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001085 pw.println("Display Power Controller Locked State:");
Jeff Brown96307042012-07-27 15:51:34 -07001086 pw.println(" mDisplayReadyLocked=" + mDisplayReadyLocked);
1087 pw.println(" mPendingRequestLocked=" + mPendingRequestLocked);
1088 pw.println(" mPendingRequestChangedLocked=" + mPendingRequestChangedLocked);
1089 pw.println(" mPendingWaitForNegativeProximityLocked="
1090 + mPendingWaitForNegativeProximityLocked);
1091 pw.println(" mPendingUpdatePowerStateLocked=" + mPendingUpdatePowerStateLocked);
1092 }
1093
1094 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001095 pw.println("Display Power Controller Configuration:");
Jeff Brown26875502014-01-30 21:47:47 -08001096 pw.println(" mScreenBrightnessDozeConfig=" + mScreenBrightnessDozeConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001097 pw.println(" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig);
Jeff Brown1bfd0f42014-08-22 01:59:06 -07001098 pw.println(" mScreenBrightnessDarkConfig=" + mScreenBrightnessDarkConfig);
Jeff Brownb76eebff2012-10-05 22:26:44 -07001099 pw.println(" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum);
1100 pw.println(" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum);
Jeff Brown2175e9c2014-09-12 16:11:07 -07001101 pw.println(" mUseSoftwareAutoBrightnessConfig=" + mUseSoftwareAutoBrightnessConfig);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -07001102 pw.println(" mAllowAutoBrightnessWhileDozingConfig=" +
1103 mAllowAutoBrightnessWhileDozingConfig);
Jeff Brown2175e9c2014-09-12 16:11:07 -07001104 pw.println(" mColorFadeFadesConfig=" + mColorFadeFadesConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001105
Jeff Brownbd6e1502012-08-28 03:27:37 -07001106 mHandler.runWithScissors(new Runnable() {
1107 @Override
1108 public void run() {
1109 dumpLocal(pw);
Jeff Brown96307042012-07-27 15:51:34 -07001110 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001111 }, 1000);
Jeff Brown96307042012-07-27 15:51:34 -07001112 }
1113
1114 private void dumpLocal(PrintWriter pw) {
1115 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001116 pw.println("Display Power Controller Thread State:");
Jeff Brown96307042012-07-27 15:51:34 -07001117 pw.println(" mPowerRequest=" + mPowerRequest);
1118 pw.println(" mWaitingForNegativeProximity=" + mWaitingForNegativeProximity);
1119
1120 pw.println(" mProximitySensor=" + mProximitySensor);
1121 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1122 pw.println(" mProximityThreshold=" + mProximityThreshold);
1123 pw.println(" mProximity=" + proximityToString(mProximity));
1124 pw.println(" mPendingProximity=" + proximityToString(mPendingProximity));
1125 pw.println(" mPendingProximityDebounceTime="
1126 + TimeUtils.formatUptime(mPendingProximityDebounceTime));
1127 pw.println(" mScreenOffBecauseOfProximity=" + mScreenOffBecauseOfProximity);
Jeff Brown970d4132014-07-19 11:33:47 -07001128 pw.println(" mAppliedAutoBrightness=" + mAppliedAutoBrightness);
1129 pw.println(" mAppliedDimming=" + mAppliedDimming);
1130 pw.println(" mAppliedLowPower=" + mAppliedLowPower);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001131 pw.println(" mPendingScreenOnUnblocker=" + mPendingScreenOnUnblocker);
1132 pw.println(" mPendingScreenOff=" + mPendingScreenOff);
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001133 pw.println(" mReportedToPolicy=" + reportedToPolicyToString(mReportedScreenStateToPolicy));
Jeff Brown96307042012-07-27 15:51:34 -07001134
Jeff Brown42558692014-05-20 22:02:46 -07001135 pw.println(" mScreenBrightnessRampAnimator.isAnimating()=" +
1136 mScreenBrightnessRampAnimator.isAnimating());
1137
Michael Lentine0839adb2014-07-29 18:47:56 -07001138 if (mColorFadeOnAnimator != null) {
1139 pw.println(" mColorFadeOnAnimator.isStarted()=" +
1140 mColorFadeOnAnimator.isStarted());
Jeff Brown96307042012-07-27 15:51:34 -07001141 }
Michael Lentine0839adb2014-07-29 18:47:56 -07001142 if (mColorFadeOffAnimator != null) {
1143 pw.println(" mColorFadeOffAnimator.isStarted()=" +
1144 mColorFadeOffAnimator.isStarted());
Jeff Brown96307042012-07-27 15:51:34 -07001145 }
1146
1147 if (mPowerState != null) {
1148 mPowerState.dump(pw);
1149 }
Michael Wright639c8be2014-01-17 18:29:12 -08001150
1151 if (mAutomaticBrightnessController != null) {
1152 mAutomaticBrightnessController.dump(pw);
1153 }
1154
Jeff Brown96307042012-07-27 15:51:34 -07001155 }
1156
1157 private static String proximityToString(int state) {
1158 switch (state) {
1159 case PROXIMITY_UNKNOWN:
1160 return "Unknown";
1161 case PROXIMITY_NEGATIVE:
1162 return "Negative";
1163 case PROXIMITY_POSITIVE:
1164 return "Positive";
1165 default:
1166 return Integer.toString(state);
1167 }
1168 }
1169
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001170 private static String reportedToPolicyToString(int state) {
1171 switch (state) {
1172 case REPORTED_TO_POLICY_SCREEN_OFF:
1173 return "REPORTED_TO_POLICY_SCREEN_OFF";
1174 case REPORTED_TO_POLICY_SCREEN_TURNING_ON:
1175 return "REPORTED_TO_POLICY_SCREEN_TURNING_ON";
1176 case REPORTED_TO_POLICY_SCREEN_ON:
1177 return "REPORTED_TO_POLICY_SCREEN_ON";
1178 default:
1179 return Integer.toString(state);
1180 }
1181 }
1182
Michael Wright639c8be2014-01-17 18:29:12 -08001183 private static Spline createAutoBrightnessSpline(int[] lux, int[] brightness) {
1184 try {
1185 final int n = brightness.length;
1186 float[] x = new float[n];
1187 float[] y = new float[n];
1188 y[0] = normalizeAbsoluteBrightness(brightness[0]);
1189 for (int i = 1; i < n; i++) {
1190 x[i] = lux[i - 1];
1191 y[i] = normalizeAbsoluteBrightness(brightness[i]);
1192 }
1193
Michael Wright3e9a1342014-08-28 17:42:16 -07001194 Spline spline = Spline.createSpline(x, y);
Michael Wright639c8be2014-01-17 18:29:12 -08001195 if (DEBUG) {
1196 Slog.d(TAG, "Auto-brightness spline: " + spline);
1197 for (float v = 1f; v < lux[lux.length - 1] * 1.25f; v *= 1.25f) {
1198 Slog.d(TAG, String.format(" %7.1f: %7.1f", v, spline.interpolate(v)));
1199 }
1200 }
1201 return spline;
1202 } catch (IllegalArgumentException ex) {
1203 Slog.e(TAG, "Could not create auto-brightness spline.", ex);
1204 return null;
Jeff Brown96307042012-07-27 15:51:34 -07001205 }
Michael Wright639c8be2014-01-17 18:29:12 -08001206 }
1207
1208 private static float normalizeAbsoluteBrightness(int value) {
1209 return (float)clampAbsoluteBrightness(value) / PowerManager.BRIGHTNESS_ON;
1210 }
1211
1212 private static int clampAbsoluteBrightness(int value) {
1213 return MathUtils.constrain(value, PowerManager.BRIGHTNESS_OFF, PowerManager.BRIGHTNESS_ON);
Jeff Brown96307042012-07-27 15:51:34 -07001214 }
1215
Jeff Brown96307042012-07-27 15:51:34 -07001216 private final class DisplayControllerHandler extends Handler {
1217 public DisplayControllerHandler(Looper looper) {
Jeff Browna2910d02012-08-25 12:29:46 -07001218 super(looper, null, true /*async*/);
Jeff Brown96307042012-07-27 15:51:34 -07001219 }
1220
1221 @Override
1222 public void handleMessage(Message msg) {
1223 switch (msg.what) {
1224 case MSG_UPDATE_POWER_STATE:
1225 updatePowerState();
1226 break;
1227
1228 case MSG_PROXIMITY_SENSOR_DEBOUNCED:
1229 debounceProximitySensor();
1230 break;
Jeff Brown3ee549c2014-09-22 20:14:39 -07001231
1232 case MSG_SCREEN_ON_UNBLOCKED:
1233 if (mPendingScreenOnUnblocker == msg.obj) {
1234 unblockScreenOn();
1235 updatePowerState();
1236 }
1237 break;
Jeff Brown96307042012-07-27 15:51:34 -07001238 }
1239 }
1240 }
1241
1242 private final SensorEventListener mProximitySensorListener = new SensorEventListener() {
1243 @Override
1244 public void onSensorChanged(SensorEvent event) {
1245 if (mProximitySensorEnabled) {
1246 final long time = SystemClock.uptimeMillis();
1247 final float distance = event.values[0];
1248 boolean positive = distance >= 0.0f && distance < mProximityThreshold;
1249 handleProximitySensorEvent(time, positive);
1250 }
1251 }
1252
1253 @Override
1254 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1255 // Not used.
1256 }
1257 };
Jeff Brown3ee549c2014-09-22 20:14:39 -07001258
1259 private final class ScreenOnUnblocker implements WindowManagerPolicy.ScreenOnListener {
1260 @Override
1261 public void onScreenOn() {
1262 Message msg = mHandler.obtainMessage(MSG_SCREEN_ON_UNBLOCKED, this);
1263 msg.setAsynchronous(true);
1264 mHandler.sendMessage(msg);
1265 }
1266 }
Jeff Brown96307042012-07-27 15:51:34 -07001267}