blob: 5f4c8efd4d9e2b61de10b4041c6d3c4a9eca958a [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
Narayan Kamath38819982017-07-06 18:15:30 +010019import android.app.ActivityManager;
Jeff Brown131206b2014-04-08 17:27:14 -070020import com.android.internal.app.IBatteryStats;
21import com.android.server.LocalServices;
22import com.android.server.am.BatteryStatsService;
Adrian Roose99bc052017-11-20 17:55:31 +010023import com.android.server.policy.WindowManagerPolicy;
Jeff Brown96307042012-07-27 15:51:34 -070024
25import android.animation.Animator;
26import android.animation.ObjectAnimator;
Michael Wright144aac92017-12-21 18:37:41 +000027import android.annotation.Nullable;
28import android.annotation.UserIdInt;
Jeff Brown96307042012-07-27 15:51:34 -070029import android.content.Context;
Michael Wright144aac92017-12-21 18:37:41 +000030import android.content.pm.ParceledListSlice;
Jeff Brown96307042012-07-27 15:51:34 -070031import android.content.res.Resources;
Michael Wrightd8460232018-01-16 18:04:59 +000032import android.database.ContentObserver;
Jeff Brown96307042012-07-27 15:51:34 -070033import android.hardware.Sensor;
34import android.hardware.SensorEvent;
35import android.hardware.SensorEventListener;
36import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000037import android.hardware.display.AmbientBrightnessDayStats;
Michael Wright144aac92017-12-21 18:37:41 +000038import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000039import android.hardware.display.BrightnessConfiguration;
Jeff Brown131206b2014-04-08 17:27:14 -070040import android.hardware.display.DisplayManagerInternal.DisplayPowerCallbacks;
41import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest;
Michael Wrightd8460232018-01-16 18:04:59 +000042import android.net.Uri;
Jeff Brown96307042012-07-27 15:51:34 -070043import android.os.Handler;
44import android.os.Looper;
45import android.os.Message;
Jeff Brown330560f2012-08-21 22:10:57 -070046import android.os.PowerManager;
Jeff Brown131206b2014-04-08 17:27:14 -070047import android.os.RemoteException;
Jeff Brown96307042012-07-27 15:51:34 -070048import android.os.SystemClock;
Jeff Brown3edf5272014-08-14 19:25:14 -070049import android.os.Trace;
Michael Wrightd8460232018-01-16 18:04:59 +000050import android.os.UserHandle;
51import android.provider.Settings;
Michael Wright41a5cdf2013-11-13 16:18:32 -080052import android.util.MathUtils;
Jeff Brown96307042012-07-27 15:51:34 -070053import android.util.Slog;
54import android.util.TimeUtils;
Jeff Brown037c33e2014-04-09 00:31:55 -070055import android.view.Display;
Jeff Brown96307042012-07-27 15:51:34 -070056
57import java.io.PrintWriter;
Jeff Brown96307042012-07-27 15:51:34 -070058
59/**
60 * Controls the power state of the display.
61 *
62 * Handles the proximity sensor, light sensor, and animations between states
63 * including the screen off animation.
64 *
65 * This component acts independently of the rest of the power manager service.
66 * In particular, it does not share any state and it only communicates
67 * via asynchronous callbacks to inform the power manager that something has
68 * changed.
69 *
70 * Everything this class does internally is serialized on its handler although
71 * it may be accessed by other threads from the outside.
72 *
73 * Note that the power manager service guarantees that it will hold a suspend
74 * blocker as long as the display is not ready. So most of the work done here
75 * does not need to worry about holding a suspend blocker unless it happens
76 * independently of the display ready signal.
Michael Lentine0839adb2014-07-29 18:47:56 -070077 *
Jeff Brown3ee549c2014-09-22 20:14:39 -070078 * For debugging, you can make the color fade and brightness animations run
Jeff Brown96307042012-07-27 15:51:34 -070079 * slower by changing the "animator duration scale" option in Development Settings.
80 */
Michael Wright639c8be2014-01-17 18:29:12 -080081final class DisplayPowerController implements AutomaticBrightnessController.Callbacks {
Jeff Brown96307042012-07-27 15:51:34 -070082 private static final String TAG = "DisplayPowerController";
Jeff Browna576b4d2015-04-23 19:58:06 -070083 private static final String SCREEN_ON_BLOCKED_TRACE_NAME = "Screen on blocked";
Jorim Jaggi51304d72017-05-17 17:25:32 +020084 private static final String SCREEN_OFF_BLOCKED_TRACE_NAME = "Screen off blocked";
Jeff Brown96307042012-07-27 15:51:34 -070085
Julius D'souza428aed02016-08-07 19:08:30 -070086 private static final boolean DEBUG = false;
Jeff Brown96307042012-07-27 15:51:34 -070087 private static final boolean DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT = false;
Jeff Brown96307042012-07-27 15:51:34 -070088
Jeff Brown3ee549c2014-09-22 20:14:39 -070089 // If true, uses the color fade on animation.
Jeff Brown13c589b2012-08-16 16:20:54 -070090 // We might want to turn this off if we cannot get a guarantee that the screen
91 // actually turns on and starts showing new content after the call to set the
Jeff Brown5356c7dc2012-08-20 20:17:36 -070092 // screen state returns. Playing the animation can also be somewhat slow.
Michael Lentine0839adb2014-07-29 18:47:56 -070093 private static final boolean USE_COLOR_FADE_ON_ANIMATION = false;
Jeff Brown13c589b2012-08-16 16:20:54 -070094
Jeff Brownb76eebff2012-10-05 22:26:44 -070095 // The minimum reduction in brightness when dimmed.
96 private static final int SCREEN_DIM_MINIMUM_REDUCTION = 10;
97
Michael Lentine0839adb2014-07-29 18:47:56 -070098 private static final int COLOR_FADE_ON_ANIMATION_DURATION_MILLIS = 250;
Michael Lentine0c9a62d2014-08-20 09:19:07 -070099 private static final int COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS = 400;
Jeff Brown96307042012-07-27 15:51:34 -0700100
101 private static final int MSG_UPDATE_POWER_STATE = 1;
102 private static final int MSG_PROXIMITY_SENSOR_DEBOUNCED = 2;
Jeff Brown3ee549c2014-09-22 20:14:39 -0700103 private static final int MSG_SCREEN_ON_UNBLOCKED = 3;
Jorim Jaggi51304d72017-05-17 17:25:32 +0200104 private static final int MSG_SCREEN_OFF_UNBLOCKED = 4;
Michael Wrighteef0e132017-11-21 17:57:52 +0000105 private static final int MSG_CONFIGURE_BRIGHTNESS = 5;
Michael Wrightd8460232018-01-16 18:04:59 +0000106 private static final int MSG_SET_TEMPORARY_BRIGHTNESS = 6;
107 private static final int MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT = 7;
Jeff Brown96307042012-07-27 15:51:34 -0700108
109 private static final int PROXIMITY_UNKNOWN = -1;
110 private static final int PROXIMITY_NEGATIVE = 0;
111 private static final int PROXIMITY_POSITIVE = 1;
112
Jeff Brown93cbbb22012-10-04 13:18:36 -0700113 // Proximity sensor debounce delay in milliseconds for positive or negative transitions.
114 private static final int PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY = 0;
Jeff Brownec083212013-09-11 20:45:25 -0700115 private static final int PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY = 250;
Jeff Brown96307042012-07-27 15:51:34 -0700116
117 // Trigger proximity if distance is less than 5 cm.
118 private static final float TYPICAL_PROXIMITY_THRESHOLD = 5.0f;
119
Julius D'souzad5105dd2017-06-02 11:03:53 -0700120 // State machine constants for tracking initial brightness ramp skipping when enabled.
121 private static final int RAMP_STATE_SKIP_NONE = 0;
122 private static final int RAMP_STATE_SKIP_INITIAL = 1;
123 private static final int RAMP_STATE_SKIP_AUTOBRIGHT = 2;
124
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700125 private static final int REPORTED_TO_POLICY_SCREEN_OFF = 0;
126 private static final int REPORTED_TO_POLICY_SCREEN_TURNING_ON = 1;
127 private static final int REPORTED_TO_POLICY_SCREEN_ON = 2;
Jorim Jaggi51304d72017-05-17 17:25:32 +0200128 private static final int REPORTED_TO_POLICY_SCREEN_TURNING_OFF = 3;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700129
Jeff Brown96307042012-07-27 15:51:34 -0700130 private final Object mLock = new Object();
131
Michael Lentine0839adb2014-07-29 18:47:56 -0700132 private final Context mContext;
133
Jeff Brown96307042012-07-27 15:51:34 -0700134 // Our handler.
135 private final DisplayControllerHandler mHandler;
136
137 // Asynchronous callbacks into the power manager service.
138 // Only invoked from the handler thread while no locks are held.
Jeff Brown131206b2014-04-08 17:27:14 -0700139 private final DisplayPowerCallbacks mCallbacks;
140
141 // Battery stats.
142 private final IBatteryStats mBatteryStats;
Jeff Brown96307042012-07-27 15:51:34 -0700143
Jeff Brown96307042012-07-27 15:51:34 -0700144 // The sensor manager.
145 private final SensorManager mSensorManager;
146
Jeff Brown3ee549c2014-09-22 20:14:39 -0700147 // The window manager policy.
148 private final WindowManagerPolicy mWindowManagerPolicy;
149
Jeff Brown037c33e2014-04-09 00:31:55 -0700150 // The display blanker.
151 private final DisplayBlanker mBlanker;
152
Michael Wrightd8460232018-01-16 18:04:59 +0000153 // Tracker for brightness changes.
154 private final BrightnessTracker mBrightnessTracker;
155
156 // Tracker for brightness settings changes.
157 private final SettingsObserver mSettingsObserver;
158
Jeff Brown96307042012-07-27 15:51:34 -0700159 // The proximity sensor, or null if not available or needed.
160 private Sensor mProximitySensor;
161
Jeff Brown26875502014-01-30 21:47:47 -0800162 // The doze screen brightness.
163 private final int mScreenBrightnessDozeConfig;
164
Jeff Brown96307042012-07-27 15:51:34 -0700165 // The dim screen brightness.
166 private final int mScreenBrightnessDimConfig;
167
Jeff Brownb76eebff2012-10-05 22:26:44 -0700168 // The minimum allowed brightness.
169 private final int mScreenBrightnessRangeMinimum;
170
171 // The maximum allowed brightness.
172 private final int mScreenBrightnessRangeMaximum;
173
Michael Wrightd8460232018-01-16 18:04:59 +0000174 // The default screen brightness.
175 private final int mScreenBrightnessDefault;
176
177 // The default screen brightness for VR.
178 private final int mScreenBrightnessForVrDefault;
179
Jeff Brown330560f2012-08-21 22:10:57 -0700180 // True if auto-brightness should be used.
Jeff Brown96307042012-07-27 15:51:34 -0700181 private boolean mUseSoftwareAutoBrightnessConfig;
Jeff Brown330560f2012-08-21 22:10:57 -0700182
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700183 // True if should use light sensor to automatically determine doze screen brightness.
184 private final boolean mAllowAutoBrightnessWhileDozingConfig;
185
Narayan Kamath38819982017-07-06 18:15:30 +0100186 // Whether or not the color fade on screen on / off is enabled.
187 private final boolean mColorFadeEnabled;
188
Jeff Brown252c2062012-10-08 16:21:01 -0700189 // True if we should fade the screen while turning it off, false if we should play
Jeff Brown3ee549c2014-09-22 20:14:39 -0700190 // a stylish color fade animation instead.
Michael Lentine0839adb2014-07-29 18:47:56 -0700191 private boolean mColorFadeFadesConfig;
Jeff Browna52772f2012-10-04 18:38:09 -0700192
Michael Wrightc3e6af82017-07-25 22:31:03 +0100193 // True if we need to fake a transition to off when coming out of a doze state.
194 // Some display hardware will blank itself when coming out of doze in order to hide
195 // artifacts. For these displays we fake a transition into OFF so that policy can appropriately
196 // blank itself and begin an appropriate power on animation.
197 private boolean mDisplayBlanksAfterDozeConfig;
Michael Wright05e76fe2017-07-20 18:18:33 +0100198
Michael Wright63a40062017-07-26 01:10:59 +0100199 // True if there are only buckets of brightness values when the display is in the doze state,
200 // rather than a full range of values. If this is true, then we'll avoid animating the screen
201 // brightness since it'd likely be multiple jarring brightness transitions instead of just one
202 // to reach the final state.
203 private boolean mBrightnessBucketsInDozeConfig;
204
Jeff Brown96307042012-07-27 15:51:34 -0700205 // The pending power request.
206 // Initially null until the first call to requestPowerState.
207 // Guarded by mLock.
208 private DisplayPowerRequest mPendingRequestLocked;
209
210 // True if a request has been made to wait for the proximity sensor to go negative.
211 // Guarded by mLock.
212 private boolean mPendingWaitForNegativeProximityLocked;
213
214 // True if the pending power request or wait for negative proximity flag
215 // has been changed since the last update occurred.
216 // Guarded by mLock.
217 private boolean mPendingRequestChangedLocked;
218
219 // Set to true when the important parts of the pending power request have been applied.
220 // The important parts are mainly the screen state. Brightness changes may occur
221 // concurrently.
222 // Guarded by mLock.
223 private boolean mDisplayReadyLocked;
224
225 // Set to true if a power state update is required.
226 // Guarded by mLock.
227 private boolean mPendingUpdatePowerStateLocked;
228
229 /* The following state must only be accessed by the handler thread. */
230
231 // The currently requested power state.
232 // The power controller will progressively update its internal state to match
233 // the requested power state. Initially null until the first update.
234 private DisplayPowerRequest mPowerRequest;
235
236 // The current power state.
237 // Must only be accessed on the handler thread.
238 private DisplayPowerState mPowerState;
239
240 // True if the device should wait for negative proximity sensor before
241 // waking up the screen. This is set to false as soon as a negative
242 // proximity sensor measurement is observed or when the device is forced to
243 // go to sleep by the user. While true, the screen remains off.
244 private boolean mWaitingForNegativeProximity;
245
246 // The actual proximity sensor threshold value.
247 private float mProximityThreshold;
248
249 // Set to true if the proximity sensor listener has been registered
250 // with the sensor manager.
251 private boolean mProximitySensorEnabled;
252
253 // The debounced proximity sensor state.
254 private int mProximity = PROXIMITY_UNKNOWN;
255
256 // The raw non-debounced proximity sensor state.
257 private int mPendingProximity = PROXIMITY_UNKNOWN;
Jeff Brownec083212013-09-11 20:45:25 -0700258 private long mPendingProximityDebounceTime = -1; // -1 if fully debounced
Jeff Brown96307042012-07-27 15:51:34 -0700259
260 // True if the screen was turned off because of the proximity sensor.
261 // When the screen turns on again, we report user activity to the power manager.
262 private boolean mScreenOffBecauseOfProximity;
263
Jeff Brown3ee549c2014-09-22 20:14:39 -0700264 // The currently active screen on unblocker. This field is non-null whenever
265 // we are waiting for a callback to release it and unblock the screen.
266 private ScreenOnUnblocker mPendingScreenOnUnblocker;
Jorim Jaggi51304d72017-05-17 17:25:32 +0200267 private ScreenOffUnblocker mPendingScreenOffUnblocker;
Jeff Brown3ee549c2014-09-22 20:14:39 -0700268
269 // True if we were in the process of turning off the screen.
270 // This allows us to recover more gracefully from situations where we abort
271 // turning off the screen.
272 private boolean mPendingScreenOff;
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700273
Jeff Brown0a434772014-09-30 14:42:27 -0700274 // True if we have unfinished business and are holding a suspend blocker.
275 private boolean mUnfinishedBusiness;
276
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700277 // The elapsed real time when the screen on was blocked.
278 private long mScreenOnBlockStartRealTime;
Jorim Jaggi51304d72017-05-17 17:25:32 +0200279 private long mScreenOffBlockStartRealTime;
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700280
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700281 // Screen state we reported to policy. Must be one of REPORTED_TO_POLICY_SCREEN_* fields.
282 private int mReportedScreenStateToPolicy;
Jeff Browne1633ad2015-06-22 19:24:24 -0700283
Julius D'souzad5105dd2017-06-02 11:03:53 -0700284 // If the last recorded screen state was dozing or not.
285 private boolean mDozing;
286
Jeff Brown970d4132014-07-19 11:33:47 -0700287 // Remembers whether certain kinds of brightness adjustments
288 // were recently applied so that we can decide how to transition.
289 private boolean mAppliedAutoBrightness;
290 private boolean mAppliedDimming;
291 private boolean mAppliedLowPower;
Jeff Brown96307042012-07-27 15:51:34 -0700292
Julius D'souzafeadad12016-08-05 14:34:38 -0700293 // Brightness animation ramp rates in brightness units per second
Prashant Malani99e6d432016-03-29 16:32:37 -0700294 private final int mBrightnessRampRateFast;
Julius D'souzafeadad12016-08-05 14:34:38 -0700295 private final int mBrightnessRampRateSlow;
Prashant Malani99e6d432016-03-29 16:32:37 -0700296
Julius D'souzad5105dd2017-06-02 11:03:53 -0700297 // Whether or not to skip the initial brightness ramps into STATE_ON.
298 private final boolean mSkipScreenOnBrightnessRamp;
299
300 // A record of state for skipping brightness ramps.
301 private int mSkipRampState = RAMP_STATE_SKIP_NONE;
302
303 // The first autobrightness value set when entering RAMP_STATE_SKIP_INITIAL.
304 private int mInitialAutoBrightness;
305
Michael Wright639c8be2014-01-17 18:29:12 -0800306 // The controller for the automatic brightness level.
307 private AutomaticBrightnessController mAutomaticBrightnessController;
308
Michael Wright144aac92017-12-21 18:37:41 +0000309 // The mapper between ambient lux, display backlight values, and display brightness.
310 @Nullable
311 private BrightnessMappingStrategy mBrightnessMapper;
312
Michael Wrighteef0e132017-11-21 17:57:52 +0000313 // The current brightness configuration.
Michael Wrightd5df3612018-01-02 12:44:52 +0000314 @Nullable
Michael Wrighteef0e132017-11-21 17:57:52 +0000315 private BrightnessConfiguration mBrightnessConfiguration;
316
Michael Wrightd5df3612018-01-02 12:44:52 +0000317 // The last brightness that was set by the user and not temporary. Set to -1 when a brightness
318 // has yet to be recorded.
Michael Wrightd8460232018-01-16 18:04:59 +0000319 private int mLastUserSetScreenBrightness;
320
321 // The screen brightenss setting has changed but not taken effect yet. If this is different
322 // from the current screen brightness setting then this is coming from something other than us
323 // and should be considered a user interaction.
324 private int mPendingScreenBrightnessSetting;
325
326 // The last observed screen brightness setting, either set by us or by the settings app on
327 // behalf of the user.
328 private int mCurrentScreenBrightnessSetting;
329
330 // The temporary screen brightness. Typically set when a user is interacting with the
331 // brightness slider but hasn't settled on a choice yet. Set to -1 when there's no temporary
332 // brightness set.
333 private int mTemporaryScreenBrightness;
334
335 // The current screen brightness while in VR mode.
336 private int mScreenBrightnessForVr;
Michael Wrightd5df3612018-01-02 12:44:52 +0000337
338 // The last auto brightness adjustment that was set by the user and not temporary. Set to
339 // Float.NaN when an auto-brightness adjustment hasn't been recorded yet.
Michael Wrightd8460232018-01-16 18:04:59 +0000340 private float mAutoBrightnessAdjustment;
341
342 // The pending auto brightness adjustment that will take effect on the next power state update.
343 private float mPendingAutoBrightnessAdjustment;
344
345 // The temporary auto brightness adjustment. Typically set when a user is interacting with the
346 // adjustment slider but hasn't settled on a choice yet. Set to Float.NaN when there's no
347 // temporary adjustment set.
348 private float mTemporaryAutoBrightnessAdjustment;
Michael Wrightd5df3612018-01-02 12:44:52 +0000349
Jeff Brown96307042012-07-27 15:51:34 -0700350 // Animators.
Michael Lentine0839adb2014-07-29 18:47:56 -0700351 private ObjectAnimator mColorFadeOnAnimator;
352 private ObjectAnimator mColorFadeOffAnimator;
Jeff Brown96307042012-07-27 15:51:34 -0700353 private RampAnimator<DisplayPowerState> mScreenBrightnessRampAnimator;
354
355 /**
356 * Creates the display power controller.
357 */
Jeff Brown131206b2014-04-08 17:27:14 -0700358 public DisplayPowerController(Context context,
Jeff Brown037c33e2014-04-09 00:31:55 -0700359 DisplayPowerCallbacks callbacks, Handler handler,
360 SensorManager sensorManager, DisplayBlanker blanker) {
Jeff Brown131206b2014-04-08 17:27:14 -0700361 mHandler = new DisplayControllerHandler(handler.getLooper());
Michael Wright144aac92017-12-21 18:37:41 +0000362 mBrightnessTracker = new BrightnessTracker(context, null);
Michael Wrightd8460232018-01-16 18:04:59 +0000363 mSettingsObserver = new SettingsObserver(mHandler);
Jeff Brown96307042012-07-27 15:51:34 -0700364 mCallbacks = callbacks;
Jeff Brown96307042012-07-27 15:51:34 -0700365
Jeff Brown131206b2014-04-08 17:27:14 -0700366 mBatteryStats = BatteryStatsService.getService();
Jeff Brown3b971592013-01-09 18:46:37 -0800367 mSensorManager = sensorManager;
Jeff Brown3ee549c2014-09-22 20:14:39 -0700368 mWindowManagerPolicy = LocalServices.getService(WindowManagerPolicy.class);
Jeff Brown037c33e2014-04-09 00:31:55 -0700369 mBlanker = blanker;
Michael Lentine0839adb2014-07-29 18:47:56 -0700370 mContext = context;
Jeff Brown96307042012-07-27 15:51:34 -0700371
372 final Resources resources = context.getResources();
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700373 final int screenBrightnessSettingMinimum = clampAbsoluteBrightness(resources.getInteger(
374 com.android.internal.R.integer.config_screenBrightnessSettingMinimum));
Jeff Brownb76eebff2012-10-05 22:26:44 -0700375
Jeff Brown26875502014-01-30 21:47:47 -0800376 mScreenBrightnessDozeConfig = clampAbsoluteBrightness(resources.getInteger(
377 com.android.internal.R.integer.config_screenBrightnessDoze));
378
Jeff Brownb76eebff2012-10-05 22:26:44 -0700379 mScreenBrightnessDimConfig = clampAbsoluteBrightness(resources.getInteger(
380 com.android.internal.R.integer.config_screenBrightnessDim));
381
Michael Wright144aac92017-12-21 18:37:41 +0000382 mScreenBrightnessRangeMinimum =
383 Math.min(screenBrightnessSettingMinimum, mScreenBrightnessDimConfig);
Michael Wright639c8be2014-01-17 18:29:12 -0800384
Michael Wrighteef0e132017-11-21 17:57:52 +0000385 mScreenBrightnessRangeMaximum = clampAbsoluteBrightness(resources.getInteger(
386 com.android.internal.R.integer.config_screenBrightnessSettingMaximum));
Michael Wrightd8460232018-01-16 18:04:59 +0000387 mScreenBrightnessDefault = clampAbsoluteBrightness(resources.getInteger(
388 com.android.internal.R.integer.config_screenBrightnessSettingDefault));
389 mScreenBrightnessForVrDefault = clampAbsoluteBrightness(resources.getInteger(
390 com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault));
Jeff Brownb76eebff2012-10-05 22:26:44 -0700391
Jeff Brown96307042012-07-27 15:51:34 -0700392 mUseSoftwareAutoBrightnessConfig = resources.getBoolean(
393 com.android.internal.R.bool.config_automatic_brightness_available);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700394
395 mAllowAutoBrightnessWhileDozingConfig = resources.getBoolean(
396 com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing);
397
Prashant Malani99e6d432016-03-29 16:32:37 -0700398 mBrightnessRampRateFast = resources.getInteger(
399 com.android.internal.R.integer.config_brightness_ramp_rate_fast);
Julius D'souzafeadad12016-08-05 14:34:38 -0700400 mBrightnessRampRateSlow = resources.getInteger(
401 com.android.internal.R.integer.config_brightness_ramp_rate_slow);
Julius D'souzad5105dd2017-06-02 11:03:53 -0700402 mSkipScreenOnBrightnessRamp = resources.getBoolean(
403 com.android.internal.R.bool.config_skipScreenOnBrightnessRamp);
Prashant Malani99e6d432016-03-29 16:32:37 -0700404
Jeff Brown96307042012-07-27 15:51:34 -0700405 if (mUseSoftwareAutoBrightnessConfig) {
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700406 final float dozeScaleFactor = resources.getFraction(
407 com.android.internal.R.fraction.config_screenAutoBrightnessDozeScaleFactor,
408 1, 1);
Jeff Brown1a30b552012-08-16 01:31:11 -0700409
Michael Wrighteef0e132017-11-21 17:57:52 +0000410 int[] brightLevels = resources.getIntArray(
411 com.android.internal.R.array.config_dynamicHysteresisBrightLevels);
412 int[] darkLevels = resources.getIntArray(
413 com.android.internal.R.array.config_dynamicHysteresisDarkLevels);
414 int[] luxHysteresisLevels = resources.getIntArray(
415 com.android.internal.R.array.config_dynamicHysteresisLuxLevels);
416 HysteresisLevels dynamicHysteresis = new HysteresisLevels(
417 brightLevels, darkLevels, luxHysteresisLevels);
418
419 long brighteningLightDebounce = resources.getInteger(
420 com.android.internal.R.integer.config_autoBrightnessBrighteningLightDebounce);
421 long darkeningLightDebounce = resources.getInteger(
422 com.android.internal.R.integer.config_autoBrightnessDarkeningLightDebounce);
423 boolean autoBrightnessResetAmbientLuxAfterWarmUp = resources.getBoolean(
424 com.android.internal.R.bool.config_autoBrightnessResetAmbientLuxAfterWarmUp);
425 int ambientLightHorizon = resources.getInteger(
426 com.android.internal.R.integer.config_autoBrightnessAmbientLightHorizon);
Michael Wrighteef0e132017-11-21 17:57:52 +0000427
428 int lightSensorWarmUpTimeConfig = resources.getInteger(
429 com.android.internal.R.integer.config_lightSensorWarmupTime);
430 int lightSensorRate = resources.getInteger(
431 com.android.internal.R.integer.config_autoBrightnessLightSensorRate);
432 int initialLightSensorRate = resources.getInteger(
433 com.android.internal.R.integer.config_autoBrightnessInitialLightSensorRate);
434 if (initialLightSensorRate == -1) {
435 initialLightSensorRate = lightSensorRate;
436 } else if (initialLightSensorRate > lightSensorRate) {
437 Slog.w(TAG, "Expected config_autoBrightnessInitialLightSensorRate ("
438 + initialLightSensorRate + ") to be less than or equal to "
439 + "config_autoBrightnessLightSensorRate (" + lightSensorRate + ").");
440 }
441
Michael Wright144aac92017-12-21 18:37:41 +0000442 mBrightnessMapper = BrightnessMappingStrategy.create(resources);
443 if (mBrightnessMapper != null) {
Jeff Brown131206b2014-04-08 17:27:14 -0700444 mAutomaticBrightnessController = new AutomaticBrightnessController(this,
Michael Wright144aac92017-12-21 18:37:41 +0000445 handler.getLooper(), sensorManager, mBrightnessMapper,
446 lightSensorWarmUpTimeConfig, mScreenBrightnessRangeMinimum,
447 mScreenBrightnessRangeMaximum, dozeScaleFactor, lightSensorRate,
448 initialLightSensorRate, brighteningLightDebounce, darkeningLightDebounce,
Julius D'souza428aed02016-08-07 19:08:30 -0700449 autoBrightnessResetAmbientLuxAfterWarmUp, ambientLightHorizon,
Dan Gittik57d6f112018-03-27 18:14:22 +0100450 dynamicHysteresis);
Michael Wrighteef0e132017-11-21 17:57:52 +0000451 } else {
452 mUseSoftwareAutoBrightnessConfig = false;
Jeff Brown96307042012-07-27 15:51:34 -0700453 }
Jeff Brown96307042012-07-27 15:51:34 -0700454 }
455
Narayan Kamath38819982017-07-06 18:15:30 +0100456 mColorFadeEnabled = !ActivityManager.isLowRamDeviceStatic();
Michael Lentine0839adb2014-07-29 18:47:56 -0700457 mColorFadeFadesConfig = resources.getBoolean(
Jeff Browna52772f2012-10-04 18:38:09 -0700458 com.android.internal.R.bool.config_animateScreenLights);
459
Michael Wrightc3e6af82017-07-25 22:31:03 +0100460 mDisplayBlanksAfterDozeConfig = resources.getBoolean(
461 com.android.internal.R.bool.config_displayBlanksAfterDoze);
Michael Wright05e76fe2017-07-20 18:18:33 +0100462
Michael Wright63a40062017-07-26 01:10:59 +0100463 mBrightnessBucketsInDozeConfig = resources.getBoolean(
464 com.android.internal.R.bool.config_displayBrightnessBucketsInDoze);
465
Jeff Brown96307042012-07-27 15:51:34 -0700466 if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) {
467 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
468 if (mProximitySensor != null) {
469 mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(),
470 TYPICAL_PROXIMITY_THRESHOLD);
471 }
472 }
473
Michael Wrightd8460232018-01-16 18:04:59 +0000474 mCurrentScreenBrightnessSetting = getScreenBrightnessSetting();
475 mScreenBrightnessForVr = getScreenBrightnessForVrSetting();
476 mAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting();
477 mTemporaryScreenBrightness = -1;
Kenny Guyad9a6ea2018-01-31 15:57:15 +0000478 mPendingScreenBrightnessSetting = -1;
Michael Wrightd8460232018-01-16 18:04:59 +0000479 mTemporaryAutoBrightnessAdjustment = Float.NaN;
Michael Wrighteef0e132017-11-21 17:57:52 +0000480 }
481
Jeff Brown96307042012-07-27 15:51:34 -0700482 /**
483 * Returns true if the proximity sensor screen-off function is available.
484 */
485 public boolean isProximitySensorAvailable() {
486 return mProximitySensor != null;
487 }
488
489 /**
Michael Wright144aac92017-12-21 18:37:41 +0000490 * Get the {@link BrightnessChangeEvent}s for the specified user.
491 * @param userId userId to fetch data for
492 * @param includePackage if false will null out the package name in events
493 */
494 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(
495 @UserIdInt int userId, boolean includePackage) {
496 return mBrightnessTracker.getEvents(userId, includePackage);
497 }
498
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000499 public void onSwitchUser(@UserIdInt int newUserId) {
Kenny Guyecc978f2018-03-14 17:30:20 +0000500 handleSettingsChange(true /* userSwitch */);
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000501 mBrightnessTracker.onSwitchUser(newUserId);
502 }
503
504 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats(
505 @UserIdInt int userId) {
506 return mBrightnessTracker.getAmbientBrightnessStats(userId);
507 }
508
Michael Wright144aac92017-12-21 18:37:41 +0000509 /**
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000510 * Persist the brightness slider events and ambient brightness stats to disk.
Michael Wright144aac92017-12-21 18:37:41 +0000511 */
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000512 public void persistBrightnessTrackerState() {
513 mBrightnessTracker.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +0000514 }
515
516 /**
Jeff Brown96307042012-07-27 15:51:34 -0700517 * Requests a new power state.
518 * The controller makes a copy of the provided object and then
519 * begins adjusting the power state to match what was requested.
520 *
521 * @param request The requested power state.
522 * @param waitForNegativeProximity If true, issues a request to wait for
523 * negative proximity before turning the screen back on, assuming the screen
524 * was turned off by the proximity sensor.
525 * @return True if display is ready, false if there are important changes that must
526 * be made asynchronously (such as turning the screen on), in which case the caller
Jeff Brown606e4e82014-09-18 15:22:26 -0700527 * should grab a wake lock, watch for {@link DisplayPowerCallbacks#onStateChanged()}
528 * then try the request again later until the state converges.
Jeff Brown96307042012-07-27 15:51:34 -0700529 */
530 public boolean requestPowerState(DisplayPowerRequest request,
531 boolean waitForNegativeProximity) {
532 if (DEBUG) {
533 Slog.d(TAG, "requestPowerState: "
534 + request + ", waitForNegativeProximity=" + waitForNegativeProximity);
535 }
536
537 synchronized (mLock) {
538 boolean changed = false;
539
540 if (waitForNegativeProximity
541 && !mPendingWaitForNegativeProximityLocked) {
542 mPendingWaitForNegativeProximityLocked = true;
543 changed = true;
544 }
545
546 if (mPendingRequestLocked == null) {
547 mPendingRequestLocked = new DisplayPowerRequest(request);
548 changed = true;
549 } else if (!mPendingRequestLocked.equals(request)) {
550 mPendingRequestLocked.copyFrom(request);
551 changed = true;
552 }
553
554 if (changed) {
555 mDisplayReadyLocked = false;
556 }
557
558 if (changed && !mPendingRequestChangedLocked) {
559 mPendingRequestChangedLocked = true;
560 sendUpdatePowerStateLocked();
561 }
562
563 return mDisplayReadyLocked;
564 }
565 }
566
Kenny Guy6d1009f2018-03-14 14:28:23 +0000567 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
568 return mAutomaticBrightnessController.getDefaultConfig();
569 }
570
Jeff Brown96307042012-07-27 15:51:34 -0700571 private void sendUpdatePowerState() {
572 synchronized (mLock) {
573 sendUpdatePowerStateLocked();
574 }
575 }
576
577 private void sendUpdatePowerStateLocked() {
578 if (!mPendingUpdatePowerStateLocked) {
579 mPendingUpdatePowerStateLocked = true;
580 Message msg = mHandler.obtainMessage(MSG_UPDATE_POWER_STATE);
Jeff Brown96307042012-07-27 15:51:34 -0700581 mHandler.sendMessage(msg);
582 }
583 }
584
585 private void initialize() {
Jeff Brown037c33e2014-04-09 00:31:55 -0700586 // Initialize the power state object for the default display.
587 // In the future, we might manage multiple displays independently.
588 mPowerState = new DisplayPowerState(mBlanker,
Narayan Kamath38819982017-07-06 18:15:30 +0100589 mColorFadeEnabled ? new ColorFade(Display.DEFAULT_DISPLAY) : null);
Jeff Brown96307042012-07-27 15:51:34 -0700590
Narayan Kamath38819982017-07-06 18:15:30 +0100591 if (mColorFadeEnabled) {
592 mColorFadeOnAnimator = ObjectAnimator.ofFloat(
593 mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 0.0f, 1.0f);
594 mColorFadeOnAnimator.setDuration(COLOR_FADE_ON_ANIMATION_DURATION_MILLIS);
595 mColorFadeOnAnimator.addListener(mAnimatorListener);
Jeff Brown96307042012-07-27 15:51:34 -0700596
Narayan Kamath38819982017-07-06 18:15:30 +0100597 mColorFadeOffAnimator = ObjectAnimator.ofFloat(
598 mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 1.0f, 0.0f);
599 mColorFadeOffAnimator.setDuration(COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS);
600 mColorFadeOffAnimator.addListener(mAnimatorListener);
601 }
Jeff Brown96307042012-07-27 15:51:34 -0700602
603 mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>(
604 mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS);
Jeff Brown42558692014-05-20 22:02:46 -0700605 mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800606
Jeff Brown131206b2014-04-08 17:27:14 -0700607 // Initialize screen state for battery stats.
608 try {
Jeff Browne95c3cd2014-05-02 16:59:26 -0700609 mBatteryStats.noteScreenState(mPowerState.getScreenState());
Jeff Brown131206b2014-04-08 17:27:14 -0700610 mBatteryStats.noteScreenBrightness(mPowerState.getScreenBrightness());
611 } catch (RemoteException ex) {
612 // same process
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800613 }
Michael Wright144aac92017-12-21 18:37:41 +0000614
615 // Initialize all of the brightness tracking state
Michael Wrightd8460232018-01-16 18:04:59 +0000616 final float brightness = convertToNits(mPowerState.getScreenBrightness());
Michael Wright144aac92017-12-21 18:37:41 +0000617 if (brightness >= 0.0f) {
618 mBrightnessTracker.start(brightness);
619 }
Michael Wrightd8460232018-01-16 18:04:59 +0000620
621 mContext.getContentResolver().registerContentObserver(
622 Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
623 false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL);
624 mContext.getContentResolver().registerContentObserver(
625 Settings.System.getUriFor(Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ),
626 false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL);
Jeff Brown96307042012-07-27 15:51:34 -0700627 }
628
629 private final Animator.AnimatorListener mAnimatorListener = new Animator.AnimatorListener() {
630 @Override
631 public void onAnimationStart(Animator animation) {
632 }
633 @Override
634 public void onAnimationEnd(Animator animation) {
635 sendUpdatePowerState();
636 }
637 @Override
638 public void onAnimationRepeat(Animator animation) {
639 }
640 @Override
641 public void onAnimationCancel(Animator animation) {
642 }
643 };
644
Jeff Brown42558692014-05-20 22:02:46 -0700645 private final RampAnimator.Listener mRampAnimatorListener = new RampAnimator.Listener() {
646 @Override
647 public void onAnimationEnd() {
648 sendUpdatePowerState();
649 }
650 };
651
Jeff Brown96307042012-07-27 15:51:34 -0700652 private void updatePowerState() {
653 // Update the power state request.
654 final boolean mustNotify;
655 boolean mustInitialize = false;
Jeff Brown330560f2012-08-21 22:10:57 -0700656
Jeff Brown96307042012-07-27 15:51:34 -0700657 synchronized (mLock) {
658 mPendingUpdatePowerStateLocked = false;
659 if (mPendingRequestLocked == null) {
660 return; // wait until first actual power request
661 }
662
663 if (mPowerRequest == null) {
664 mPowerRequest = new DisplayPowerRequest(mPendingRequestLocked);
665 mWaitingForNegativeProximity = mPendingWaitForNegativeProximityLocked;
Jeff Brown6307a152012-08-20 13:24:23 -0700666 mPendingWaitForNegativeProximityLocked = false;
Jeff Brown96307042012-07-27 15:51:34 -0700667 mPendingRequestChangedLocked = false;
668 mustInitialize = true;
669 } else if (mPendingRequestChangedLocked) {
670 mPowerRequest.copyFrom(mPendingRequestLocked);
671 mWaitingForNegativeProximity |= mPendingWaitForNegativeProximityLocked;
Jeff Brown6307a152012-08-20 13:24:23 -0700672 mPendingWaitForNegativeProximityLocked = false;
Jeff Brown96307042012-07-27 15:51:34 -0700673 mPendingRequestChangedLocked = false;
674 mDisplayReadyLocked = false;
675 }
676
677 mustNotify = !mDisplayReadyLocked;
678 }
679
680 // Initialize things the first time the power state is changed.
681 if (mustInitialize) {
682 initialize();
683 }
684
Jeff Brown970d4132014-07-19 11:33:47 -0700685 // Compute the basic display state using the policy.
686 // We might override this below based on other factors.
687 int state;
688 int brightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700689 boolean performScreenOffTransition = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700690 switch (mPowerRequest.policy) {
691 case DisplayPowerRequest.POLICY_OFF:
692 state = Display.STATE_OFF;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700693 performScreenOffTransition = true;
Jeff Brown970d4132014-07-19 11:33:47 -0700694 break;
695 case DisplayPowerRequest.POLICY_DOZE:
696 if (mPowerRequest.dozeScreenState != Display.STATE_UNKNOWN) {
697 state = mPowerRequest.dozeScreenState;
698 } else {
699 state = Display.STATE_DOZE;
700 }
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700701 if (!mAllowAutoBrightnessWhileDozingConfig) {
702 brightness = mPowerRequest.dozeScreenBrightness;
703 }
Jeff Brown970d4132014-07-19 11:33:47 -0700704 break;
Santos Cordon3107d292016-09-20 15:50:35 -0700705 case DisplayPowerRequest.POLICY_VR:
706 state = Display.STATE_VR;
707 break;
Jeff Brown970d4132014-07-19 11:33:47 -0700708 case DisplayPowerRequest.POLICY_DIM:
709 case DisplayPowerRequest.POLICY_BRIGHT:
710 default:
711 state = Display.STATE_ON;
712 break;
713 }
Jeff Brown2175e9c2014-09-12 16:11:07 -0700714 assert(state != Display.STATE_UNKNOWN);
Jeff Brown970d4132014-07-19 11:33:47 -0700715
Jeff Brown6307a152012-08-20 13:24:23 -0700716 // Apply the proximity sensor.
Jeff Brown96307042012-07-27 15:51:34 -0700717 if (mProximitySensor != null) {
Jeff Brown970d4132014-07-19 11:33:47 -0700718 if (mPowerRequest.useProximitySensor && state != Display.STATE_OFF) {
Jeff Brown6307a152012-08-20 13:24:23 -0700719 setProximitySensorEnabled(true);
720 if (!mScreenOffBecauseOfProximity
721 && mProximity == PROXIMITY_POSITIVE) {
722 mScreenOffBecauseOfProximity = true;
Jeff Brownec083212013-09-11 20:45:25 -0700723 sendOnProximityPositiveWithWakelock();
Jeff Brown6307a152012-08-20 13:24:23 -0700724 }
725 } else if (mWaitingForNegativeProximity
726 && mScreenOffBecauseOfProximity
727 && mProximity == PROXIMITY_POSITIVE
Jeff Brown970d4132014-07-19 11:33:47 -0700728 && state != Display.STATE_OFF) {
Jeff Brown6307a152012-08-20 13:24:23 -0700729 setProximitySensorEnabled(true);
730 } else {
731 setProximitySensorEnabled(false);
732 mWaitingForNegativeProximity = false;
733 }
734 if (mScreenOffBecauseOfProximity
735 && mProximity != PROXIMITY_POSITIVE) {
Jeff Brown96307042012-07-27 15:51:34 -0700736 mScreenOffBecauseOfProximity = false;
Jeff Brownec083212013-09-11 20:45:25 -0700737 sendOnProximityNegativeWithWakelock();
Jeff Brown96307042012-07-27 15:51:34 -0700738 }
Jeff Brown6307a152012-08-20 13:24:23 -0700739 } else {
740 mWaitingForNegativeProximity = false;
Jeff Brown96307042012-07-27 15:51:34 -0700741 }
Jeff Brown970d4132014-07-19 11:33:47 -0700742 if (mScreenOffBecauseOfProximity) {
743 state = Display.STATE_OFF;
Jeff Brown96307042012-07-27 15:51:34 -0700744 }
745
Jeff Brownbf4e4142014-10-02 13:08:05 -0700746 // Animate the screen state change unless already animating.
747 // The transition may be deferred, so after this point we will use the
748 // actual state instead of the desired one.
Santos Cordon3107d292016-09-20 15:50:35 -0700749 final int oldState = mPowerState.getScreenState();
Jeff Brownbf4e4142014-10-02 13:08:05 -0700750 animateScreenStateChange(state, performScreenOffTransition);
751 state = mPowerState.getScreenState();
752
Jeff Brown970d4132014-07-19 11:33:47 -0700753 // Use zero brightness when screen is off.
754 if (state == Display.STATE_OFF) {
755 brightness = PowerManager.BRIGHTNESS_OFF;
756 }
757
Michael Wrightd8460232018-01-16 18:04:59 +0000758 // Always use the VR brightness when in the VR state.
759 if (state == Display.STATE_VR) {
760 brightness = mScreenBrightnessForVr;
761 }
762
763 if (brightness < 0 && mPowerRequest.screenBrightnessOverride > 0) {
764 brightness = mPowerRequest.screenBrightnessOverride;
765 }
Michael Wright144aac92017-12-21 18:37:41 +0000766
767 final boolean autoBrightnessEnabledInDoze =
768 mAllowAutoBrightnessWhileDozingConfig && Display.isDozeState(state);
769 final boolean autoBrightnessEnabled = mPowerRequest.useAutoBrightness
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700770 && (state == Display.STATE_ON || autoBrightnessEnabledInDoze)
Michael Wright144aac92017-12-21 18:37:41 +0000771 && brightness < 0
772 && mAutomaticBrightnessController != null;
Michael Wrightd8460232018-01-16 18:04:59 +0000773 boolean brightnessIsTemporary = false;
Michael Wright144aac92017-12-21 18:37:41 +0000774
Michael Wrightd8460232018-01-16 18:04:59 +0000775 final boolean userSetBrightnessChanged = updateUserSetScreenBrightness();
776 if (userSetBrightnessChanged) {
777 mTemporaryScreenBrightness = -1;
Michael Wright144aac92017-12-21 18:37:41 +0000778 }
779
Michael Wrightd8460232018-01-16 18:04:59 +0000780 // Use the temporary screen brightness if there isn't an override, either from
781 // WindowManager or based on the display state.
782 if (mTemporaryScreenBrightness > 0) {
783 brightness = mTemporaryScreenBrightness;
784 brightnessIsTemporary = true;
785 }
786
787 final boolean autoBrightnessAdjustmentChanged = updateAutoBrightnessAdjustment();
788 if (autoBrightnessAdjustmentChanged) {
789 mTemporaryAutoBrightnessAdjustment = Float.NaN;
790 }
791
792 // Use the autobrightness adjustment override if set.
793 final float autoBrightnessAdjustment;
794 if (!Float.isNaN(mTemporaryAutoBrightnessAdjustment)) {
795 autoBrightnessAdjustment = mTemporaryAutoBrightnessAdjustment;
796 brightnessIsTemporary = true;
797 } else {
798 autoBrightnessAdjustment = mAutoBrightnessAdjustment;
799 }
800
801 // Apply brightness boost.
802 // We do this here after deciding whether auto-brightness is enabled so that we don't
803 // disable the light sensor during this temporary state. That way when boost ends we will
804 // be able to resume normal auto-brightness behavior without any delay.
805 if (mPowerRequest.boostScreenBrightness
806 && brightness != PowerManager.BRIGHTNESS_OFF) {
807 brightness = PowerManager.BRIGHTNESS_ON;
808 }
809
Kenny Guy53d06612018-01-30 14:19:13 +0000810 // If the brightness is already set then it's been overridden by something other than the
Michael Wrightd8460232018-01-16 18:04:59 +0000811 // user, or is a temporary adjustment.
812 final boolean userInitiatedChange = brightness < 0
813 && (autoBrightnessAdjustmentChanged || userSetBrightnessChanged);
814
Kenny Guy53d06612018-01-30 14:19:13 +0000815 boolean hadUserBrightnessPoint = false;
Michael Wright144aac92017-12-21 18:37:41 +0000816 // Configure auto-brightness.
817 if (mAutomaticBrightnessController != null) {
Kenny Guy53d06612018-01-30 14:19:13 +0000818 hadUserBrightnessPoint = mAutomaticBrightnessController.hasUserDataPoints();
Jeff Brown970d4132014-07-19 11:33:47 -0700819 mAutomaticBrightnessController.configure(autoBrightnessEnabled,
Michael Wrightd8460232018-01-16 18:04:59 +0000820 mBrightnessConfiguration,
821 mLastUserSetScreenBrightness / (float) PowerManager.BRIGHTNESS_ON,
Michael Wright617564f2018-01-25 22:20:54 +0000822 userSetBrightnessChanged, autoBrightnessAdjustment,
823 autoBrightnessAdjustmentChanged, mPowerRequest.policy);
Jeff Brown7b5be5e2014-11-12 18:45:31 -0800824 }
825
Jeff Brown970d4132014-07-19 11:33:47 -0700826 // Apply auto-brightness.
827 boolean slowChange = false;
828 if (brightness < 0) {
Michael Wright617564f2018-01-25 22:20:54 +0000829 float newAutoBrightnessAdjustment = autoBrightnessAdjustment;
Jeff Brown970d4132014-07-19 11:33:47 -0700830 if (autoBrightnessEnabled) {
831 brightness = mAutomaticBrightnessController.getAutomaticScreenBrightness();
Michael Wright617564f2018-01-25 22:20:54 +0000832 newAutoBrightnessAdjustment =
833 mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment();
Jeff Brown970d4132014-07-19 11:33:47 -0700834 }
Michael Wright617564f2018-01-25 22:20:54 +0000835
Jeff Brown970d4132014-07-19 11:33:47 -0700836 if (brightness >= 0) {
837 // Use current auto-brightness value and slowly adjust to changes.
838 brightness = clampScreenBrightness(brightness);
839 if (mAppliedAutoBrightness && !autoBrightnessAdjustmentChanged) {
840 slowChange = true; // slowly adapt to auto-brightness
841 }
Michael Wrightd8460232018-01-16 18:04:59 +0000842 // Tell the rest of the system about the new brightness. Note that we do this
843 // before applying the low power or dim transformations so that the slider
844 // accurately represents the full possible range, even if they range changes what
845 // it means in absolute terms.
846 putScreenBrightnessSetting(brightness);
Jeff Brown970d4132014-07-19 11:33:47 -0700847 mAppliedAutoBrightness = true;
Jeff Brown96307042012-07-27 15:51:34 -0700848 } else {
Jeff Brown970d4132014-07-19 11:33:47 -0700849 mAppliedAutoBrightness = false;
Jeff Brown96307042012-07-27 15:51:34 -0700850 }
Michael Wright617564f2018-01-25 22:20:54 +0000851 if (autoBrightnessAdjustment != newAutoBrightnessAdjustment) {
852 // If the autobrightness controller has decided to change the adjustment value
853 // used, make sure that's reflected in settings.
854 putAutoBrightnessAdjustmentSetting(newAutoBrightnessAdjustment);
855 }
Jeff Brown96307042012-07-27 15:51:34 -0700856 } else {
Jeff Brown970d4132014-07-19 11:33:47 -0700857 mAppliedAutoBrightness = false;
858 }
859
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700860 // Use default brightness when dozing unless overridden.
Michael Wrightb0a1d3d2017-09-22 15:05:02 +0100861 if (brightness < 0 && Display.isDozeState(state)) {
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700862 brightness = mScreenBrightnessDozeConfig;
863 }
864
Jeff Brown970d4132014-07-19 11:33:47 -0700865 // Apply manual brightness.
Jeff Brown970d4132014-07-19 11:33:47 -0700866 if (brightness < 0) {
Michael Wright7bb22342018-01-26 00:16:41 +0000867 brightness = clampScreenBrightness(mCurrentScreenBrightnessSetting);
Jeff Brown970d4132014-07-19 11:33:47 -0700868 }
869
Michael Wrightd8460232018-01-16 18:04:59 +0000870
Jeff Brown970d4132014-07-19 11:33:47 -0700871 // Apply dimming by at least some minimum amount when user activity
872 // timeout is about to expire.
873 if (mPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) {
Jeff Brown5c8ea082014-07-24 19:30:31 -0700874 if (brightness > mScreenBrightnessRangeMinimum) {
875 brightness = Math.max(Math.min(brightness - SCREEN_DIM_MINIMUM_REDUCTION,
876 mScreenBrightnessDimConfig), mScreenBrightnessRangeMinimum);
877 }
Jeff Brown970d4132014-07-19 11:33:47 -0700878 if (!mAppliedDimming) {
879 slowChange = false;
880 }
881 mAppliedDimming = true;
mochangmingf0cb46c2015-12-29 13:55:24 +0800882 } else if (mAppliedDimming) {
883 slowChange = false;
884 mAppliedDimming = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700885 }
886
jackqdyulei92681e82017-02-28 11:26:28 -0800887 // If low power mode is enabled, scale brightness by screenLowPowerBrightnessFactor
Jeff Brown970d4132014-07-19 11:33:47 -0700888 // as long as it is above the minimum threshold.
889 if (mPowerRequest.lowPowerMode) {
890 if (brightness > mScreenBrightnessRangeMinimum) {
jackqdyulei92681e82017-02-28 11:26:28 -0800891 final float brightnessFactor =
892 Math.min(mPowerRequest.screenLowPowerBrightnessFactor, 1);
893 final int lowPowerBrightness = (int) (brightness * brightnessFactor);
894 brightness = Math.max(lowPowerBrightness, mScreenBrightnessRangeMinimum);
Jeff Brown970d4132014-07-19 11:33:47 -0700895 }
896 if (!mAppliedLowPower) {
897 slowChange = false;
898 }
899 mAppliedLowPower = true;
mochangmingf0cb46c2015-12-29 13:55:24 +0800900 } else if (mAppliedLowPower) {
901 slowChange = false;
902 mAppliedLowPower = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700903 }
904
Jeff Brown0a434772014-09-30 14:42:27 -0700905 // Animate the screen brightness when the screen is on or dozing.
Santos Cordon3107d292016-09-20 15:50:35 -0700906 // Skip the animation when the screen is off or suspended or transition to/from VR.
Prashant Malani33538242014-11-13 14:04:00 -0800907 if (!mPendingScreenOff) {
Julius D'souzad5105dd2017-06-02 11:03:53 -0700908 if (mSkipScreenOnBrightnessRamp) {
Julius D'souzad5105dd2017-06-02 11:03:53 -0700909 if (state == Display.STATE_ON) {
910 if (mSkipRampState == RAMP_STATE_SKIP_NONE && mDozing) {
911 mInitialAutoBrightness = brightness;
912 mSkipRampState = RAMP_STATE_SKIP_INITIAL;
913 } else if (mSkipRampState == RAMP_STATE_SKIP_INITIAL
914 && mUseSoftwareAutoBrightnessConfig
915 && brightness != mInitialAutoBrightness) {
916 mSkipRampState = RAMP_STATE_SKIP_AUTOBRIGHT;
917 } else if (mSkipRampState == RAMP_STATE_SKIP_AUTOBRIGHT) {
918 mSkipRampState = RAMP_STATE_SKIP_NONE;
919 }
920 } else {
921 mSkipRampState = RAMP_STATE_SKIP_NONE;
922 }
923 }
924
Michael Wrightb0a1d3d2017-09-22 15:05:02 +0100925 final boolean wasOrWillBeInVr =
926 (state == Display.STATE_VR || oldState == Display.STATE_VR);
927 final boolean initialRampSkip =
928 state == Display.STATE_ON && mSkipRampState != RAMP_STATE_SKIP_NONE;
929 // While dozing, sometimes the brightness is split into buckets. Rather than animating
930 // through the buckets, which is unlikely to be smooth in the first place, just jump
931 // right to the suggested brightness.
932 final boolean hasBrightnessBuckets =
933 Display.isDozeState(state) && mBrightnessBucketsInDozeConfig;
934 // If the color fade is totally covering the screen then we can change the backlight
935 // level without it being a noticeable jump since any actual content isn't yet visible.
936 final boolean isDisplayContentVisible =
937 mColorFadeEnabled && mPowerState.getColorFadeLevel() == 1.0f;
938 if (initialRampSkip || hasBrightnessBuckets
Michael Wrightd8460232018-01-16 18:04:59 +0000939 || wasOrWillBeInVr || !isDisplayContentVisible || brightnessIsTemporary) {
Michael Wrightb0a1d3d2017-09-22 15:05:02 +0100940 animateScreenBrightness(brightness, 0);
941 } else {
Prashant Malani33538242014-11-13 14:04:00 -0800942 animateScreenBrightness(brightness,
Julius D'souzafeadad12016-08-05 14:34:38 -0700943 slowChange ? mBrightnessRampRateSlow : mBrightnessRampRateFast);
Prashant Malani33538242014-11-13 14:04:00 -0800944 }
Michael Wright144aac92017-12-21 18:37:41 +0000945
Michael Wrightd8460232018-01-16 18:04:59 +0000946 if (!brightnessIsTemporary) {
Kenny Guy53d06612018-01-30 14:19:13 +0000947 notifyBrightnessChanged(brightness, userInitiatedChange, hadUserBrightnessPoint);
Michael Wright144aac92017-12-21 18:37:41 +0000948 }
Michael Wrightd8460232018-01-16 18:04:59 +0000949
Jeff Brown0a434772014-09-30 14:42:27 -0700950 }
951
952 // Determine whether the display is ready for use in the newly requested state.
953 // Note that we do not wait for the brightness ramp animation to complete before
954 // reporting the display is ready because we only need to ensure the screen is in the
955 // right power state even as it continues to converge on the desired brightness.
Narayan Kamath38819982017-07-06 18:15:30 +0100956 final boolean ready = mPendingScreenOnUnblocker == null &&
957 (!mColorFadeEnabled ||
958 (!mColorFadeOnAnimator.isStarted() && !mColorFadeOffAnimator.isStarted()))
Jeff Brown0a434772014-09-30 14:42:27 -0700959 && mPowerState.waitUntilClean(mCleanListener);
960 final boolean finished = ready
961 && !mScreenBrightnessRampAnimator.isAnimating();
962
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700963 // Notify policy about screen turned on.
964 if (ready && state != Display.STATE_OFF
965 && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_ON) {
Michael Wrightc3e6af82017-07-25 22:31:03 +0100966 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_ON);
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700967 mWindowManagerPolicy.screenTurnedOn();
968 }
969
Jeff Brown0a434772014-09-30 14:42:27 -0700970 // Grab a wake lock if we have unfinished business.
971 if (!finished && !mUnfinishedBusiness) {
972 if (DEBUG) {
973 Slog.d(TAG, "Unfinished business...");
974 }
975 mCallbacks.acquireSuspendBlocker();
976 mUnfinishedBusiness = true;
977 }
978
979 // Notify the power manager when ready.
980 if (ready && mustNotify) {
981 // Send state change.
Jeff Brown96307042012-07-27 15:51:34 -0700982 synchronized (mLock) {
983 if (!mPendingRequestChangedLocked) {
984 mDisplayReadyLocked = true;
Jeff Brownc38c9be2012-10-04 13:16:19 -0700985
986 if (DEBUG) {
987 Slog.d(TAG, "Display ready!");
988 }
Jeff Brown96307042012-07-27 15:51:34 -0700989 }
990 }
Jeff Brownec083212013-09-11 20:45:25 -0700991 sendOnStateChangedWithWakelock();
Jeff Brown96307042012-07-27 15:51:34 -0700992 }
Jeff Brown0a434772014-09-30 14:42:27 -0700993
994 // Release the wake lock when we have no unfinished business.
995 if (finished && mUnfinishedBusiness) {
996 if (DEBUG) {
997 Slog.d(TAG, "Finished business...");
998 }
999 mUnfinishedBusiness = false;
1000 mCallbacks.releaseSuspendBlocker();
1001 }
Julius D'souzad5105dd2017-06-02 11:03:53 -07001002
1003 // Record if dozing for future comparison.
1004 mDozing = state != Display.STATE_ON;
Jeff Brown96307042012-07-27 15:51:34 -07001005 }
1006
Michael Wright639c8be2014-01-17 18:29:12 -08001007 @Override
1008 public void updateBrightness() {
1009 sendUpdatePowerState();
1010 }
1011
Michael Wrighteef0e132017-11-21 17:57:52 +00001012 public void setBrightnessConfiguration(BrightnessConfiguration c) {
1013 Message msg = mHandler.obtainMessage(MSG_CONFIGURE_BRIGHTNESS, c);
1014 msg.sendToTarget();
1015 }
1016
Michael Wrightd8460232018-01-16 18:04:59 +00001017 public void setTemporaryBrightness(int brightness) {
1018 Message msg = mHandler.obtainMessage(MSG_SET_TEMPORARY_BRIGHTNESS,
1019 brightness, 0 /*unused*/);
1020 msg.sendToTarget();
1021 }
1022
1023 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
1024 Message msg = mHandler.obtainMessage(MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT,
1025 Float.floatToIntBits(adjustment), 0 /*unused*/);
1026 msg.sendToTarget();
1027 }
1028
Jeff Brown8b9cf1c2012-10-07 14:54:17 -07001029 private void blockScreenOn() {
Jeff Brown3ee549c2014-09-22 20:14:39 -07001030 if (mPendingScreenOnUnblocker == null) {
Jeff Brown3edf5272014-08-14 19:25:14 -07001031 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001032 mPendingScreenOnUnblocker = new ScreenOnUnblocker();
Jeff Brown037c33e2014-04-09 00:31:55 -07001033 mScreenOnBlockStartRealTime = SystemClock.elapsedRealtime();
Jeff Brown3edf5272014-08-14 19:25:14 -07001034 Slog.i(TAG, "Blocking screen on until initial contents have been drawn.");
Jeff Brown8b9cf1c2012-10-07 14:54:17 -07001035 }
1036 }
1037
1038 private void unblockScreenOn() {
Jeff Brown3ee549c2014-09-22 20:14:39 -07001039 if (mPendingScreenOnUnblocker != null) {
1040 mPendingScreenOnUnblocker = null;
Jeff Brown2d8a3902014-03-11 23:02:35 -07001041 long delay = SystemClock.elapsedRealtime() - mScreenOnBlockStartRealTime;
Jeff Brown3edf5272014-08-14 19:25:14 -07001042 Slog.i(TAG, "Unblocked screen on after " + delay + " ms");
1043 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0);
Jeff Brown8b9cf1c2012-10-07 14:54:17 -07001044 }
1045 }
1046
Jorim Jaggi51304d72017-05-17 17:25:32 +02001047 private void blockScreenOff() {
1048 if (mPendingScreenOffUnblocker == null) {
1049 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_OFF_BLOCKED_TRACE_NAME, 0);
1050 mPendingScreenOffUnblocker = new ScreenOffUnblocker();
1051 mScreenOffBlockStartRealTime = SystemClock.elapsedRealtime();
1052 Slog.i(TAG, "Blocking screen off");
1053 }
1054 }
1055
1056 private void unblockScreenOff() {
1057 if (mPendingScreenOffUnblocker != null) {
1058 mPendingScreenOffUnblocker = null;
1059 long delay = SystemClock.elapsedRealtime() - mScreenOffBlockStartRealTime;
1060 Slog.i(TAG, "Unblocked screen off after " + delay + " ms");
1061 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_OFF_BLOCKED_TRACE_NAME, 0);
1062 }
1063 }
1064
Jeff Brown3ee549c2014-09-22 20:14:39 -07001065 private boolean setScreenState(int state) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001066 return setScreenState(state, false /*reportOnly*/);
Michael Wright05e76fe2017-07-20 18:18:33 +01001067 }
1068
Michael Wrightc3e6af82017-07-25 22:31:03 +01001069 private boolean setScreenState(int state, boolean reportOnly) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02001070 final boolean isOff = (state == Display.STATE_OFF);
Jeff Brown037c33e2014-04-09 00:31:55 -07001071 if (mPowerState.getScreenState() != state) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02001072
1073 // If we are trying to turn screen off, give policy a chance to do something before we
1074 // actually turn the screen off.
1075 if (isOff && !mScreenOffBecauseOfProximity) {
1076 if (mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_ON) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001077 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_TURNING_OFF);
Jorim Jaggi51304d72017-05-17 17:25:32 +02001078 blockScreenOff();
1079 mWindowManagerPolicy.screenTurningOff(mPendingScreenOffUnblocker);
Michael Wrightc3e6af82017-07-25 22:31:03 +01001080 unblockScreenOff();
Jorim Jaggi51304d72017-05-17 17:25:32 +02001081 } else if (mPendingScreenOffUnblocker != null) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02001082 // Abort doing the state change until screen off is unblocked.
1083 return false;
1084 }
1085 }
1086
Michael Wrightc3e6af82017-07-25 22:31:03 +01001087 if (!reportOnly) {
Michael Wrightb0a1d3d2017-09-22 15:05:02 +01001088 Trace.traceCounter(Trace.TRACE_TAG_POWER, "ScreenState", state);
Michael Wrightc3e6af82017-07-25 22:31:03 +01001089 mPowerState.setScreenState(state);
1090 // Tell battery stats about the transition.
1091 try {
1092 mBatteryStats.noteScreenState(state);
1093 } catch (RemoteException ex) {
1094 // same process
1095 }
Jeff Brown96307042012-07-27 15:51:34 -07001096 }
1097 }
Jeff Browne1633ad2015-06-22 19:24:24 -07001098
1099 // Tell the window manager policy when the screen is turned off or on unless it's due
1100 // to the proximity sensor. We temporarily block turning the screen on until the
1101 // window manager is ready by leaving a black surface covering the screen.
1102 // This surface is essentially the final state of the color fade animation and
1103 // it is only removed once the window manager tells us that the activity has
1104 // finished drawing underneath.
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001105 if (isOff && mReportedScreenStateToPolicy != REPORTED_TO_POLICY_SCREEN_OFF
1106 && !mScreenOffBecauseOfProximity) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001107 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_OFF);
Jeff Browne1633ad2015-06-22 19:24:24 -07001108 unblockScreenOn();
1109 mWindowManagerPolicy.screenTurnedOff();
Jorim Jaggi51304d72017-05-17 17:25:32 +02001110 } else if (!isOff
1111 && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_OFF) {
1112
1113 // We told policy already that screen was turning off, but now we changed our minds.
1114 // Complete the full state transition on -> turningOff -> off.
1115 unblockScreenOff();
1116 mWindowManagerPolicy.screenTurnedOff();
Michael Wrightc3e6af82017-07-25 22:31:03 +01001117 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_OFF);
Jorim Jaggi51304d72017-05-17 17:25:32 +02001118 }
1119 if (!isOff && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_OFF) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001120 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_TURNING_ON);
Jeff Browne1633ad2015-06-22 19:24:24 -07001121 if (mPowerState.getColorFadeLevel() == 0.0f) {
1122 blockScreenOn();
1123 } else {
1124 unblockScreenOn();
1125 }
1126 mWindowManagerPolicy.screenTurningOn(mPendingScreenOnUnblocker);
1127 }
1128
1129 // Return true if the screen isn't blocked.
Jeff Brown3ee549c2014-09-22 20:14:39 -07001130 return mPendingScreenOnUnblocker == null;
Jeff Brown96307042012-07-27 15:51:34 -07001131 }
1132
Michael Wrightc3e6af82017-07-25 22:31:03 +01001133 private void setReportedScreenState(int state) {
1134 Trace.traceCounter(Trace.TRACE_TAG_POWER, "ReportedScreenStateToPolicy", state);
1135 mReportedScreenStateToPolicy = state;
1136 }
1137
Jeff Brown330560f2012-08-21 22:10:57 -07001138 private int clampScreenBrightness(int value) {
Michael Wright639c8be2014-01-17 18:29:12 -08001139 return MathUtils.constrain(
1140 value, mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum);
Jeff Brown330560f2012-08-21 22:10:57 -07001141 }
1142
Jeff Brown96307042012-07-27 15:51:34 -07001143 private void animateScreenBrightness(int target, int rate) {
Jeff Brown0a434772014-09-30 14:42:27 -07001144 if (DEBUG) {
1145 Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate);
1146 }
Jeff Brown96307042012-07-27 15:51:34 -07001147 if (mScreenBrightnessRampAnimator.animateTo(target, rate)) {
Michael Wrighta9f37ab2017-08-15 17:14:20 +01001148 Trace.traceCounter(Trace.TRACE_TAG_POWER, "TargetScreenBrightness", target);
Jeff Brown131206b2014-04-08 17:27:14 -07001149 try {
1150 mBatteryStats.noteScreenBrightness(target);
1151 } catch (RemoteException ex) {
1152 // same process
1153 }
Jeff Brown96307042012-07-27 15:51:34 -07001154 }
1155 }
1156
Jeff Brown606e4e82014-09-18 15:22:26 -07001157 private void animateScreenStateChange(int target, boolean performScreenOffTransition) {
1158 // If there is already an animation in progress, don't interfere with it.
Narayan Kamath38819982017-07-06 18:15:30 +01001159 if (mColorFadeEnabled &&
1160 (mColorFadeOnAnimator.isStarted() || mColorFadeOffAnimator.isStarted())) {
Chong Zhangb131c702016-02-16 16:31:48 -08001161 if (target != Display.STATE_ON) {
1162 return;
1163 }
1164 // If display state changed to on, proceed and stop the color fade and turn screen on.
1165 mPendingScreenOff = false;
Jeff Brown606e4e82014-09-18 15:22:26 -07001166 }
1167
Michael Wrightc3e6af82017-07-25 22:31:03 +01001168 if (mDisplayBlanksAfterDozeConfig
1169 && Display.isDozeState(mPowerState.getScreenState())
Michael Wright05e76fe2017-07-20 18:18:33 +01001170 && !Display.isDozeState(target)) {
Michael Wright05e76fe2017-07-20 18:18:33 +01001171 // Skip the screen off animation and add a black surface to hide the
Michael Wrightc3e6af82017-07-25 22:31:03 +01001172 // contents of the screen.
1173 mPowerState.prepareColorFade(mContext,
1174 mColorFadeFadesConfig ? ColorFade.MODE_FADE : ColorFade.MODE_WARM_UP);
Lucas Dupina84952b2017-08-15 15:56:50 -07001175 if (mColorFadeOffAnimator != null) {
1176 mColorFadeOffAnimator.end();
1177 }
Michael Wrightc3e6af82017-07-25 22:31:03 +01001178 // Some display hardware will blank itself on the transition between doze and non-doze
1179 // but still on display states. In this case we want to report to policy that the
1180 // display has turned off so it can prepare the appropriate power on animation, but we
1181 // don't want to actually transition to the fully off state since that takes
1182 // significantly longer to transition from.
1183 setScreenState(Display.STATE_OFF, target != Display.STATE_OFF /*reportOnly*/);
Michael Wright05e76fe2017-07-20 18:18:33 +01001184 }
1185
Jeff Brown3ee549c2014-09-22 20:14:39 -07001186 // If we were in the process of turning off the screen but didn't quite
1187 // finish. Then finish up now to prevent a jarring transition back
1188 // to screen on if we skipped blocking screen on as usual.
1189 if (mPendingScreenOff && target != Display.STATE_OFF) {
1190 setScreenState(Display.STATE_OFF);
1191 mPendingScreenOff = false;
Michael Lentined73854d2015-09-25 10:55:26 -07001192 mPowerState.dismissColorFadeResources();
Jeff Brown606e4e82014-09-18 15:22:26 -07001193 }
1194
1195 if (target == Display.STATE_ON) {
1196 // Want screen on. The contents of the screen may not yet
Jeff Brown3ee549c2014-09-22 20:14:39 -07001197 // be visible if the color fade has not been dismissed because
Jeff Brown606e4e82014-09-18 15:22:26 -07001198 // its last frame of animation is solid black.
Jeff Brown3ee549c2014-09-22 20:14:39 -07001199 if (!setScreenState(Display.STATE_ON)) {
1200 return; // screen on blocked
1201 }
Narayan Kamath38819982017-07-06 18:15:30 +01001202 if (USE_COLOR_FADE_ON_ANIMATION && mColorFadeEnabled && mPowerRequest.isBrightOrDim()) {
Jeff Brown606e4e82014-09-18 15:22:26 -07001203 // Perform screen on animation.
1204 if (mPowerState.getColorFadeLevel() == 1.0f) {
1205 mPowerState.dismissColorFade();
1206 } else if (mPowerState.prepareColorFade(mContext,
1207 mColorFadeFadesConfig ?
1208 ColorFade.MODE_FADE :
1209 ColorFade.MODE_WARM_UP)) {
1210 mColorFadeOnAnimator.start();
1211 } else {
1212 mColorFadeOnAnimator.end();
1213 }
1214 } else {
1215 // Skip screen on animation.
1216 mPowerState.setColorFadeLevel(1.0f);
1217 mPowerState.dismissColorFade();
1218 }
Santos Cordon3107d292016-09-20 15:50:35 -07001219 } else if (target == Display.STATE_VR) {
1220 // Wait for brightness animation to complete beforehand when entering VR
1221 // from screen on to prevent a perceptible jump because brightness may operate
1222 // differently when the display is configured for dozing.
1223 if (mScreenBrightnessRampAnimator.isAnimating()
1224 && mPowerState.getScreenState() == Display.STATE_ON) {
1225 return;
1226 }
1227
1228 // Set screen state.
1229 if (!setScreenState(Display.STATE_VR)) {
1230 return; // screen on blocked
1231 }
1232
1233 // Dismiss the black surface without fanfare.
1234 mPowerState.setColorFadeLevel(1.0f);
1235 mPowerState.dismissColorFade();
Jeff Brown606e4e82014-09-18 15:22:26 -07001236 } else if (target == Display.STATE_DOZE) {
1237 // Want screen dozing.
1238 // Wait for brightness animation to complete beforehand when entering doze
1239 // from screen on to prevent a perceptible jump because brightness may operate
1240 // differently when the display is configured for dozing.
1241 if (mScreenBrightnessRampAnimator.isAnimating()
1242 && mPowerState.getScreenState() == Display.STATE_ON) {
1243 return;
1244 }
1245
Jeff Brown3ee549c2014-09-22 20:14:39 -07001246 // Set screen state.
1247 if (!setScreenState(Display.STATE_DOZE)) {
1248 return; // screen on blocked
1249 }
1250
1251 // Dismiss the black surface without fanfare.
Jeff Brown606e4e82014-09-18 15:22:26 -07001252 mPowerState.setColorFadeLevel(1.0f);
1253 mPowerState.dismissColorFade();
1254 } else if (target == Display.STATE_DOZE_SUSPEND) {
1255 // Want screen dozing and suspended.
1256 // Wait for brightness animation to complete beforehand unless already
1257 // suspended because we may not be able to change it after suspension.
1258 if (mScreenBrightnessRampAnimator.isAnimating()
1259 && mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) {
1260 return;
1261 }
1262
Jeff Brown3ee549c2014-09-22 20:14:39 -07001263 // If not already suspending, temporarily set the state to doze until the
1264 // screen on is unblocked, then suspend.
1265 if (mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) {
1266 if (!setScreenState(Display.STATE_DOZE)) {
1267 return; // screen on blocked
1268 }
1269 setScreenState(Display.STATE_DOZE_SUSPEND); // already on so can't block
1270 }
1271
1272 // Dismiss the black surface without fanfare.
Jeff Brown606e4e82014-09-18 15:22:26 -07001273 mPowerState.setColorFadeLevel(1.0f);
1274 mPowerState.dismissColorFade();
Chris Phoenix10a4a642017-09-25 13:21:00 -07001275 } else if (target == Display.STATE_ON_SUSPEND) {
1276 // Want screen full-power and suspended.
1277 // Wait for brightness animation to complete beforehand unless already
1278 // suspended because we may not be able to change it after suspension.
1279 if (mScreenBrightnessRampAnimator.isAnimating()
1280 && mPowerState.getScreenState() != Display.STATE_ON_SUSPEND) {
1281 return;
1282 }
1283
1284 // If not already suspending, temporarily set the state to on until the
1285 // screen on is unblocked, then suspend.
1286 if (mPowerState.getScreenState() != Display.STATE_ON_SUSPEND) {
1287 if (!setScreenState(Display.STATE_ON)) {
1288 return;
1289 }
1290 setScreenState(Display.STATE_ON_SUSPEND);
1291 }
1292
1293 // Dismiss the black surface without fanfare.
1294 mPowerState.setColorFadeLevel(1.0f);
1295 mPowerState.dismissColorFade();
Jeff Brown606e4e82014-09-18 15:22:26 -07001296 } else {
1297 // Want screen off.
Jeff Brown3ee549c2014-09-22 20:14:39 -07001298 mPendingScreenOff = true;
Narayan Kamath38819982017-07-06 18:15:30 +01001299 if (!mColorFadeEnabled) {
1300 mPowerState.setColorFadeLevel(0.0f);
1301 }
1302
Jeff Brown606e4e82014-09-18 15:22:26 -07001303 if (mPowerState.getColorFadeLevel() == 0.0f) {
1304 // Turn the screen off.
1305 // A black surface is already hiding the contents of the screen.
1306 setScreenState(Display.STATE_OFF);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001307 mPendingScreenOff = false;
Michael Lentined73854d2015-09-25 10:55:26 -07001308 mPowerState.dismissColorFadeResources();
Jeff Brown606e4e82014-09-18 15:22:26 -07001309 } else if (performScreenOffTransition
1310 && mPowerState.prepareColorFade(mContext,
1311 mColorFadeFadesConfig ?
1312 ColorFade.MODE_FADE : ColorFade.MODE_COOL_DOWN)
1313 && mPowerState.getScreenState() != Display.STATE_OFF) {
1314 // Perform the screen off animation.
1315 mColorFadeOffAnimator.start();
1316 } else {
1317 // Skip the screen off animation and add a black surface to hide the
1318 // contents of the screen.
1319 mColorFadeOffAnimator.end();
1320 }
1321 }
1322 }
1323
Jeff Brown96307042012-07-27 15:51:34 -07001324 private final Runnable mCleanListener = new Runnable() {
1325 @Override
1326 public void run() {
1327 sendUpdatePowerState();
1328 }
1329 };
1330
1331 private void setProximitySensorEnabled(boolean enable) {
1332 if (enable) {
1333 if (!mProximitySensorEnabled) {
Jeff Brownec083212013-09-11 20:45:25 -07001334 // Register the listener.
1335 // Proximity sensor state already cleared initially.
Jeff Brown96307042012-07-27 15:51:34 -07001336 mProximitySensorEnabled = true;
Jeff Brown96307042012-07-27 15:51:34 -07001337 mSensorManager.registerListener(mProximitySensorListener, mProximitySensor,
1338 SensorManager.SENSOR_DELAY_NORMAL, mHandler);
1339 }
1340 } else {
1341 if (mProximitySensorEnabled) {
Jeff Brownec083212013-09-11 20:45:25 -07001342 // Unregister the listener.
1343 // Clear the proximity sensor state for next time.
Jeff Brown96307042012-07-27 15:51:34 -07001344 mProximitySensorEnabled = false;
1345 mProximity = PROXIMITY_UNKNOWN;
Jeff Brownec083212013-09-11 20:45:25 -07001346 mPendingProximity = PROXIMITY_UNKNOWN;
Jeff Brown96307042012-07-27 15:51:34 -07001347 mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
1348 mSensorManager.unregisterListener(mProximitySensorListener);
Jeff Brownec083212013-09-11 20:45:25 -07001349 clearPendingProximityDebounceTime(); // release wake lock (must be last)
Jeff Brown96307042012-07-27 15:51:34 -07001350 }
1351 }
1352 }
1353
1354 private void handleProximitySensorEvent(long time, boolean positive) {
Jeff Brownec083212013-09-11 20:45:25 -07001355 if (mProximitySensorEnabled) {
1356 if (mPendingProximity == PROXIMITY_NEGATIVE && !positive) {
1357 return; // no change
1358 }
1359 if (mPendingProximity == PROXIMITY_POSITIVE && positive) {
1360 return; // no change
1361 }
Jeff Brown96307042012-07-27 15:51:34 -07001362
Jeff Brownec083212013-09-11 20:45:25 -07001363 // Only accept a proximity sensor reading if it remains
1364 // stable for the entire debounce delay. We hold a wake lock while
1365 // debouncing the sensor.
1366 mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
1367 if (positive) {
1368 mPendingProximity = PROXIMITY_POSITIVE;
1369 setPendingProximityDebounceTime(
1370 time + PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY); // acquire wake lock
1371 } else {
1372 mPendingProximity = PROXIMITY_NEGATIVE;
1373 setPendingProximityDebounceTime(
1374 time + PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY); // acquire wake lock
1375 }
1376
1377 // Debounce the new sensor reading.
1378 debounceProximitySensor();
Jeff Brown93cbbb22012-10-04 13:18:36 -07001379 }
Jeff Brown96307042012-07-27 15:51:34 -07001380 }
1381
1382 private void debounceProximitySensor() {
Jeff Brownec083212013-09-11 20:45:25 -07001383 if (mProximitySensorEnabled
1384 && mPendingProximity != PROXIMITY_UNKNOWN
1385 && mPendingProximityDebounceTime >= 0) {
Jeff Brown96307042012-07-27 15:51:34 -07001386 final long now = SystemClock.uptimeMillis();
1387 if (mPendingProximityDebounceTime <= now) {
Jeff Brownec083212013-09-11 20:45:25 -07001388 // Sensor reading accepted. Apply the change then release the wake lock.
Jeff Brown96307042012-07-27 15:51:34 -07001389 mProximity = mPendingProximity;
Jeff Brownec083212013-09-11 20:45:25 -07001390 updatePowerState();
1391 clearPendingProximityDebounceTime(); // release wake lock (must be last)
Jeff Brown96307042012-07-27 15:51:34 -07001392 } else {
Jeff Brownec083212013-09-11 20:45:25 -07001393 // Need to wait a little longer.
1394 // Debounce again later. We continue holding a wake lock while waiting.
Jeff Brown96307042012-07-27 15:51:34 -07001395 Message msg = mHandler.obtainMessage(MSG_PROXIMITY_SENSOR_DEBOUNCED);
Jeff Brown96307042012-07-27 15:51:34 -07001396 mHandler.sendMessageAtTime(msg, mPendingProximityDebounceTime);
1397 }
1398 }
1399 }
1400
Jeff Brownec083212013-09-11 20:45:25 -07001401 private void clearPendingProximityDebounceTime() {
1402 if (mPendingProximityDebounceTime >= 0) {
1403 mPendingProximityDebounceTime = -1;
Jeff Brown131206b2014-04-08 17:27:14 -07001404 mCallbacks.releaseSuspendBlocker(); // release wake lock
Jeff Brownec083212013-09-11 20:45:25 -07001405 }
1406 }
1407
1408 private void setPendingProximityDebounceTime(long debounceTime) {
1409 if (mPendingProximityDebounceTime < 0) {
Jeff Brown131206b2014-04-08 17:27:14 -07001410 mCallbacks.acquireSuspendBlocker(); // acquire wake lock
Jeff Brownec083212013-09-11 20:45:25 -07001411 }
1412 mPendingProximityDebounceTime = debounceTime;
1413 }
1414
Jeff Brownec083212013-09-11 20:45:25 -07001415 private void sendOnStateChangedWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001416 mCallbacks.acquireSuspendBlocker();
1417 mHandler.post(mOnStateChangedRunnable);
Jeff Brown96307042012-07-27 15:51:34 -07001418 }
1419
Kenny Guyecc978f2018-03-14 17:30:20 +00001420 private void handleSettingsChange(boolean userSwitch) {
Michael Wrightd8460232018-01-16 18:04:59 +00001421 mPendingScreenBrightnessSetting = getScreenBrightnessSetting();
Kenny Guyecc978f2018-03-14 17:30:20 +00001422 if (userSwitch) {
1423 // Don't treat user switches as user initiated change.
1424 mCurrentScreenBrightnessSetting = mPendingScreenBrightnessSetting;
1425 }
Michael Wrightd8460232018-01-16 18:04:59 +00001426 mPendingAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting();
1427 // We don't bother with a pending variable for VR screen brightness since we just
1428 // immediately adapt to it.
1429 mScreenBrightnessForVr = getScreenBrightnessForVrSetting();
1430 sendUpdatePowerState();
1431 }
1432
1433 private float getAutoBrightnessAdjustmentSetting() {
1434 final float adj = Settings.System.getFloatForUser(mContext.getContentResolver(),
1435 Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.0f, UserHandle.USER_CURRENT);
1436 return Float.isNaN(adj) ? 0.0f : clampAutoBrightnessAdjustment(adj);
1437 }
1438
1439 private int getScreenBrightnessSetting() {
1440 final int brightness = Settings.System.getIntForUser(mContext.getContentResolver(),
1441 Settings.System.SCREEN_BRIGHTNESS, mScreenBrightnessDefault,
1442 UserHandle.USER_CURRENT);
1443 return clampAbsoluteBrightness(brightness);
1444 }
1445
1446 private int getScreenBrightnessForVrSetting() {
1447 final int brightness = Settings.System.getIntForUser(mContext.getContentResolver(),
1448 Settings.System.SCREEN_BRIGHTNESS_FOR_VR, mScreenBrightnessForVrDefault,
1449 UserHandle.USER_CURRENT);
1450 return clampAbsoluteBrightness(brightness);
1451 }
1452
1453 private void putScreenBrightnessSetting(int brightness) {
1454 mCurrentScreenBrightnessSetting = brightness;
1455 Settings.System.putIntForUser(mContext.getContentResolver(),
Michael Wright617564f2018-01-25 22:20:54 +00001456 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
1457 }
1458
1459 private void putAutoBrightnessAdjustmentSetting(float adjustment) {
1460 mAutoBrightnessAdjustment = adjustment;
1461 Settings.System.putFloatForUser(mContext.getContentResolver(),
1462 Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, adjustment, UserHandle.USER_CURRENT);
Michael Wrightd8460232018-01-16 18:04:59 +00001463 }
1464
1465 private boolean updateAutoBrightnessAdjustment() {
1466 if (Float.isNaN(mPendingAutoBrightnessAdjustment)) {
1467 return false;
1468 }
1469 if (mAutoBrightnessAdjustment == mPendingAutoBrightnessAdjustment) {
1470 return false;
1471 }
1472 mAutoBrightnessAdjustment = mPendingAutoBrightnessAdjustment;
1473 mPendingAutoBrightnessAdjustment = Float.NaN;
1474 return true;
1475 }
1476
1477 private boolean updateUserSetScreenBrightness() {
1478 if (mPendingScreenBrightnessSetting < 0) {
1479 return false;
1480 }
1481 if (mCurrentScreenBrightnessSetting == mPendingScreenBrightnessSetting) {
Michael Wright44dc8b12018-01-25 23:25:20 +00001482 mPendingScreenBrightnessSetting = -1;
Michael Wrightd8460232018-01-16 18:04:59 +00001483 return false;
1484 }
Kenny Guyad9a6ea2018-01-31 15:57:15 +00001485 mCurrentScreenBrightnessSetting = mPendingScreenBrightnessSetting;
Michael Wrightd8460232018-01-16 18:04:59 +00001486 mLastUserSetScreenBrightness = mPendingScreenBrightnessSetting;
1487 mPendingScreenBrightnessSetting = -1;
1488 return true;
1489 }
1490
Kenny Guy53d06612018-01-30 14:19:13 +00001491 private void notifyBrightnessChanged(int brightness, boolean userInitiated,
1492 boolean hadUserDataPoint) {
Michael Wrightd8460232018-01-16 18:04:59 +00001493 final float brightnessInNits = convertToNits(brightness);
Kenny Guyad9a6ea2018-01-31 15:57:15 +00001494 if (mPowerRequest.useAutoBrightness && brightnessInNits >= 0.0f
1495 && mAutomaticBrightnessController != null) {
Michael Wrightd8460232018-01-16 18:04:59 +00001496 // We only want to track changes on devices that can actually map the display backlight
1497 // values into a physical brightness unit since the value provided by the API is in
1498 // nits and not using the arbitrary backlight units.
Kenny Guy53d06612018-01-30 14:19:13 +00001499 final float powerFactor = mPowerRequest.lowPowerMode
1500 ? mPowerRequest.screenLowPowerBrightnessFactor
1501 : 1.0f;
1502 mBrightnessTracker.notifyBrightnessChanged(brightnessInNits, userInitiated,
1503 powerFactor, hadUserDataPoint,
1504 mAutomaticBrightnessController.isDefaultConfig());
Michael Wrightd8460232018-01-16 18:04:59 +00001505 }
1506 }
1507
1508 private float convertToNits(int backlight) {
Michael Wright144aac92017-12-21 18:37:41 +00001509 if (mBrightnessMapper != null) {
Michael Wrightd8460232018-01-16 18:04:59 +00001510 return mBrightnessMapper.convertToNits(backlight);
Michael Wright144aac92017-12-21 18:37:41 +00001511 } else {
1512 return -1.0f;
1513 }
1514 }
1515
Jeff Brown96307042012-07-27 15:51:34 -07001516 private final Runnable mOnStateChangedRunnable = new Runnable() {
1517 @Override
1518 public void run() {
1519 mCallbacks.onStateChanged();
Jeff Brown131206b2014-04-08 17:27:14 -07001520 mCallbacks.releaseSuspendBlocker();
Jeff Brown96307042012-07-27 15:51:34 -07001521 }
1522 };
1523
Jeff Brownec083212013-09-11 20:45:25 -07001524 private void sendOnProximityPositiveWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001525 mCallbacks.acquireSuspendBlocker();
1526 mHandler.post(mOnProximityPositiveRunnable);
Jeff Brown93cbbb22012-10-04 13:18:36 -07001527 }
1528
1529 private final Runnable mOnProximityPositiveRunnable = new Runnable() {
1530 @Override
1531 public void run() {
1532 mCallbacks.onProximityPositive();
Jeff Brown131206b2014-04-08 17:27:14 -07001533 mCallbacks.releaseSuspendBlocker();
Jeff Brown93cbbb22012-10-04 13:18:36 -07001534 }
1535 };
1536
Jeff Brownec083212013-09-11 20:45:25 -07001537 private void sendOnProximityNegativeWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001538 mCallbacks.acquireSuspendBlocker();
1539 mHandler.post(mOnProximityNegativeRunnable);
Jeff Brown96307042012-07-27 15:51:34 -07001540 }
1541
1542 private final Runnable mOnProximityNegativeRunnable = new Runnable() {
1543 @Override
1544 public void run() {
1545 mCallbacks.onProximityNegative();
Jeff Brown131206b2014-04-08 17:27:14 -07001546 mCallbacks.releaseSuspendBlocker();
Jeff Brown96307042012-07-27 15:51:34 -07001547 }
1548 };
1549
Jeff Brownbd6e1502012-08-28 03:27:37 -07001550 public void dump(final PrintWriter pw) {
Jeff Brown96307042012-07-27 15:51:34 -07001551 synchronized (mLock) {
1552 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001553 pw.println("Display Power Controller Locked State:");
Jeff Brown96307042012-07-27 15:51:34 -07001554 pw.println(" mDisplayReadyLocked=" + mDisplayReadyLocked);
1555 pw.println(" mPendingRequestLocked=" + mPendingRequestLocked);
1556 pw.println(" mPendingRequestChangedLocked=" + mPendingRequestChangedLocked);
1557 pw.println(" mPendingWaitForNegativeProximityLocked="
1558 + mPendingWaitForNegativeProximityLocked);
1559 pw.println(" mPendingUpdatePowerStateLocked=" + mPendingUpdatePowerStateLocked);
1560 }
1561
1562 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001563 pw.println("Display Power Controller Configuration:");
Jeff Brown26875502014-01-30 21:47:47 -08001564 pw.println(" mScreenBrightnessDozeConfig=" + mScreenBrightnessDozeConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001565 pw.println(" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig);
Jeff Brownb76eebff2012-10-05 22:26:44 -07001566 pw.println(" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum);
1567 pw.println(" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum);
Jeff Brown2175e9c2014-09-12 16:11:07 -07001568 pw.println(" mUseSoftwareAutoBrightnessConfig=" + mUseSoftwareAutoBrightnessConfig);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -07001569 pw.println(" mAllowAutoBrightnessWhileDozingConfig=" +
1570 mAllowAutoBrightnessWhileDozingConfig);
Jeff Brown2175e9c2014-09-12 16:11:07 -07001571 pw.println(" mColorFadeFadesConfig=" + mColorFadeFadesConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001572
Jeff Brownbd6e1502012-08-28 03:27:37 -07001573 mHandler.runWithScissors(new Runnable() {
1574 @Override
1575 public void run() {
1576 dumpLocal(pw);
Jeff Brown96307042012-07-27 15:51:34 -07001577 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001578 }, 1000);
Jeff Brown96307042012-07-27 15:51:34 -07001579 }
1580
1581 private void dumpLocal(PrintWriter pw) {
1582 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001583 pw.println("Display Power Controller Thread State:");
Jeff Brown96307042012-07-27 15:51:34 -07001584 pw.println(" mPowerRequest=" + mPowerRequest);
1585 pw.println(" mWaitingForNegativeProximity=" + mWaitingForNegativeProximity);
Jeff Brown96307042012-07-27 15:51:34 -07001586 pw.println(" mProximitySensor=" + mProximitySensor);
1587 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1588 pw.println(" mProximityThreshold=" + mProximityThreshold);
1589 pw.println(" mProximity=" + proximityToString(mProximity));
1590 pw.println(" mPendingProximity=" + proximityToString(mPendingProximity));
1591 pw.println(" mPendingProximityDebounceTime="
1592 + TimeUtils.formatUptime(mPendingProximityDebounceTime));
1593 pw.println(" mScreenOffBecauseOfProximity=" + mScreenOffBecauseOfProximity);
Michael Wrightd8460232018-01-16 18:04:59 +00001594 pw.println(" mLastUserSetScreenBrightness=" + mLastUserSetScreenBrightness);
1595 pw.println(" mCurrentScreenBrightnessSetting=" + mCurrentScreenBrightnessSetting);
1596 pw.println(" mPendingScreenBrightnessSetting=" + mPendingScreenBrightnessSetting);
1597 pw.println(" mAutoBrightnessAdjustment=" + mAutoBrightnessAdjustment);
1598 pw.println(" mPendingAutoBrightnessAdjustment=" + mPendingAutoBrightnessAdjustment);
Jeff Brown970d4132014-07-19 11:33:47 -07001599 pw.println(" mAppliedAutoBrightness=" + mAppliedAutoBrightness);
1600 pw.println(" mAppliedDimming=" + mAppliedDimming);
1601 pw.println(" mAppliedLowPower=" + mAppliedLowPower);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001602 pw.println(" mPendingScreenOnUnblocker=" + mPendingScreenOnUnblocker);
1603 pw.println(" mPendingScreenOff=" + mPendingScreenOff);
Michael Wrightc3e6af82017-07-25 22:31:03 +01001604 pw.println(" mReportedToPolicy=" +
1605 reportedToPolicyToString(mReportedScreenStateToPolicy));
Jeff Brown96307042012-07-27 15:51:34 -07001606
Jeff Brown42558692014-05-20 22:02:46 -07001607 pw.println(" mScreenBrightnessRampAnimator.isAnimating()=" +
1608 mScreenBrightnessRampAnimator.isAnimating());
1609
Michael Lentine0839adb2014-07-29 18:47:56 -07001610 if (mColorFadeOnAnimator != null) {
1611 pw.println(" mColorFadeOnAnimator.isStarted()=" +
1612 mColorFadeOnAnimator.isStarted());
Jeff Brown96307042012-07-27 15:51:34 -07001613 }
Michael Lentine0839adb2014-07-29 18:47:56 -07001614 if (mColorFadeOffAnimator != null) {
1615 pw.println(" mColorFadeOffAnimator.isStarted()=" +
1616 mColorFadeOffAnimator.isStarted());
Jeff Brown96307042012-07-27 15:51:34 -07001617 }
1618
1619 if (mPowerState != null) {
1620 mPowerState.dump(pw);
1621 }
Michael Wright639c8be2014-01-17 18:29:12 -08001622
1623 if (mAutomaticBrightnessController != null) {
1624 mAutomaticBrightnessController.dump(pw);
1625 }
1626
Michael Wright144aac92017-12-21 18:37:41 +00001627 if (mBrightnessTracker != null) {
1628 pw.println();
1629 mBrightnessTracker.dump(pw);
1630 }
Jeff Brown96307042012-07-27 15:51:34 -07001631 }
1632
1633 private static String proximityToString(int state) {
1634 switch (state) {
1635 case PROXIMITY_UNKNOWN:
1636 return "Unknown";
1637 case PROXIMITY_NEGATIVE:
1638 return "Negative";
1639 case PROXIMITY_POSITIVE:
1640 return "Positive";
1641 default:
1642 return Integer.toString(state);
1643 }
1644 }
1645
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001646 private static String reportedToPolicyToString(int state) {
1647 switch (state) {
1648 case REPORTED_TO_POLICY_SCREEN_OFF:
1649 return "REPORTED_TO_POLICY_SCREEN_OFF";
1650 case REPORTED_TO_POLICY_SCREEN_TURNING_ON:
1651 return "REPORTED_TO_POLICY_SCREEN_TURNING_ON";
1652 case REPORTED_TO_POLICY_SCREEN_ON:
1653 return "REPORTED_TO_POLICY_SCREEN_ON";
1654 default:
1655 return Integer.toString(state);
1656 }
1657 }
1658
Michael Wright639c8be2014-01-17 18:29:12 -08001659 private static int clampAbsoluteBrightness(int value) {
1660 return MathUtils.constrain(value, PowerManager.BRIGHTNESS_OFF, PowerManager.BRIGHTNESS_ON);
Jeff Brown96307042012-07-27 15:51:34 -07001661 }
1662
Michael Wrightd8460232018-01-16 18:04:59 +00001663 private static float clampAutoBrightnessAdjustment(float value) {
1664 return MathUtils.constrain(value, -1.0f, 1.0f);
1665 }
1666
Jeff Brown96307042012-07-27 15:51:34 -07001667 private final class DisplayControllerHandler extends Handler {
1668 public DisplayControllerHandler(Looper looper) {
Jeff Browna2910d02012-08-25 12:29:46 -07001669 super(looper, null, true /*async*/);
Jeff Brown96307042012-07-27 15:51:34 -07001670 }
1671
1672 @Override
1673 public void handleMessage(Message msg) {
1674 switch (msg.what) {
1675 case MSG_UPDATE_POWER_STATE:
1676 updatePowerState();
1677 break;
1678
1679 case MSG_PROXIMITY_SENSOR_DEBOUNCED:
1680 debounceProximitySensor();
1681 break;
Jeff Brown3ee549c2014-09-22 20:14:39 -07001682
1683 case MSG_SCREEN_ON_UNBLOCKED:
1684 if (mPendingScreenOnUnblocker == msg.obj) {
1685 unblockScreenOn();
1686 updatePowerState();
1687 }
1688 break;
Jorim Jaggi51304d72017-05-17 17:25:32 +02001689 case MSG_SCREEN_OFF_UNBLOCKED:
1690 if (mPendingScreenOffUnblocker == msg.obj) {
1691 unblockScreenOff();
1692 updatePowerState();
1693 }
1694 break;
Michael Wrighteef0e132017-11-21 17:57:52 +00001695 case MSG_CONFIGURE_BRIGHTNESS:
Michael Wrightd5df3612018-01-02 12:44:52 +00001696 mBrightnessConfiguration = (BrightnessConfiguration)msg.obj;
Michael Wrighteef0e132017-11-21 17:57:52 +00001697 updatePowerState();
1698 break;
Michael Wrightd8460232018-01-16 18:04:59 +00001699
1700 case MSG_SET_TEMPORARY_BRIGHTNESS:
1701 // TODO: Should we have a a timeout for the temporary brightness?
1702 mTemporaryScreenBrightness = msg.arg1;
1703 updatePowerState();
1704 break;
1705
1706 case MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT:
1707 mTemporaryAutoBrightnessAdjustment = Float.intBitsToFloat(msg.arg1);
1708 updatePowerState();
1709 break;
Jeff Brown96307042012-07-27 15:51:34 -07001710 }
1711 }
1712 }
1713
1714 private final SensorEventListener mProximitySensorListener = new SensorEventListener() {
1715 @Override
1716 public void onSensorChanged(SensorEvent event) {
1717 if (mProximitySensorEnabled) {
1718 final long time = SystemClock.uptimeMillis();
1719 final float distance = event.values[0];
1720 boolean positive = distance >= 0.0f && distance < mProximityThreshold;
1721 handleProximitySensorEvent(time, positive);
1722 }
1723 }
1724
1725 @Override
1726 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1727 // Not used.
1728 }
1729 };
Jeff Brown3ee549c2014-09-22 20:14:39 -07001730
Michael Wrightd8460232018-01-16 18:04:59 +00001731
1732 private final class SettingsObserver extends ContentObserver {
1733 public SettingsObserver(Handler handler) {
1734 super(handler);
1735 }
1736
1737 @Override
1738 public void onChange(boolean selfChange, Uri uri) {
Kenny Guyecc978f2018-03-14 17:30:20 +00001739 handleSettingsChange(false /* userSwitch */);
Michael Wrightd8460232018-01-16 18:04:59 +00001740 }
1741 }
1742
Jeff Brown3ee549c2014-09-22 20:14:39 -07001743 private final class ScreenOnUnblocker implements WindowManagerPolicy.ScreenOnListener {
1744 @Override
1745 public void onScreenOn() {
1746 Message msg = mHandler.obtainMessage(MSG_SCREEN_ON_UNBLOCKED, this);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001747 mHandler.sendMessage(msg);
1748 }
1749 }
Jorim Jaggi51304d72017-05-17 17:25:32 +02001750
1751 private final class ScreenOffUnblocker implements WindowManagerPolicy.ScreenOffListener {
Jorim Jaggi51304d72017-05-17 17:25:32 +02001752 @Override
1753 public void onScreenOff() {
1754 Message msg = mHandler.obtainMessage(MSG_SCREEN_OFF_UNBLOCKED, this);
Jorim Jaggi51304d72017-05-17 17:25:32 +02001755 mHandler.sendMessage(msg);
1756 }
1757 }
Jeff Brown96307042012-07-27 15:51:34 -07001758}