blob: b79ead05c0747503041a63aaf9af5d79aabb9e54 [file] [log] [blame]
Jeff Brown96307042012-07-27 15:51:34 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown131206b2014-04-08 17:27:14 -070017package com.android.server.display;
Jeff Brown96307042012-07-27 15:51:34 -070018
Jeff Brown96307042012-07-27 15:51:34 -070019import android.animation.Animator;
20import android.animation.ObjectAnimator;
Michael Wright144aac92017-12-21 18:37:41 +000021import android.annotation.Nullable;
22import android.annotation.UserIdInt;
Dan Gittiked958e92018-11-13 14:58:20 +000023import android.app.ActivityManager;
Jeff Brown96307042012-07-27 15:51:34 -070024import android.content.Context;
Michael Wright144aac92017-12-21 18:37:41 +000025import android.content.pm.ParceledListSlice;
Jeff Brown96307042012-07-27 15:51:34 -070026import android.content.res.Resources;
Michael Wrightd8460232018-01-16 18:04:59 +000027import android.database.ContentObserver;
Jeff Brown96307042012-07-27 15:51:34 -070028import android.hardware.Sensor;
29import android.hardware.SensorEvent;
30import android.hardware.SensorEventListener;
31import android.hardware.SensorManager;
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +000032import android.hardware.display.AmbientBrightnessDayStats;
Michael Wright144aac92017-12-21 18:37:41 +000033import android.hardware.display.BrightnessChangeEvent;
Michael Wrighteef0e132017-11-21 17:57:52 +000034import android.hardware.display.BrightnessConfiguration;
Jeff Brown131206b2014-04-08 17:27:14 -070035import android.hardware.display.DisplayManagerInternal.DisplayPowerCallbacks;
36import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest;
Michael Wrightd8460232018-01-16 18:04:59 +000037import android.net.Uri;
Jeff Brown96307042012-07-27 15:51:34 -070038import android.os.Handler;
39import android.os.Looper;
40import android.os.Message;
Jeff Brown330560f2012-08-21 22:10:57 -070041import android.os.PowerManager;
Jeff Brown131206b2014-04-08 17:27:14 -070042import android.os.RemoteException;
Jeff Brown96307042012-07-27 15:51:34 -070043import android.os.SystemClock;
Jeff Brown3edf5272014-08-14 19:25:14 -070044import android.os.Trace;
Michael Wrightd8460232018-01-16 18:04:59 +000045import android.os.UserHandle;
46import android.provider.Settings;
Michael Wrighte02db662019-03-08 21:24:36 +000047import android.text.TextUtils;
Michael Wright41a5cdf2013-11-13 16:18:32 -080048import android.util.MathUtils;
Jeff Brown96307042012-07-27 15:51:34 -070049import android.util.Slog;
50import android.util.TimeUtils;
Jeff Brown037c33e2014-04-09 00:31:55 -070051import android.view.Display;
Jeff Brown96307042012-07-27 15:51:34 -070052
Dan Gittiked958e92018-11-13 14:58:20 +000053import com.android.internal.app.IBatteryStats;
54import com.android.server.LocalServices;
55import com.android.server.am.BatteryStatsService;
Dan Gittik8dbd7e92018-12-03 15:35:53 +000056import com.android.server.display.whitebalance.DisplayWhiteBalanceController;
57import com.android.server.display.whitebalance.DisplayWhiteBalanceFactory;
58import com.android.server.display.whitebalance.DisplayWhiteBalanceSettings;
Dan Gittiked958e92018-11-13 14:58:20 +000059import com.android.server.policy.WindowManagerPolicy;
60
Jeff Brown96307042012-07-27 15:51:34 -070061import java.io.PrintWriter;
Michael Wrighte02db662019-03-08 21:24:36 +000062import java.util.List;
Jeff Brown96307042012-07-27 15:51:34 -070063
64/**
65 * Controls the power state of the display.
66 *
67 * Handles the proximity sensor, light sensor, and animations between states
68 * including the screen off animation.
69 *
70 * This component acts independently of the rest of the power manager service.
71 * In particular, it does not share any state and it only communicates
72 * via asynchronous callbacks to inform the power manager that something has
73 * changed.
74 *
75 * Everything this class does internally is serialized on its handler although
76 * it may be accessed by other threads from the outside.
77 *
78 * Note that the power manager service guarantees that it will hold a suspend
79 * blocker as long as the display is not ready. So most of the work done here
80 * does not need to worry about holding a suspend blocker unless it happens
81 * independently of the display ready signal.
Michael Lentine0839adb2014-07-29 18:47:56 -070082 *
Jeff Brown3ee549c2014-09-22 20:14:39 -070083 * For debugging, you can make the color fade and brightness animations run
Jeff Brown96307042012-07-27 15:51:34 -070084 * slower by changing the "animator duration scale" option in Development Settings.
85 */
Dan Gittik8dbd7e92018-12-03 15:35:53 +000086final class DisplayPowerController implements AutomaticBrightnessController.Callbacks,
87 DisplayWhiteBalanceController.Callbacks {
Jeff Brown96307042012-07-27 15:51:34 -070088 private static final String TAG = "DisplayPowerController";
Jeff Browna576b4d2015-04-23 19:58:06 -070089 private static final String SCREEN_ON_BLOCKED_TRACE_NAME = "Screen on blocked";
Jorim Jaggi51304d72017-05-17 17:25:32 +020090 private static final String SCREEN_OFF_BLOCKED_TRACE_NAME = "Screen off blocked";
Jeff Brown96307042012-07-27 15:51:34 -070091
Julius D'souza428aed02016-08-07 19:08:30 -070092 private static final boolean DEBUG = false;
Jeff Brown96307042012-07-27 15:51:34 -070093 private static final boolean DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT = false;
Jeff Brown96307042012-07-27 15:51:34 -070094
Jeff Brown3ee549c2014-09-22 20:14:39 -070095 // If true, uses the color fade on animation.
Jeff Brown13c589b2012-08-16 16:20:54 -070096 // We might want to turn this off if we cannot get a guarantee that the screen
97 // actually turns on and starts showing new content after the call to set the
Jeff Brown5356c7dc2012-08-20 20:17:36 -070098 // screen state returns. Playing the animation can also be somewhat slow.
Michael Lentine0839adb2014-07-29 18:47:56 -070099 private static final boolean USE_COLOR_FADE_ON_ANIMATION = false;
Jeff Brown13c589b2012-08-16 16:20:54 -0700100
Jeff Brownb76eebff2012-10-05 22:26:44 -0700101 // The minimum reduction in brightness when dimmed.
102 private static final int SCREEN_DIM_MINIMUM_REDUCTION = 10;
103
Michael Lentine0839adb2014-07-29 18:47:56 -0700104 private static final int COLOR_FADE_ON_ANIMATION_DURATION_MILLIS = 250;
Michael Lentine0c9a62d2014-08-20 09:19:07 -0700105 private static final int COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS = 400;
Jeff Brown96307042012-07-27 15:51:34 -0700106
107 private static final int MSG_UPDATE_POWER_STATE = 1;
108 private static final int MSG_PROXIMITY_SENSOR_DEBOUNCED = 2;
Jeff Brown3ee549c2014-09-22 20:14:39 -0700109 private static final int MSG_SCREEN_ON_UNBLOCKED = 3;
Jorim Jaggi51304d72017-05-17 17:25:32 +0200110 private static final int MSG_SCREEN_OFF_UNBLOCKED = 4;
Michael Wrighteef0e132017-11-21 17:57:52 +0000111 private static final int MSG_CONFIGURE_BRIGHTNESS = 5;
Michael Wrightd8460232018-01-16 18:04:59 +0000112 private static final int MSG_SET_TEMPORARY_BRIGHTNESS = 6;
113 private static final int MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT = 7;
Jeff Brown96307042012-07-27 15:51:34 -0700114
115 private static final int PROXIMITY_UNKNOWN = -1;
116 private static final int PROXIMITY_NEGATIVE = 0;
117 private static final int PROXIMITY_POSITIVE = 1;
118
Jeff Brown93cbbb22012-10-04 13:18:36 -0700119 // Proximity sensor debounce delay in milliseconds for positive or negative transitions.
120 private static final int PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY = 0;
Jeff Brownec083212013-09-11 20:45:25 -0700121 private static final int PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY = 250;
Jeff Brown96307042012-07-27 15:51:34 -0700122
123 // Trigger proximity if distance is less than 5 cm.
124 private static final float TYPICAL_PROXIMITY_THRESHOLD = 5.0f;
125
Julius D'souzad5105dd2017-06-02 11:03:53 -0700126 // State machine constants for tracking initial brightness ramp skipping when enabled.
127 private static final int RAMP_STATE_SKIP_NONE = 0;
128 private static final int RAMP_STATE_SKIP_INITIAL = 1;
129 private static final int RAMP_STATE_SKIP_AUTOBRIGHT = 2;
130
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700131 private static final int REPORTED_TO_POLICY_SCREEN_OFF = 0;
132 private static final int REPORTED_TO_POLICY_SCREEN_TURNING_ON = 1;
133 private static final int REPORTED_TO_POLICY_SCREEN_ON = 2;
Jorim Jaggi51304d72017-05-17 17:25:32 +0200134 private static final int REPORTED_TO_POLICY_SCREEN_TURNING_OFF = 3;
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700135
Jeff Brown96307042012-07-27 15:51:34 -0700136 private final Object mLock = new Object();
137
Michael Lentine0839adb2014-07-29 18:47:56 -0700138 private final Context mContext;
139
Jeff Brown96307042012-07-27 15:51:34 -0700140 // Our handler.
141 private final DisplayControllerHandler mHandler;
142
143 // Asynchronous callbacks into the power manager service.
144 // Only invoked from the handler thread while no locks are held.
Jeff Brown131206b2014-04-08 17:27:14 -0700145 private final DisplayPowerCallbacks mCallbacks;
146
147 // Battery stats.
148 private final IBatteryStats mBatteryStats;
Jeff Brown96307042012-07-27 15:51:34 -0700149
Jeff Brown96307042012-07-27 15:51:34 -0700150 // The sensor manager.
151 private final SensorManager mSensorManager;
152
Jeff Brown3ee549c2014-09-22 20:14:39 -0700153 // The window manager policy.
154 private final WindowManagerPolicy mWindowManagerPolicy;
155
Jeff Brown037c33e2014-04-09 00:31:55 -0700156 // The display blanker.
157 private final DisplayBlanker mBlanker;
158
Michael Wrightd8460232018-01-16 18:04:59 +0000159 // Tracker for brightness changes.
160 private final BrightnessTracker mBrightnessTracker;
161
162 // Tracker for brightness settings changes.
163 private final SettingsObserver mSettingsObserver;
164
Jeff Brown96307042012-07-27 15:51:34 -0700165 // The proximity sensor, or null if not available or needed.
166 private Sensor mProximitySensor;
167
Jeff Brown26875502014-01-30 21:47:47 -0800168 // The doze screen brightness.
169 private final int mScreenBrightnessDozeConfig;
170
Jeff Brown96307042012-07-27 15:51:34 -0700171 // The dim screen brightness.
172 private final int mScreenBrightnessDimConfig;
173
Jeff Brownb76eebff2012-10-05 22:26:44 -0700174 // The minimum allowed brightness.
175 private final int mScreenBrightnessRangeMinimum;
176
177 // The maximum allowed brightness.
178 private final int mScreenBrightnessRangeMaximum;
179
Michael Wrightd8460232018-01-16 18:04:59 +0000180 // The default screen brightness.
181 private final int mScreenBrightnessDefault;
182
Santos Cordonb12c7e12018-04-13 17:10:54 -0700183 // The minimum allowed brightness while in VR.
184 private final int mScreenBrightnessForVrRangeMinimum;
185
186 // The maximum allowed brightness while in VR.
187 private final int mScreenBrightnessForVrRangeMaximum;
188
Michael Wrightd8460232018-01-16 18:04:59 +0000189 // The default screen brightness for VR.
190 private final int mScreenBrightnessForVrDefault;
191
Jeff Brown330560f2012-08-21 22:10:57 -0700192 // True if auto-brightness should be used.
Jeff Brown96307042012-07-27 15:51:34 -0700193 private boolean mUseSoftwareAutoBrightnessConfig;
Jeff Brown330560f2012-08-21 22:10:57 -0700194
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700195 // True if should use light sensor to automatically determine doze screen brightness.
196 private final boolean mAllowAutoBrightnessWhileDozingConfig;
197
Narayan Kamath38819982017-07-06 18:15:30 +0100198 // Whether or not the color fade on screen on / off is enabled.
199 private final boolean mColorFadeEnabled;
200
Jeff Brown252c2062012-10-08 16:21:01 -0700201 // True if we should fade the screen while turning it off, false if we should play
Jeff Brown3ee549c2014-09-22 20:14:39 -0700202 // a stylish color fade animation instead.
Michael Lentine0839adb2014-07-29 18:47:56 -0700203 private boolean mColorFadeFadesConfig;
Jeff Browna52772f2012-10-04 18:38:09 -0700204
Michael Wrightc3e6af82017-07-25 22:31:03 +0100205 // True if we need to fake a transition to off when coming out of a doze state.
206 // Some display hardware will blank itself when coming out of doze in order to hide
207 // artifacts. For these displays we fake a transition into OFF so that policy can appropriately
208 // blank itself and begin an appropriate power on animation.
209 private boolean mDisplayBlanksAfterDozeConfig;
Michael Wright05e76fe2017-07-20 18:18:33 +0100210
Michael Wright63a40062017-07-26 01:10:59 +0100211 // True if there are only buckets of brightness values when the display is in the doze state,
212 // rather than a full range of values. If this is true, then we'll avoid animating the screen
213 // brightness since it'd likely be multiple jarring brightness transitions instead of just one
214 // to reach the final state.
215 private boolean mBrightnessBucketsInDozeConfig;
216
Jeff Brown96307042012-07-27 15:51:34 -0700217 // The pending power request.
218 // Initially null until the first call to requestPowerState.
219 // Guarded by mLock.
220 private DisplayPowerRequest mPendingRequestLocked;
221
222 // True if a request has been made to wait for the proximity sensor to go negative.
223 // Guarded by mLock.
224 private boolean mPendingWaitForNegativeProximityLocked;
225
226 // True if the pending power request or wait for negative proximity flag
227 // has been changed since the last update occurred.
228 // Guarded by mLock.
229 private boolean mPendingRequestChangedLocked;
230
231 // Set to true when the important parts of the pending power request have been applied.
232 // The important parts are mainly the screen state. Brightness changes may occur
233 // concurrently.
234 // Guarded by mLock.
235 private boolean mDisplayReadyLocked;
236
237 // Set to true if a power state update is required.
238 // Guarded by mLock.
239 private boolean mPendingUpdatePowerStateLocked;
240
241 /* The following state must only be accessed by the handler thread. */
242
243 // The currently requested power state.
244 // The power controller will progressively update its internal state to match
245 // the requested power state. Initially null until the first update.
246 private DisplayPowerRequest mPowerRequest;
247
248 // The current power state.
249 // Must only be accessed on the handler thread.
250 private DisplayPowerState mPowerState;
251
252 // True if the device should wait for negative proximity sensor before
253 // waking up the screen. This is set to false as soon as a negative
254 // proximity sensor measurement is observed or when the device is forced to
255 // go to sleep by the user. While true, the screen remains off.
256 private boolean mWaitingForNegativeProximity;
257
258 // The actual proximity sensor threshold value.
259 private float mProximityThreshold;
260
261 // Set to true if the proximity sensor listener has been registered
262 // with the sensor manager.
263 private boolean mProximitySensorEnabled;
264
265 // The debounced proximity sensor state.
266 private int mProximity = PROXIMITY_UNKNOWN;
267
268 // The raw non-debounced proximity sensor state.
269 private int mPendingProximity = PROXIMITY_UNKNOWN;
Jeff Brownec083212013-09-11 20:45:25 -0700270 private long mPendingProximityDebounceTime = -1; // -1 if fully debounced
Jeff Brown96307042012-07-27 15:51:34 -0700271
272 // True if the screen was turned off because of the proximity sensor.
273 // When the screen turns on again, we report user activity to the power manager.
274 private boolean mScreenOffBecauseOfProximity;
275
Jeff Brown3ee549c2014-09-22 20:14:39 -0700276 // The currently active screen on unblocker. This field is non-null whenever
277 // we are waiting for a callback to release it and unblock the screen.
278 private ScreenOnUnblocker mPendingScreenOnUnblocker;
Jorim Jaggi51304d72017-05-17 17:25:32 +0200279 private ScreenOffUnblocker mPendingScreenOffUnblocker;
Jeff Brown3ee549c2014-09-22 20:14:39 -0700280
281 // True if we were in the process of turning off the screen.
282 // This allows us to recover more gracefully from situations where we abort
283 // turning off the screen.
284 private boolean mPendingScreenOff;
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700285
Jeff Brown0a434772014-09-30 14:42:27 -0700286 // True if we have unfinished business and are holding a suspend blocker.
287 private boolean mUnfinishedBusiness;
288
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700289 // The elapsed real time when the screen on was blocked.
290 private long mScreenOnBlockStartRealTime;
Jorim Jaggi51304d72017-05-17 17:25:32 +0200291 private long mScreenOffBlockStartRealTime;
Jeff Brown8b9cf1c2012-10-07 14:54:17 -0700292
Jorim Jaggi0d210f62015-07-10 14:24:44 -0700293 // Screen state we reported to policy. Must be one of REPORTED_TO_POLICY_SCREEN_* fields.
294 private int mReportedScreenStateToPolicy;
Jeff Browne1633ad2015-06-22 19:24:24 -0700295
Julius D'souzad5105dd2017-06-02 11:03:53 -0700296 // If the last recorded screen state was dozing or not.
297 private boolean mDozing;
298
Jeff Brown970d4132014-07-19 11:33:47 -0700299 // Remembers whether certain kinds of brightness adjustments
300 // were recently applied so that we can decide how to transition.
301 private boolean mAppliedAutoBrightness;
302 private boolean mAppliedDimming;
303 private boolean mAppliedLowPower;
Michael Wright2155bc22018-05-01 00:38:32 +0100304 private boolean mAppliedScreenBrightnessOverride;
305 private boolean mAppliedTemporaryBrightness;
306 private boolean mAppliedTemporaryAutoBrightnessAdjustment;
307 private boolean mAppliedBrightnessBoost;
Jeff Brown96307042012-07-27 15:51:34 -0700308
Julius D'souzafeadad12016-08-05 14:34:38 -0700309 // Brightness animation ramp rates in brightness units per second
Prashant Malani99e6d432016-03-29 16:32:37 -0700310 private final int mBrightnessRampRateFast;
Julius D'souzafeadad12016-08-05 14:34:38 -0700311 private final int mBrightnessRampRateSlow;
Prashant Malani99e6d432016-03-29 16:32:37 -0700312
Julius D'souzad5105dd2017-06-02 11:03:53 -0700313 // Whether or not to skip the initial brightness ramps into STATE_ON.
314 private final boolean mSkipScreenOnBrightnessRamp;
315
Dan Gittik8dbd7e92018-12-03 15:35:53 +0000316 // Display white balance components.
317 @Nullable
318 private final DisplayWhiteBalanceSettings mDisplayWhiteBalanceSettings;
319 @Nullable
320 private final DisplayWhiteBalanceController mDisplayWhiteBalanceController;
321
Julius D'souzad5105dd2017-06-02 11:03:53 -0700322 // A record of state for skipping brightness ramps.
323 private int mSkipRampState = RAMP_STATE_SKIP_NONE;
324
325 // The first autobrightness value set when entering RAMP_STATE_SKIP_INITIAL.
326 private int mInitialAutoBrightness;
327
Michael Wright639c8be2014-01-17 18:29:12 -0800328 // The controller for the automatic brightness level.
329 private AutomaticBrightnessController mAutomaticBrightnessController;
330
Michael Wright144aac92017-12-21 18:37:41 +0000331 // The mapper between ambient lux, display backlight values, and display brightness.
332 @Nullable
333 private BrightnessMappingStrategy mBrightnessMapper;
334
Michael Wrighteef0e132017-11-21 17:57:52 +0000335 // The current brightness configuration.
Michael Wrightd5df3612018-01-02 12:44:52 +0000336 @Nullable
Michael Wrighteef0e132017-11-21 17:57:52 +0000337 private BrightnessConfiguration mBrightnessConfiguration;
338
Michael Wrightd5df3612018-01-02 12:44:52 +0000339 // The last brightness that was set by the user and not temporary. Set to -1 when a brightness
340 // has yet to be recorded.
Michael Wrightd8460232018-01-16 18:04:59 +0000341 private int mLastUserSetScreenBrightness;
342
343 // The screen brightenss setting has changed but not taken effect yet. If this is different
344 // from the current screen brightness setting then this is coming from something other than us
345 // and should be considered a user interaction.
346 private int mPendingScreenBrightnessSetting;
347
348 // The last observed screen brightness setting, either set by us or by the settings app on
349 // behalf of the user.
350 private int mCurrentScreenBrightnessSetting;
351
352 // The temporary screen brightness. Typically set when a user is interacting with the
353 // brightness slider but hasn't settled on a choice yet. Set to -1 when there's no temporary
354 // brightness set.
355 private int mTemporaryScreenBrightness;
356
357 // The current screen brightness while in VR mode.
358 private int mScreenBrightnessForVr;
Michael Wrightd5df3612018-01-02 12:44:52 +0000359
360 // The last auto brightness adjustment that was set by the user and not temporary. Set to
361 // Float.NaN when an auto-brightness adjustment hasn't been recorded yet.
Michael Wrightd8460232018-01-16 18:04:59 +0000362 private float mAutoBrightnessAdjustment;
363
364 // The pending auto brightness adjustment that will take effect on the next power state update.
365 private float mPendingAutoBrightnessAdjustment;
366
367 // The temporary auto brightness adjustment. Typically set when a user is interacting with the
368 // adjustment slider but hasn't settled on a choice yet. Set to Float.NaN when there's no
369 // temporary adjustment set.
370 private float mTemporaryAutoBrightnessAdjustment;
Michael Wrightd5df3612018-01-02 12:44:52 +0000371
Jeff Brown96307042012-07-27 15:51:34 -0700372 // Animators.
Michael Lentine0839adb2014-07-29 18:47:56 -0700373 private ObjectAnimator mColorFadeOnAnimator;
374 private ObjectAnimator mColorFadeOffAnimator;
Jeff Brown96307042012-07-27 15:51:34 -0700375 private RampAnimator<DisplayPowerState> mScreenBrightnessRampAnimator;
376
377 /**
378 * Creates the display power controller.
379 */
Jeff Brown131206b2014-04-08 17:27:14 -0700380 public DisplayPowerController(Context context,
Jeff Brown037c33e2014-04-09 00:31:55 -0700381 DisplayPowerCallbacks callbacks, Handler handler,
382 SensorManager sensorManager, DisplayBlanker blanker) {
Jeff Brown131206b2014-04-08 17:27:14 -0700383 mHandler = new DisplayControllerHandler(handler.getLooper());
Michael Wright144aac92017-12-21 18:37:41 +0000384 mBrightnessTracker = new BrightnessTracker(context, null);
Michael Wrightd8460232018-01-16 18:04:59 +0000385 mSettingsObserver = new SettingsObserver(mHandler);
Jeff Brown96307042012-07-27 15:51:34 -0700386 mCallbacks = callbacks;
Jeff Brown96307042012-07-27 15:51:34 -0700387
Jeff Brown131206b2014-04-08 17:27:14 -0700388 mBatteryStats = BatteryStatsService.getService();
Jeff Brown3b971592013-01-09 18:46:37 -0800389 mSensorManager = sensorManager;
Jeff Brown3ee549c2014-09-22 20:14:39 -0700390 mWindowManagerPolicy = LocalServices.getService(WindowManagerPolicy.class);
Jeff Brown037c33e2014-04-09 00:31:55 -0700391 mBlanker = blanker;
Michael Lentine0839adb2014-07-29 18:47:56 -0700392 mContext = context;
Jeff Brown96307042012-07-27 15:51:34 -0700393
394 final Resources resources = context.getResources();
Jeff Brown1bfd0f42014-08-22 01:59:06 -0700395 final int screenBrightnessSettingMinimum = clampAbsoluteBrightness(resources.getInteger(
396 com.android.internal.R.integer.config_screenBrightnessSettingMinimum));
Jeff Brownb76eebff2012-10-05 22:26:44 -0700397
Jeff Brown26875502014-01-30 21:47:47 -0800398 mScreenBrightnessDozeConfig = clampAbsoluteBrightness(resources.getInteger(
399 com.android.internal.R.integer.config_screenBrightnessDoze));
400
Jeff Brownb76eebff2012-10-05 22:26:44 -0700401 mScreenBrightnessDimConfig = clampAbsoluteBrightness(resources.getInteger(
402 com.android.internal.R.integer.config_screenBrightnessDim));
403
Michael Wright144aac92017-12-21 18:37:41 +0000404 mScreenBrightnessRangeMinimum =
405 Math.min(screenBrightnessSettingMinimum, mScreenBrightnessDimConfig);
Michael Wright639c8be2014-01-17 18:29:12 -0800406
Michael Wrighteef0e132017-11-21 17:57:52 +0000407 mScreenBrightnessRangeMaximum = clampAbsoluteBrightness(resources.getInteger(
408 com.android.internal.R.integer.config_screenBrightnessSettingMaximum));
Michael Wrightd8460232018-01-16 18:04:59 +0000409 mScreenBrightnessDefault = clampAbsoluteBrightness(resources.getInteger(
410 com.android.internal.R.integer.config_screenBrightnessSettingDefault));
Santos Cordonb12c7e12018-04-13 17:10:54 -0700411
412 mScreenBrightnessForVrRangeMinimum = clampAbsoluteBrightness(resources.getInteger(
413 com.android.internal.R.integer.config_screenBrightnessForVrSettingMinimum));
414 mScreenBrightnessForVrRangeMaximum = clampAbsoluteBrightness(resources.getInteger(
415 com.android.internal.R.integer.config_screenBrightnessForVrSettingMaximum));
Michael Wrightd8460232018-01-16 18:04:59 +0000416 mScreenBrightnessForVrDefault = clampAbsoluteBrightness(resources.getInteger(
417 com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault));
Jeff Brownb76eebff2012-10-05 22:26:44 -0700418
Jeff Brown96307042012-07-27 15:51:34 -0700419 mUseSoftwareAutoBrightnessConfig = resources.getBoolean(
420 com.android.internal.R.bool.config_automatic_brightness_available);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700421
422 mAllowAutoBrightnessWhileDozingConfig = resources.getBoolean(
423 com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing);
424
Prashant Malani99e6d432016-03-29 16:32:37 -0700425 mBrightnessRampRateFast = resources.getInteger(
426 com.android.internal.R.integer.config_brightness_ramp_rate_fast);
Julius D'souzafeadad12016-08-05 14:34:38 -0700427 mBrightnessRampRateSlow = resources.getInteger(
428 com.android.internal.R.integer.config_brightness_ramp_rate_slow);
Julius D'souzad5105dd2017-06-02 11:03:53 -0700429 mSkipScreenOnBrightnessRamp = resources.getBoolean(
430 com.android.internal.R.bool.config_skipScreenOnBrightnessRamp);
Prashant Malani99e6d432016-03-29 16:32:37 -0700431
Jeff Brown96307042012-07-27 15:51:34 -0700432 if (mUseSoftwareAutoBrightnessConfig) {
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700433 final float dozeScaleFactor = resources.getFraction(
434 com.android.internal.R.fraction.config_screenAutoBrightnessDozeScaleFactor,
435 1, 1);
Jeff Brown1a30b552012-08-16 01:31:11 -0700436
Dan Gittiked958e92018-11-13 14:58:20 +0000437 int[] ambientBrighteningThresholds = resources.getIntArray(
438 com.android.internal.R.array.config_ambientBrighteningThresholds);
439 int[] ambientDarkeningThresholds = resources.getIntArray(
440 com.android.internal.R.array.config_ambientDarkeningThresholds);
441 int[] ambientThresholdLevels = resources.getIntArray(
442 com.android.internal.R.array.config_ambientThresholdLevels);
443 HysteresisLevels ambientBrightnessThresholds = new HysteresisLevels(
444 ambientBrighteningThresholds, ambientDarkeningThresholds,
445 ambientThresholdLevels);
446
447 int[] screenBrighteningThresholds = resources.getIntArray(
448 com.android.internal.R.array.config_screenBrighteningThresholds);
449 int[] screenDarkeningThresholds = resources.getIntArray(
450 com.android.internal.R.array.config_screenDarkeningThresholds);
451 int[] screenThresholdLevels = resources.getIntArray(
452 com.android.internal.R.array.config_screenThresholdLevels);
453 HysteresisLevels screenBrightnessThresholds = new HysteresisLevels(
454 screenBrighteningThresholds, screenDarkeningThresholds, screenThresholdLevels);
Michael Wrighteef0e132017-11-21 17:57:52 +0000455
456 long brighteningLightDebounce = resources.getInteger(
457 com.android.internal.R.integer.config_autoBrightnessBrighteningLightDebounce);
458 long darkeningLightDebounce = resources.getInteger(
459 com.android.internal.R.integer.config_autoBrightnessDarkeningLightDebounce);
460 boolean autoBrightnessResetAmbientLuxAfterWarmUp = resources.getBoolean(
461 com.android.internal.R.bool.config_autoBrightnessResetAmbientLuxAfterWarmUp);
Michael Wrighteef0e132017-11-21 17:57:52 +0000462
463 int lightSensorWarmUpTimeConfig = resources.getInteger(
464 com.android.internal.R.integer.config_lightSensorWarmupTime);
465 int lightSensorRate = resources.getInteger(
466 com.android.internal.R.integer.config_autoBrightnessLightSensorRate);
467 int initialLightSensorRate = resources.getInteger(
468 com.android.internal.R.integer.config_autoBrightnessInitialLightSensorRate);
469 if (initialLightSensorRate == -1) {
470 initialLightSensorRate = lightSensorRate;
471 } else if (initialLightSensorRate > lightSensorRate) {
472 Slog.w(TAG, "Expected config_autoBrightnessInitialLightSensorRate ("
473 + initialLightSensorRate + ") to be less than or equal to "
474 + "config_autoBrightnessLightSensorRate (" + lightSensorRate + ").");
475 }
Dan Gittik825cc072018-12-17 14:24:48 +0000476 int shortTermModelTimeout = resources.getInteger(
477 com.android.internal.R.integer.config_autoBrightnessShortTermModelTimeout);
Michael Wrighteef0e132017-11-21 17:57:52 +0000478
Michael Wrighte02db662019-03-08 21:24:36 +0000479 String lightSensorType = resources.getString(
480 com.android.internal.R.string.config_displayLightSensorType);
481 Sensor lightSensor = findDisplayLightSensor(lightSensorType);
482
Michael Wright144aac92017-12-21 18:37:41 +0000483 mBrightnessMapper = BrightnessMappingStrategy.create(resources);
484 if (mBrightnessMapper != null) {
Jeff Brown131206b2014-04-08 17:27:14 -0700485 mAutomaticBrightnessController = new AutomaticBrightnessController(this,
Michael Wrighte02db662019-03-08 21:24:36 +0000486 handler.getLooper(), sensorManager, lightSensor, mBrightnessMapper,
Michael Wright144aac92017-12-21 18:37:41 +0000487 lightSensorWarmUpTimeConfig, mScreenBrightnessRangeMinimum,
488 mScreenBrightnessRangeMaximum, dozeScaleFactor, lightSensorRate,
489 initialLightSensorRate, brighteningLightDebounce, darkeningLightDebounce,
Dan Gittiked958e92018-11-13 14:58:20 +0000490 autoBrightnessResetAmbientLuxAfterWarmUp, ambientBrightnessThresholds,
Dan Gittika5a2d632019-01-09 14:25:29 +0000491 screenBrightnessThresholds, shortTermModelTimeout,
492 context.getPackageManager());
Michael Wrighteef0e132017-11-21 17:57:52 +0000493 } else {
494 mUseSoftwareAutoBrightnessConfig = false;
Jeff Brown96307042012-07-27 15:51:34 -0700495 }
Jeff Brown96307042012-07-27 15:51:34 -0700496 }
497
Narayan Kamath38819982017-07-06 18:15:30 +0100498 mColorFadeEnabled = !ActivityManager.isLowRamDeviceStatic();
Michael Lentine0839adb2014-07-29 18:47:56 -0700499 mColorFadeFadesConfig = resources.getBoolean(
Jeff Browna52772f2012-10-04 18:38:09 -0700500 com.android.internal.R.bool.config_animateScreenLights);
501
Michael Wrightc3e6af82017-07-25 22:31:03 +0100502 mDisplayBlanksAfterDozeConfig = resources.getBoolean(
503 com.android.internal.R.bool.config_displayBlanksAfterDoze);
Michael Wright05e76fe2017-07-20 18:18:33 +0100504
Michael Wright63a40062017-07-26 01:10:59 +0100505 mBrightnessBucketsInDozeConfig = resources.getBoolean(
506 com.android.internal.R.bool.config_displayBrightnessBucketsInDoze);
507
Jeff Brown96307042012-07-27 15:51:34 -0700508 if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) {
509 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
510 if (mProximitySensor != null) {
511 mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(),
512 TYPICAL_PROXIMITY_THRESHOLD);
513 }
514 }
515
Michael Wrightd8460232018-01-16 18:04:59 +0000516 mCurrentScreenBrightnessSetting = getScreenBrightnessSetting();
517 mScreenBrightnessForVr = getScreenBrightnessForVrSetting();
518 mAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting();
519 mTemporaryScreenBrightness = -1;
Kenny Guyad9a6ea2018-01-31 15:57:15 +0000520 mPendingScreenBrightnessSetting = -1;
Michael Wrightd8460232018-01-16 18:04:59 +0000521 mTemporaryAutoBrightnessAdjustment = Float.NaN;
Michael Wright9fdf66b2018-05-22 15:35:05 +0100522 mPendingAutoBrightnessAdjustment = Float.NaN;
Dan Gittik8dbd7e92018-12-03 15:35:53 +0000523
524 DisplayWhiteBalanceSettings displayWhiteBalanceSettings = null;
525 DisplayWhiteBalanceController displayWhiteBalanceController = null;
526 try {
527 displayWhiteBalanceSettings = new DisplayWhiteBalanceSettings(mContext, mHandler);
528 displayWhiteBalanceController = DisplayWhiteBalanceFactory.create(mHandler,
529 mSensorManager, resources);
530 displayWhiteBalanceSettings.setCallbacks(this);
531 displayWhiteBalanceController.setCallbacks(this);
532 } catch (Exception e) {
533 Slog.e(TAG, "failed to set up display white-balance: " + e);
534 }
535 mDisplayWhiteBalanceSettings = displayWhiteBalanceSettings;
536 mDisplayWhiteBalanceController = displayWhiteBalanceController;
Michael Wrighteef0e132017-11-21 17:57:52 +0000537 }
538
Michael Wrighte02db662019-03-08 21:24:36 +0000539 private Sensor findDisplayLightSensor(String sensorType) {
540 if (!TextUtils.isEmpty(sensorType)) {
541 List<Sensor> sensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
542 for (int i = 0; i < sensors.size(); i++) {
543 Sensor sensor = sensors.get(i);
544 if (sensorType.equals(sensor.getStringType())) {
545 return sensor;
546 }
547 }
548 }
549 return mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
550 }
551
Jeff Brown96307042012-07-27 15:51:34 -0700552 /**
553 * Returns true if the proximity sensor screen-off function is available.
554 */
555 public boolean isProximitySensorAvailable() {
556 return mProximitySensor != null;
557 }
558
559 /**
Michael Wright144aac92017-12-21 18:37:41 +0000560 * Get the {@link BrightnessChangeEvent}s for the specified user.
561 * @param userId userId to fetch data for
562 * @param includePackage if false will null out the package name in events
563 */
564 public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents(
565 @UserIdInt int userId, boolean includePackage) {
566 return mBrightnessTracker.getEvents(userId, includePackage);
567 }
568
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000569 public void onSwitchUser(@UserIdInt int newUserId) {
Kenny Guyecc978f2018-03-14 17:30:20 +0000570 handleSettingsChange(true /* userSwitch */);
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000571 mBrightnessTracker.onSwitchUser(newUserId);
Dan Gittik8dbd7e92018-12-03 15:35:53 +0000572 if (mDisplayWhiteBalanceSettings != null) {
573 mDisplayWhiteBalanceSettings.onSwitchUser();
574 }
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000575 }
576
577 public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats(
578 @UserIdInt int userId) {
579 return mBrightnessTracker.getAmbientBrightnessStats(userId);
580 }
581
Michael Wright144aac92017-12-21 18:37:41 +0000582 /**
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000583 * Persist the brightness slider events and ambient brightness stats to disk.
Michael Wright144aac92017-12-21 18:37:41 +0000584 */
Peeyush Agarwalcc155dd2018-01-10 11:51:33 +0000585 public void persistBrightnessTrackerState() {
586 mBrightnessTracker.persistBrightnessTrackerState();
Michael Wright144aac92017-12-21 18:37:41 +0000587 }
588
589 /**
Jeff Brown96307042012-07-27 15:51:34 -0700590 * Requests a new power state.
591 * The controller makes a copy of the provided object and then
592 * begins adjusting the power state to match what was requested.
593 *
594 * @param request The requested power state.
595 * @param waitForNegativeProximity If true, issues a request to wait for
596 * negative proximity before turning the screen back on, assuming the screen
597 * was turned off by the proximity sensor.
598 * @return True if display is ready, false if there are important changes that must
599 * be made asynchronously (such as turning the screen on), in which case the caller
Jeff Brown606e4e82014-09-18 15:22:26 -0700600 * should grab a wake lock, watch for {@link DisplayPowerCallbacks#onStateChanged()}
601 * then try the request again later until the state converges.
Jeff Brown96307042012-07-27 15:51:34 -0700602 */
603 public boolean requestPowerState(DisplayPowerRequest request,
604 boolean waitForNegativeProximity) {
605 if (DEBUG) {
606 Slog.d(TAG, "requestPowerState: "
607 + request + ", waitForNegativeProximity=" + waitForNegativeProximity);
608 }
609
610 synchronized (mLock) {
611 boolean changed = false;
612
613 if (waitForNegativeProximity
614 && !mPendingWaitForNegativeProximityLocked) {
615 mPendingWaitForNegativeProximityLocked = true;
616 changed = true;
617 }
618
619 if (mPendingRequestLocked == null) {
620 mPendingRequestLocked = new DisplayPowerRequest(request);
621 changed = true;
622 } else if (!mPendingRequestLocked.equals(request)) {
623 mPendingRequestLocked.copyFrom(request);
624 changed = true;
625 }
626
627 if (changed) {
628 mDisplayReadyLocked = false;
629 }
630
631 if (changed && !mPendingRequestChangedLocked) {
632 mPendingRequestChangedLocked = true;
633 sendUpdatePowerStateLocked();
634 }
635
636 return mDisplayReadyLocked;
637 }
638 }
639
Kenny Guy6d1009f2018-03-14 14:28:23 +0000640 public BrightnessConfiguration getDefaultBrightnessConfiguration() {
Dan Gittik9a338892019-01-30 18:52:37 +0000641 if (mAutomaticBrightnessController == null) {
642 return null;
643 }
Kenny Guy6d1009f2018-03-14 14:28:23 +0000644 return mAutomaticBrightnessController.getDefaultConfig();
645 }
646
Jeff Brown96307042012-07-27 15:51:34 -0700647 private void sendUpdatePowerState() {
648 synchronized (mLock) {
649 sendUpdatePowerStateLocked();
650 }
651 }
652
653 private void sendUpdatePowerStateLocked() {
654 if (!mPendingUpdatePowerStateLocked) {
655 mPendingUpdatePowerStateLocked = true;
656 Message msg = mHandler.obtainMessage(MSG_UPDATE_POWER_STATE);
Jeff Brown96307042012-07-27 15:51:34 -0700657 mHandler.sendMessage(msg);
658 }
659 }
660
661 private void initialize() {
Jeff Brown037c33e2014-04-09 00:31:55 -0700662 // Initialize the power state object for the default display.
663 // In the future, we might manage multiple displays independently.
664 mPowerState = new DisplayPowerState(mBlanker,
Narayan Kamath38819982017-07-06 18:15:30 +0100665 mColorFadeEnabled ? new ColorFade(Display.DEFAULT_DISPLAY) : null);
Jeff Brown96307042012-07-27 15:51:34 -0700666
Narayan Kamath38819982017-07-06 18:15:30 +0100667 if (mColorFadeEnabled) {
668 mColorFadeOnAnimator = ObjectAnimator.ofFloat(
669 mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 0.0f, 1.0f);
670 mColorFadeOnAnimator.setDuration(COLOR_FADE_ON_ANIMATION_DURATION_MILLIS);
671 mColorFadeOnAnimator.addListener(mAnimatorListener);
Jeff Brown96307042012-07-27 15:51:34 -0700672
Narayan Kamath38819982017-07-06 18:15:30 +0100673 mColorFadeOffAnimator = ObjectAnimator.ofFloat(
674 mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 1.0f, 0.0f);
675 mColorFadeOffAnimator.setDuration(COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS);
676 mColorFadeOffAnimator.addListener(mAnimatorListener);
677 }
Jeff Brown96307042012-07-27 15:51:34 -0700678
679 mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>(
680 mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS);
Jeff Brown42558692014-05-20 22:02:46 -0700681 mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800682
Jeff Brown131206b2014-04-08 17:27:14 -0700683 // Initialize screen state for battery stats.
684 try {
Jeff Browne95c3cd2014-05-02 16:59:26 -0700685 mBatteryStats.noteScreenState(mPowerState.getScreenState());
Jeff Brown131206b2014-04-08 17:27:14 -0700686 mBatteryStats.noteScreenBrightness(mPowerState.getScreenBrightness());
687 } catch (RemoteException ex) {
688 // same process
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800689 }
Michael Wright144aac92017-12-21 18:37:41 +0000690
691 // Initialize all of the brightness tracking state
Michael Wrightd8460232018-01-16 18:04:59 +0000692 final float brightness = convertToNits(mPowerState.getScreenBrightness());
Michael Wright144aac92017-12-21 18:37:41 +0000693 if (brightness >= 0.0f) {
694 mBrightnessTracker.start(brightness);
695 }
Michael Wrightd8460232018-01-16 18:04:59 +0000696
697 mContext.getContentResolver().registerContentObserver(
698 Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
699 false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL);
700 mContext.getContentResolver().registerContentObserver(
Santos Cordonb12c7e12018-04-13 17:10:54 -0700701 Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_FOR_VR),
702 false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL);
703 mContext.getContentResolver().registerContentObserver(
Michael Wrightd8460232018-01-16 18:04:59 +0000704 Settings.System.getUriFor(Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ),
705 false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL);
Jeff Brown96307042012-07-27 15:51:34 -0700706 }
707
708 private final Animator.AnimatorListener mAnimatorListener = new Animator.AnimatorListener() {
709 @Override
710 public void onAnimationStart(Animator animation) {
711 }
712 @Override
713 public void onAnimationEnd(Animator animation) {
714 sendUpdatePowerState();
715 }
716 @Override
717 public void onAnimationRepeat(Animator animation) {
718 }
719 @Override
720 public void onAnimationCancel(Animator animation) {
721 }
722 };
723
Jeff Brown42558692014-05-20 22:02:46 -0700724 private final RampAnimator.Listener mRampAnimatorListener = new RampAnimator.Listener() {
725 @Override
726 public void onAnimationEnd() {
727 sendUpdatePowerState();
728 }
729 };
730
Jeff Brown96307042012-07-27 15:51:34 -0700731 private void updatePowerState() {
732 // Update the power state request.
733 final boolean mustNotify;
734 boolean mustInitialize = false;
Jeff Brown330560f2012-08-21 22:10:57 -0700735
Jeff Brown96307042012-07-27 15:51:34 -0700736 synchronized (mLock) {
737 mPendingUpdatePowerStateLocked = false;
738 if (mPendingRequestLocked == null) {
739 return; // wait until first actual power request
740 }
741
742 if (mPowerRequest == null) {
743 mPowerRequest = new DisplayPowerRequest(mPendingRequestLocked);
744 mWaitingForNegativeProximity = mPendingWaitForNegativeProximityLocked;
Jeff Brown6307a152012-08-20 13:24:23 -0700745 mPendingWaitForNegativeProximityLocked = false;
Jeff Brown96307042012-07-27 15:51:34 -0700746 mPendingRequestChangedLocked = false;
747 mustInitialize = true;
748 } else if (mPendingRequestChangedLocked) {
749 mPowerRequest.copyFrom(mPendingRequestLocked);
750 mWaitingForNegativeProximity |= mPendingWaitForNegativeProximityLocked;
Jeff Brown6307a152012-08-20 13:24:23 -0700751 mPendingWaitForNegativeProximityLocked = false;
Jeff Brown96307042012-07-27 15:51:34 -0700752 mPendingRequestChangedLocked = false;
753 mDisplayReadyLocked = false;
754 }
755
756 mustNotify = !mDisplayReadyLocked;
757 }
758
759 // Initialize things the first time the power state is changed.
760 if (mustInitialize) {
761 initialize();
762 }
763
Jeff Brown970d4132014-07-19 11:33:47 -0700764 // Compute the basic display state using the policy.
765 // We might override this below based on other factors.
766 int state;
767 int brightness = PowerManager.BRIGHTNESS_DEFAULT;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700768 boolean performScreenOffTransition = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700769 switch (mPowerRequest.policy) {
770 case DisplayPowerRequest.POLICY_OFF:
771 state = Display.STATE_OFF;
Jeff Brown2175e9c2014-09-12 16:11:07 -0700772 performScreenOffTransition = true;
Jeff Brown970d4132014-07-19 11:33:47 -0700773 break;
774 case DisplayPowerRequest.POLICY_DOZE:
775 if (mPowerRequest.dozeScreenState != Display.STATE_UNKNOWN) {
776 state = mPowerRequest.dozeScreenState;
777 } else {
778 state = Display.STATE_DOZE;
779 }
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700780 if (!mAllowAutoBrightnessWhileDozingConfig) {
781 brightness = mPowerRequest.dozeScreenBrightness;
782 }
Jeff Brown970d4132014-07-19 11:33:47 -0700783 break;
Santos Cordon3107d292016-09-20 15:50:35 -0700784 case DisplayPowerRequest.POLICY_VR:
785 state = Display.STATE_VR;
786 break;
Jeff Brown970d4132014-07-19 11:33:47 -0700787 case DisplayPowerRequest.POLICY_DIM:
788 case DisplayPowerRequest.POLICY_BRIGHT:
789 default:
790 state = Display.STATE_ON;
791 break;
792 }
Jeff Brown2175e9c2014-09-12 16:11:07 -0700793 assert(state != Display.STATE_UNKNOWN);
Jeff Brown970d4132014-07-19 11:33:47 -0700794
Jeff Brown6307a152012-08-20 13:24:23 -0700795 // Apply the proximity sensor.
Jeff Brown96307042012-07-27 15:51:34 -0700796 if (mProximitySensor != null) {
Jeff Brown970d4132014-07-19 11:33:47 -0700797 if (mPowerRequest.useProximitySensor && state != Display.STATE_OFF) {
Jeff Brown6307a152012-08-20 13:24:23 -0700798 setProximitySensorEnabled(true);
799 if (!mScreenOffBecauseOfProximity
800 && mProximity == PROXIMITY_POSITIVE) {
801 mScreenOffBecauseOfProximity = true;
Jeff Brownec083212013-09-11 20:45:25 -0700802 sendOnProximityPositiveWithWakelock();
Jeff Brown6307a152012-08-20 13:24:23 -0700803 }
804 } else if (mWaitingForNegativeProximity
805 && mScreenOffBecauseOfProximity
806 && mProximity == PROXIMITY_POSITIVE
Jeff Brown970d4132014-07-19 11:33:47 -0700807 && state != Display.STATE_OFF) {
Jeff Brown6307a152012-08-20 13:24:23 -0700808 setProximitySensorEnabled(true);
809 } else {
810 setProximitySensorEnabled(false);
811 mWaitingForNegativeProximity = false;
812 }
813 if (mScreenOffBecauseOfProximity
814 && mProximity != PROXIMITY_POSITIVE) {
Jeff Brown96307042012-07-27 15:51:34 -0700815 mScreenOffBecauseOfProximity = false;
Jeff Brownec083212013-09-11 20:45:25 -0700816 sendOnProximityNegativeWithWakelock();
Jeff Brown96307042012-07-27 15:51:34 -0700817 }
Jeff Brown6307a152012-08-20 13:24:23 -0700818 } else {
819 mWaitingForNegativeProximity = false;
Jeff Brown96307042012-07-27 15:51:34 -0700820 }
Jeff Brown970d4132014-07-19 11:33:47 -0700821 if (mScreenOffBecauseOfProximity) {
822 state = Display.STATE_OFF;
Jeff Brown96307042012-07-27 15:51:34 -0700823 }
824
Jeff Brownbf4e4142014-10-02 13:08:05 -0700825 // Animate the screen state change unless already animating.
826 // The transition may be deferred, so after this point we will use the
827 // actual state instead of the desired one.
Santos Cordon3107d292016-09-20 15:50:35 -0700828 final int oldState = mPowerState.getScreenState();
Jeff Brownbf4e4142014-10-02 13:08:05 -0700829 animateScreenStateChange(state, performScreenOffTransition);
830 state = mPowerState.getScreenState();
831
Jeff Brown970d4132014-07-19 11:33:47 -0700832 // Use zero brightness when screen is off.
833 if (state == Display.STATE_OFF) {
834 brightness = PowerManager.BRIGHTNESS_OFF;
835 }
836
Michael Wrightd8460232018-01-16 18:04:59 +0000837 // Always use the VR brightness when in the VR state.
838 if (state == Display.STATE_VR) {
839 brightness = mScreenBrightnessForVr;
840 }
841
842 if (brightness < 0 && mPowerRequest.screenBrightnessOverride > 0) {
843 brightness = mPowerRequest.screenBrightnessOverride;
Michael Wright2155bc22018-05-01 00:38:32 +0100844 mAppliedScreenBrightnessOverride = true;
845 } else {
846 mAppliedScreenBrightnessOverride = false;
Michael Wrightd8460232018-01-16 18:04:59 +0000847 }
Michael Wright144aac92017-12-21 18:37:41 +0000848
849 final boolean autoBrightnessEnabledInDoze =
850 mAllowAutoBrightnessWhileDozingConfig && Display.isDozeState(state);
851 final boolean autoBrightnessEnabled = mPowerRequest.useAutoBrightness
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700852 && (state == Display.STATE_ON || autoBrightnessEnabledInDoze)
Michael Wright144aac92017-12-21 18:37:41 +0000853 && brightness < 0
854 && mAutomaticBrightnessController != null;
Michael Wright144aac92017-12-21 18:37:41 +0000855
Michael Wrightd8460232018-01-16 18:04:59 +0000856 final boolean userSetBrightnessChanged = updateUserSetScreenBrightness();
Michael Wright144aac92017-12-21 18:37:41 +0000857
Michael Wrightd8460232018-01-16 18:04:59 +0000858 // Use the temporary screen brightness if there isn't an override, either from
859 // WindowManager or based on the display state.
860 if (mTemporaryScreenBrightness > 0) {
861 brightness = mTemporaryScreenBrightness;
Michael Wright2155bc22018-05-01 00:38:32 +0100862 mAppliedTemporaryBrightness = true;
863 } else {
864 mAppliedTemporaryBrightness = false;
Michael Wrightd8460232018-01-16 18:04:59 +0000865 }
866
867 final boolean autoBrightnessAdjustmentChanged = updateAutoBrightnessAdjustment();
868 if (autoBrightnessAdjustmentChanged) {
869 mTemporaryAutoBrightnessAdjustment = Float.NaN;
870 }
871
872 // Use the autobrightness adjustment override if set.
873 final float autoBrightnessAdjustment;
874 if (!Float.isNaN(mTemporaryAutoBrightnessAdjustment)) {
875 autoBrightnessAdjustment = mTemporaryAutoBrightnessAdjustment;
Michael Wright2155bc22018-05-01 00:38:32 +0100876 mAppliedTemporaryAutoBrightnessAdjustment = true;
Michael Wrightd8460232018-01-16 18:04:59 +0000877 } else {
878 autoBrightnessAdjustment = mAutoBrightnessAdjustment;
Kenny Guy3931a3e2018-05-22 11:41:17 +0100879 mAppliedTemporaryAutoBrightnessAdjustment = false;
Michael Wrightd8460232018-01-16 18:04:59 +0000880 }
881
882 // Apply brightness boost.
883 // We do this here after deciding whether auto-brightness is enabled so that we don't
884 // disable the light sensor during this temporary state. That way when boost ends we will
885 // be able to resume normal auto-brightness behavior without any delay.
886 if (mPowerRequest.boostScreenBrightness
887 && brightness != PowerManager.BRIGHTNESS_OFF) {
888 brightness = PowerManager.BRIGHTNESS_ON;
Michael Wright2155bc22018-05-01 00:38:32 +0100889 mAppliedBrightnessBoost = true;
890 } else {
891 mAppliedBrightnessBoost = false;
Michael Wrightd8460232018-01-16 18:04:59 +0000892 }
893
Kenny Guy53d06612018-01-30 14:19:13 +0000894 // If the brightness is already set then it's been overridden by something other than the
Michael Wrightd8460232018-01-16 18:04:59 +0000895 // user, or is a temporary adjustment.
Kenny Guyb54d40b2019-02-28 12:49:53 +0000896 boolean userInitiatedChange = brightness < 0
Michael Wrightd8460232018-01-16 18:04:59 +0000897 && (autoBrightnessAdjustmentChanged || userSetBrightnessChanged);
898
Kenny Guy53d06612018-01-30 14:19:13 +0000899 boolean hadUserBrightnessPoint = false;
Michael Wright144aac92017-12-21 18:37:41 +0000900 // Configure auto-brightness.
901 if (mAutomaticBrightnessController != null) {
Kenny Guy53d06612018-01-30 14:19:13 +0000902 hadUserBrightnessPoint = mAutomaticBrightnessController.hasUserDataPoints();
Jeff Brown970d4132014-07-19 11:33:47 -0700903 mAutomaticBrightnessController.configure(autoBrightnessEnabled,
Michael Wrightd8460232018-01-16 18:04:59 +0000904 mBrightnessConfiguration,
905 mLastUserSetScreenBrightness / (float) PowerManager.BRIGHTNESS_ON,
Michael Wright617564f2018-01-25 22:20:54 +0000906 userSetBrightnessChanged, autoBrightnessAdjustment,
907 autoBrightnessAdjustmentChanged, mPowerRequest.policy);
Jeff Brown7b5be5e2014-11-12 18:45:31 -0800908 }
909
Jeff Brown970d4132014-07-19 11:33:47 -0700910 // Apply auto-brightness.
911 boolean slowChange = false;
912 if (brightness < 0) {
Michael Wright617564f2018-01-25 22:20:54 +0000913 float newAutoBrightnessAdjustment = autoBrightnessAdjustment;
Jeff Brown970d4132014-07-19 11:33:47 -0700914 if (autoBrightnessEnabled) {
915 brightness = mAutomaticBrightnessController.getAutomaticScreenBrightness();
Michael Wright617564f2018-01-25 22:20:54 +0000916 newAutoBrightnessAdjustment =
917 mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment();
Jeff Brown970d4132014-07-19 11:33:47 -0700918 }
Michael Wright617564f2018-01-25 22:20:54 +0000919
Jeff Brown970d4132014-07-19 11:33:47 -0700920 if (brightness >= 0) {
921 // Use current auto-brightness value and slowly adjust to changes.
922 brightness = clampScreenBrightness(brightness);
923 if (mAppliedAutoBrightness && !autoBrightnessAdjustmentChanged) {
924 slowChange = true; // slowly adapt to auto-brightness
925 }
Michael Wrightd8460232018-01-16 18:04:59 +0000926 // Tell the rest of the system about the new brightness. Note that we do this
927 // before applying the low power or dim transformations so that the slider
928 // accurately represents the full possible range, even if they range changes what
929 // it means in absolute terms.
930 putScreenBrightnessSetting(brightness);
Jeff Brown970d4132014-07-19 11:33:47 -0700931 mAppliedAutoBrightness = true;
Jeff Brown96307042012-07-27 15:51:34 -0700932 } else {
Jeff Brown970d4132014-07-19 11:33:47 -0700933 mAppliedAutoBrightness = false;
Jeff Brown96307042012-07-27 15:51:34 -0700934 }
Michael Wright617564f2018-01-25 22:20:54 +0000935 if (autoBrightnessAdjustment != newAutoBrightnessAdjustment) {
936 // If the autobrightness controller has decided to change the adjustment value
937 // used, make sure that's reflected in settings.
938 putAutoBrightnessAdjustmentSetting(newAutoBrightnessAdjustment);
939 }
Jeff Brown96307042012-07-27 15:51:34 -0700940 } else {
Jeff Brown970d4132014-07-19 11:33:47 -0700941 mAppliedAutoBrightness = false;
942 }
943
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700944 // Use default brightness when dozing unless overridden.
Michael Wrightb0a1d3d2017-09-22 15:05:02 +0100945 if (brightness < 0 && Display.isDozeState(state)) {
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -0700946 brightness = mScreenBrightnessDozeConfig;
947 }
948
Jeff Brown970d4132014-07-19 11:33:47 -0700949 // Apply manual brightness.
Jeff Brown970d4132014-07-19 11:33:47 -0700950 if (brightness < 0) {
Michael Wright7bb22342018-01-26 00:16:41 +0000951 brightness = clampScreenBrightness(mCurrentScreenBrightnessSetting);
Jeff Brown970d4132014-07-19 11:33:47 -0700952 }
953
954 // Apply dimming by at least some minimum amount when user activity
955 // timeout is about to expire.
956 if (mPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) {
Jeff Brown5c8ea082014-07-24 19:30:31 -0700957 if (brightness > mScreenBrightnessRangeMinimum) {
958 brightness = Math.max(Math.min(brightness - SCREEN_DIM_MINIMUM_REDUCTION,
959 mScreenBrightnessDimConfig), mScreenBrightnessRangeMinimum);
960 }
Jeff Brown970d4132014-07-19 11:33:47 -0700961 if (!mAppliedDimming) {
962 slowChange = false;
963 }
964 mAppliedDimming = true;
mochangmingf0cb46c2015-12-29 13:55:24 +0800965 } else if (mAppliedDimming) {
966 slowChange = false;
967 mAppliedDimming = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700968 }
969
jackqdyulei92681e82017-02-28 11:26:28 -0800970 // If low power mode is enabled, scale brightness by screenLowPowerBrightnessFactor
Jeff Brown970d4132014-07-19 11:33:47 -0700971 // as long as it is above the minimum threshold.
972 if (mPowerRequest.lowPowerMode) {
973 if (brightness > mScreenBrightnessRangeMinimum) {
jackqdyulei92681e82017-02-28 11:26:28 -0800974 final float brightnessFactor =
975 Math.min(mPowerRequest.screenLowPowerBrightnessFactor, 1);
976 final int lowPowerBrightness = (int) (brightness * brightnessFactor);
977 brightness = Math.max(lowPowerBrightness, mScreenBrightnessRangeMinimum);
Jeff Brown970d4132014-07-19 11:33:47 -0700978 }
979 if (!mAppliedLowPower) {
980 slowChange = false;
981 }
982 mAppliedLowPower = true;
mochangmingf0cb46c2015-12-29 13:55:24 +0800983 } else if (mAppliedLowPower) {
984 slowChange = false;
985 mAppliedLowPower = false;
Jeff Brown970d4132014-07-19 11:33:47 -0700986 }
987
Jeff Brown0a434772014-09-30 14:42:27 -0700988 // Animate the screen brightness when the screen is on or dozing.
Santos Cordon3107d292016-09-20 15:50:35 -0700989 // Skip the animation when the screen is off or suspended or transition to/from VR.
Prashant Malani33538242014-11-13 14:04:00 -0800990 if (!mPendingScreenOff) {
Julius D'souzad5105dd2017-06-02 11:03:53 -0700991 if (mSkipScreenOnBrightnessRamp) {
Julius D'souzad5105dd2017-06-02 11:03:53 -0700992 if (state == Display.STATE_ON) {
993 if (mSkipRampState == RAMP_STATE_SKIP_NONE && mDozing) {
994 mInitialAutoBrightness = brightness;
995 mSkipRampState = RAMP_STATE_SKIP_INITIAL;
996 } else if (mSkipRampState == RAMP_STATE_SKIP_INITIAL
997 && mUseSoftwareAutoBrightnessConfig
998 && brightness != mInitialAutoBrightness) {
999 mSkipRampState = RAMP_STATE_SKIP_AUTOBRIGHT;
1000 } else if (mSkipRampState == RAMP_STATE_SKIP_AUTOBRIGHT) {
1001 mSkipRampState = RAMP_STATE_SKIP_NONE;
1002 }
1003 } else {
1004 mSkipRampState = RAMP_STATE_SKIP_NONE;
1005 }
1006 }
1007
Michael Wrightb0a1d3d2017-09-22 15:05:02 +01001008 final boolean wasOrWillBeInVr =
1009 (state == Display.STATE_VR || oldState == Display.STATE_VR);
1010 final boolean initialRampSkip =
1011 state == Display.STATE_ON && mSkipRampState != RAMP_STATE_SKIP_NONE;
1012 // While dozing, sometimes the brightness is split into buckets. Rather than animating
1013 // through the buckets, which is unlikely to be smooth in the first place, just jump
1014 // right to the suggested brightness.
1015 final boolean hasBrightnessBuckets =
1016 Display.isDozeState(state) && mBrightnessBucketsInDozeConfig;
1017 // If the color fade is totally covering the screen then we can change the backlight
1018 // level without it being a noticeable jump since any actual content isn't yet visible.
1019 final boolean isDisplayContentVisible =
1020 mColorFadeEnabled && mPowerState.getColorFadeLevel() == 1.0f;
Michael Wright2155bc22018-05-01 00:38:32 +01001021 final boolean brightnessIsTemporary =
1022 mAppliedTemporaryBrightness || mAppliedTemporaryAutoBrightnessAdjustment;
Michael Wrightb0a1d3d2017-09-22 15:05:02 +01001023 if (initialRampSkip || hasBrightnessBuckets
Michael Wrightd8460232018-01-16 18:04:59 +00001024 || wasOrWillBeInVr || !isDisplayContentVisible || brightnessIsTemporary) {
Michael Wrightb0a1d3d2017-09-22 15:05:02 +01001025 animateScreenBrightness(brightness, 0);
1026 } else {
Prashant Malani33538242014-11-13 14:04:00 -08001027 animateScreenBrightness(brightness,
Julius D'souzafeadad12016-08-05 14:34:38 -07001028 slowChange ? mBrightnessRampRateSlow : mBrightnessRampRateFast);
Prashant Malani33538242014-11-13 14:04:00 -08001029 }
Michael Wright144aac92017-12-21 18:37:41 +00001030
Michael Wrightd8460232018-01-16 18:04:59 +00001031 if (!brightnessIsTemporary) {
Kenny Guyb54d40b2019-02-28 12:49:53 +00001032 if (userInitiatedChange && (mAutomaticBrightnessController == null
1033 || !mAutomaticBrightnessController.hasValidAmbientLux())) {
1034 // If we don't have a valid lux reading we can't report a valid
1035 // slider event so notify as if the system changed the brightness.
1036 userInitiatedChange = false;
1037 }
Kenny Guy53d06612018-01-30 14:19:13 +00001038 notifyBrightnessChanged(brightness, userInitiatedChange, hadUserBrightnessPoint);
Michael Wright144aac92017-12-21 18:37:41 +00001039 }
Michael Wrightd8460232018-01-16 18:04:59 +00001040
Jeff Brown0a434772014-09-30 14:42:27 -07001041 }
1042
Dan Gittik8dbd7e92018-12-03 15:35:53 +00001043 // Update display white-balance.
1044 if (mDisplayWhiteBalanceController != null) {
1045 if (state == Display.STATE_ON && mDisplayWhiteBalanceSettings.isEnabled()) {
1046 mDisplayWhiteBalanceController.setEnabled(true);
Dan Gittik1151ac02019-02-13 14:05:37 +00001047 mDisplayWhiteBalanceController.updateDisplayColorTemperature();
Dan Gittik8dbd7e92018-12-03 15:35:53 +00001048 } else {
1049 mDisplayWhiteBalanceController.setEnabled(false);
1050 }
1051 }
1052
Jeff Brown0a434772014-09-30 14:42:27 -07001053 // Determine whether the display is ready for use in the newly requested state.
1054 // Note that we do not wait for the brightness ramp animation to complete before
1055 // reporting the display is ready because we only need to ensure the screen is in the
1056 // right power state even as it continues to converge on the desired brightness.
Narayan Kamath38819982017-07-06 18:15:30 +01001057 final boolean ready = mPendingScreenOnUnblocker == null &&
1058 (!mColorFadeEnabled ||
1059 (!mColorFadeOnAnimator.isStarted() && !mColorFadeOffAnimator.isStarted()))
Jeff Brown0a434772014-09-30 14:42:27 -07001060 && mPowerState.waitUntilClean(mCleanListener);
1061 final boolean finished = ready
1062 && !mScreenBrightnessRampAnimator.isAnimating();
1063
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001064 // Notify policy about screen turned on.
1065 if (ready && state != Display.STATE_OFF
1066 && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_ON) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001067 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_ON);
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001068 mWindowManagerPolicy.screenTurnedOn();
1069 }
1070
Jeff Brown0a434772014-09-30 14:42:27 -07001071 // Grab a wake lock if we have unfinished business.
1072 if (!finished && !mUnfinishedBusiness) {
1073 if (DEBUG) {
1074 Slog.d(TAG, "Unfinished business...");
1075 }
1076 mCallbacks.acquireSuspendBlocker();
1077 mUnfinishedBusiness = true;
1078 }
1079
1080 // Notify the power manager when ready.
1081 if (ready && mustNotify) {
1082 // Send state change.
Jeff Brown96307042012-07-27 15:51:34 -07001083 synchronized (mLock) {
1084 if (!mPendingRequestChangedLocked) {
1085 mDisplayReadyLocked = true;
Jeff Brownc38c9be2012-10-04 13:16:19 -07001086
1087 if (DEBUG) {
1088 Slog.d(TAG, "Display ready!");
1089 }
Jeff Brown96307042012-07-27 15:51:34 -07001090 }
1091 }
Jeff Brownec083212013-09-11 20:45:25 -07001092 sendOnStateChangedWithWakelock();
Jeff Brown96307042012-07-27 15:51:34 -07001093 }
Jeff Brown0a434772014-09-30 14:42:27 -07001094
1095 // Release the wake lock when we have no unfinished business.
1096 if (finished && mUnfinishedBusiness) {
1097 if (DEBUG) {
1098 Slog.d(TAG, "Finished business...");
1099 }
1100 mUnfinishedBusiness = false;
1101 mCallbacks.releaseSuspendBlocker();
1102 }
Julius D'souzad5105dd2017-06-02 11:03:53 -07001103
1104 // Record if dozing for future comparison.
1105 mDozing = state != Display.STATE_ON;
Jeff Brown96307042012-07-27 15:51:34 -07001106 }
1107
Michael Wright639c8be2014-01-17 18:29:12 -08001108 @Override
1109 public void updateBrightness() {
1110 sendUpdatePowerState();
1111 }
1112
Michael Wrighteef0e132017-11-21 17:57:52 +00001113 public void setBrightnessConfiguration(BrightnessConfiguration c) {
1114 Message msg = mHandler.obtainMessage(MSG_CONFIGURE_BRIGHTNESS, c);
1115 msg.sendToTarget();
1116 }
1117
Michael Wrightd8460232018-01-16 18:04:59 +00001118 public void setTemporaryBrightness(int brightness) {
1119 Message msg = mHandler.obtainMessage(MSG_SET_TEMPORARY_BRIGHTNESS,
1120 brightness, 0 /*unused*/);
1121 msg.sendToTarget();
1122 }
1123
1124 public void setTemporaryAutoBrightnessAdjustment(float adjustment) {
1125 Message msg = mHandler.obtainMessage(MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT,
1126 Float.floatToIntBits(adjustment), 0 /*unused*/);
1127 msg.sendToTarget();
1128 }
1129
Jeff Brown8b9cf1c2012-10-07 14:54:17 -07001130 private void blockScreenOn() {
Jeff Brown3ee549c2014-09-22 20:14:39 -07001131 if (mPendingScreenOnUnblocker == null) {
Jeff Brown3edf5272014-08-14 19:25:14 -07001132 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001133 mPendingScreenOnUnblocker = new ScreenOnUnblocker();
Jeff Brown037c33e2014-04-09 00:31:55 -07001134 mScreenOnBlockStartRealTime = SystemClock.elapsedRealtime();
Jeff Brown3edf5272014-08-14 19:25:14 -07001135 Slog.i(TAG, "Blocking screen on until initial contents have been drawn.");
Jeff Brown8b9cf1c2012-10-07 14:54:17 -07001136 }
1137 }
1138
1139 private void unblockScreenOn() {
Jeff Brown3ee549c2014-09-22 20:14:39 -07001140 if (mPendingScreenOnUnblocker != null) {
1141 mPendingScreenOnUnblocker = null;
Jeff Brown2d8a3902014-03-11 23:02:35 -07001142 long delay = SystemClock.elapsedRealtime() - mScreenOnBlockStartRealTime;
Jeff Brown3edf5272014-08-14 19:25:14 -07001143 Slog.i(TAG, "Unblocked screen on after " + delay + " ms");
1144 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0);
Jeff Brown8b9cf1c2012-10-07 14:54:17 -07001145 }
1146 }
1147
Jorim Jaggi51304d72017-05-17 17:25:32 +02001148 private void blockScreenOff() {
1149 if (mPendingScreenOffUnblocker == null) {
1150 Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_OFF_BLOCKED_TRACE_NAME, 0);
1151 mPendingScreenOffUnblocker = new ScreenOffUnblocker();
1152 mScreenOffBlockStartRealTime = SystemClock.elapsedRealtime();
1153 Slog.i(TAG, "Blocking screen off");
1154 }
1155 }
1156
1157 private void unblockScreenOff() {
1158 if (mPendingScreenOffUnblocker != null) {
1159 mPendingScreenOffUnblocker = null;
1160 long delay = SystemClock.elapsedRealtime() - mScreenOffBlockStartRealTime;
1161 Slog.i(TAG, "Unblocked screen off after " + delay + " ms");
1162 Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_OFF_BLOCKED_TRACE_NAME, 0);
1163 }
1164 }
1165
Jeff Brown3ee549c2014-09-22 20:14:39 -07001166 private boolean setScreenState(int state) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001167 return setScreenState(state, false /*reportOnly*/);
Michael Wright05e76fe2017-07-20 18:18:33 +01001168 }
1169
Michael Wrightc3e6af82017-07-25 22:31:03 +01001170 private boolean setScreenState(int state, boolean reportOnly) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02001171 final boolean isOff = (state == Display.STATE_OFF);
Jeff Brown037c33e2014-04-09 00:31:55 -07001172 if (mPowerState.getScreenState() != state) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02001173
1174 // If we are trying to turn screen off, give policy a chance to do something before we
1175 // actually turn the screen off.
1176 if (isOff && !mScreenOffBecauseOfProximity) {
1177 if (mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_ON) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001178 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_TURNING_OFF);
Jorim Jaggi51304d72017-05-17 17:25:32 +02001179 blockScreenOff();
1180 mWindowManagerPolicy.screenTurningOff(mPendingScreenOffUnblocker);
Michael Wrightc3e6af82017-07-25 22:31:03 +01001181 unblockScreenOff();
Jorim Jaggi51304d72017-05-17 17:25:32 +02001182 } else if (mPendingScreenOffUnblocker != null) {
Jorim Jaggi51304d72017-05-17 17:25:32 +02001183 // Abort doing the state change until screen off is unblocked.
1184 return false;
1185 }
1186 }
1187
Michael Wrightc3e6af82017-07-25 22:31:03 +01001188 if (!reportOnly) {
Michael Wrightb0a1d3d2017-09-22 15:05:02 +01001189 Trace.traceCounter(Trace.TRACE_TAG_POWER, "ScreenState", state);
Michael Wrightc3e6af82017-07-25 22:31:03 +01001190 mPowerState.setScreenState(state);
1191 // Tell battery stats about the transition.
1192 try {
1193 mBatteryStats.noteScreenState(state);
1194 } catch (RemoteException ex) {
1195 // same process
1196 }
Jeff Brown96307042012-07-27 15:51:34 -07001197 }
1198 }
Jeff Browne1633ad2015-06-22 19:24:24 -07001199
1200 // Tell the window manager policy when the screen is turned off or on unless it's due
1201 // to the proximity sensor. We temporarily block turning the screen on until the
1202 // window manager is ready by leaving a black surface covering the screen.
1203 // This surface is essentially the final state of the color fade animation and
1204 // it is only removed once the window manager tells us that the activity has
1205 // finished drawing underneath.
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001206 if (isOff && mReportedScreenStateToPolicy != REPORTED_TO_POLICY_SCREEN_OFF
1207 && !mScreenOffBecauseOfProximity) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001208 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_OFF);
Jeff Browne1633ad2015-06-22 19:24:24 -07001209 unblockScreenOn();
1210 mWindowManagerPolicy.screenTurnedOff();
Jorim Jaggi51304d72017-05-17 17:25:32 +02001211 } else if (!isOff
1212 && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_OFF) {
1213
1214 // We told policy already that screen was turning off, but now we changed our minds.
1215 // Complete the full state transition on -> turningOff -> off.
1216 unblockScreenOff();
1217 mWindowManagerPolicy.screenTurnedOff();
Michael Wrightc3e6af82017-07-25 22:31:03 +01001218 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_OFF);
Jorim Jaggi51304d72017-05-17 17:25:32 +02001219 }
1220 if (!isOff && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_OFF) {
Michael Wrightc3e6af82017-07-25 22:31:03 +01001221 setReportedScreenState(REPORTED_TO_POLICY_SCREEN_TURNING_ON);
Jeff Browne1633ad2015-06-22 19:24:24 -07001222 if (mPowerState.getColorFadeLevel() == 0.0f) {
1223 blockScreenOn();
1224 } else {
1225 unblockScreenOn();
1226 }
1227 mWindowManagerPolicy.screenTurningOn(mPendingScreenOnUnblocker);
1228 }
1229
1230 // Return true if the screen isn't blocked.
Jeff Brown3ee549c2014-09-22 20:14:39 -07001231 return mPendingScreenOnUnblocker == null;
Jeff Brown96307042012-07-27 15:51:34 -07001232 }
1233
Michael Wrightc3e6af82017-07-25 22:31:03 +01001234 private void setReportedScreenState(int state) {
1235 Trace.traceCounter(Trace.TRACE_TAG_POWER, "ReportedScreenStateToPolicy", state);
1236 mReportedScreenStateToPolicy = state;
1237 }
1238
Santos Cordonb12c7e12018-04-13 17:10:54 -07001239 private int clampScreenBrightnessForVr(int value) {
1240 return MathUtils.constrain(
1241 value, mScreenBrightnessForVrRangeMinimum, mScreenBrightnessForVrRangeMaximum);
1242 }
1243
Jeff Brown330560f2012-08-21 22:10:57 -07001244 private int clampScreenBrightness(int value) {
Michael Wright639c8be2014-01-17 18:29:12 -08001245 return MathUtils.constrain(
1246 value, mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum);
Jeff Brown330560f2012-08-21 22:10:57 -07001247 }
1248
Jeff Brown96307042012-07-27 15:51:34 -07001249 private void animateScreenBrightness(int target, int rate) {
Jeff Brown0a434772014-09-30 14:42:27 -07001250 if (DEBUG) {
1251 Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate);
1252 }
Jeff Brown96307042012-07-27 15:51:34 -07001253 if (mScreenBrightnessRampAnimator.animateTo(target, rate)) {
Michael Wrighta9f37ab2017-08-15 17:14:20 +01001254 Trace.traceCounter(Trace.TRACE_TAG_POWER, "TargetScreenBrightness", target);
Jeff Brown131206b2014-04-08 17:27:14 -07001255 try {
1256 mBatteryStats.noteScreenBrightness(target);
1257 } catch (RemoteException ex) {
1258 // same process
1259 }
Jeff Brown96307042012-07-27 15:51:34 -07001260 }
1261 }
1262
Jeff Brown606e4e82014-09-18 15:22:26 -07001263 private void animateScreenStateChange(int target, boolean performScreenOffTransition) {
1264 // If there is already an animation in progress, don't interfere with it.
Narayan Kamath38819982017-07-06 18:15:30 +01001265 if (mColorFadeEnabled &&
1266 (mColorFadeOnAnimator.isStarted() || mColorFadeOffAnimator.isStarted())) {
Chong Zhangb131c702016-02-16 16:31:48 -08001267 if (target != Display.STATE_ON) {
1268 return;
1269 }
1270 // If display state changed to on, proceed and stop the color fade and turn screen on.
1271 mPendingScreenOff = false;
Jeff Brown606e4e82014-09-18 15:22:26 -07001272 }
1273
Michael Wrightc3e6af82017-07-25 22:31:03 +01001274 if (mDisplayBlanksAfterDozeConfig
1275 && Display.isDozeState(mPowerState.getScreenState())
Michael Wright05e76fe2017-07-20 18:18:33 +01001276 && !Display.isDozeState(target)) {
Michael Wright05e76fe2017-07-20 18:18:33 +01001277 // Skip the screen off animation and add a black surface to hide the
Michael Wrightc3e6af82017-07-25 22:31:03 +01001278 // contents of the screen.
1279 mPowerState.prepareColorFade(mContext,
1280 mColorFadeFadesConfig ? ColorFade.MODE_FADE : ColorFade.MODE_WARM_UP);
Lucas Dupina84952b2017-08-15 15:56:50 -07001281 if (mColorFadeOffAnimator != null) {
1282 mColorFadeOffAnimator.end();
1283 }
Michael Wrightc3e6af82017-07-25 22:31:03 +01001284 // Some display hardware will blank itself on the transition between doze and non-doze
1285 // but still on display states. In this case we want to report to policy that the
1286 // display has turned off so it can prepare the appropriate power on animation, but we
1287 // don't want to actually transition to the fully off state since that takes
1288 // significantly longer to transition from.
1289 setScreenState(Display.STATE_OFF, target != Display.STATE_OFF /*reportOnly*/);
Michael Wright05e76fe2017-07-20 18:18:33 +01001290 }
1291
Jeff Brown3ee549c2014-09-22 20:14:39 -07001292 // If we were in the process of turning off the screen but didn't quite
1293 // finish. Then finish up now to prevent a jarring transition back
1294 // to screen on if we skipped blocking screen on as usual.
1295 if (mPendingScreenOff && target != Display.STATE_OFF) {
1296 setScreenState(Display.STATE_OFF);
1297 mPendingScreenOff = false;
Michael Lentined73854d2015-09-25 10:55:26 -07001298 mPowerState.dismissColorFadeResources();
Jeff Brown606e4e82014-09-18 15:22:26 -07001299 }
1300
1301 if (target == Display.STATE_ON) {
1302 // Want screen on. The contents of the screen may not yet
Jeff Brown3ee549c2014-09-22 20:14:39 -07001303 // be visible if the color fade has not been dismissed because
Jeff Brown606e4e82014-09-18 15:22:26 -07001304 // its last frame of animation is solid black.
Jeff Brown3ee549c2014-09-22 20:14:39 -07001305 if (!setScreenState(Display.STATE_ON)) {
1306 return; // screen on blocked
1307 }
Narayan Kamath38819982017-07-06 18:15:30 +01001308 if (USE_COLOR_FADE_ON_ANIMATION && mColorFadeEnabled && mPowerRequest.isBrightOrDim()) {
Jeff Brown606e4e82014-09-18 15:22:26 -07001309 // Perform screen on animation.
1310 if (mPowerState.getColorFadeLevel() == 1.0f) {
1311 mPowerState.dismissColorFade();
1312 } else if (mPowerState.prepareColorFade(mContext,
1313 mColorFadeFadesConfig ?
1314 ColorFade.MODE_FADE :
1315 ColorFade.MODE_WARM_UP)) {
1316 mColorFadeOnAnimator.start();
1317 } else {
1318 mColorFadeOnAnimator.end();
1319 }
1320 } else {
1321 // Skip screen on animation.
1322 mPowerState.setColorFadeLevel(1.0f);
1323 mPowerState.dismissColorFade();
1324 }
Santos Cordon3107d292016-09-20 15:50:35 -07001325 } else if (target == Display.STATE_VR) {
1326 // Wait for brightness animation to complete beforehand when entering VR
1327 // from screen on to prevent a perceptible jump because brightness may operate
1328 // differently when the display is configured for dozing.
1329 if (mScreenBrightnessRampAnimator.isAnimating()
1330 && mPowerState.getScreenState() == Display.STATE_ON) {
1331 return;
1332 }
1333
1334 // Set screen state.
1335 if (!setScreenState(Display.STATE_VR)) {
1336 return; // screen on blocked
1337 }
1338
1339 // Dismiss the black surface without fanfare.
1340 mPowerState.setColorFadeLevel(1.0f);
1341 mPowerState.dismissColorFade();
Jeff Brown606e4e82014-09-18 15:22:26 -07001342 } else if (target == Display.STATE_DOZE) {
1343 // Want screen dozing.
1344 // Wait for brightness animation to complete beforehand when entering doze
1345 // from screen on to prevent a perceptible jump because brightness may operate
1346 // differently when the display is configured for dozing.
1347 if (mScreenBrightnessRampAnimator.isAnimating()
1348 && mPowerState.getScreenState() == Display.STATE_ON) {
1349 return;
1350 }
1351
Jeff Brown3ee549c2014-09-22 20:14:39 -07001352 // Set screen state.
1353 if (!setScreenState(Display.STATE_DOZE)) {
1354 return; // screen on blocked
1355 }
1356
1357 // Dismiss the black surface without fanfare.
Jeff Brown606e4e82014-09-18 15:22:26 -07001358 mPowerState.setColorFadeLevel(1.0f);
1359 mPowerState.dismissColorFade();
1360 } else if (target == Display.STATE_DOZE_SUSPEND) {
1361 // Want screen dozing and suspended.
1362 // Wait for brightness animation to complete beforehand unless already
1363 // suspended because we may not be able to change it after suspension.
1364 if (mScreenBrightnessRampAnimator.isAnimating()
1365 && mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) {
1366 return;
1367 }
1368
Jeff Brown3ee549c2014-09-22 20:14:39 -07001369 // If not already suspending, temporarily set the state to doze until the
1370 // screen on is unblocked, then suspend.
1371 if (mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) {
1372 if (!setScreenState(Display.STATE_DOZE)) {
1373 return; // screen on blocked
1374 }
1375 setScreenState(Display.STATE_DOZE_SUSPEND); // already on so can't block
1376 }
1377
1378 // Dismiss the black surface without fanfare.
Jeff Brown606e4e82014-09-18 15:22:26 -07001379 mPowerState.setColorFadeLevel(1.0f);
1380 mPowerState.dismissColorFade();
Chris Phoenix10a4a642017-09-25 13:21:00 -07001381 } else if (target == Display.STATE_ON_SUSPEND) {
1382 // Want screen full-power and suspended.
1383 // Wait for brightness animation to complete beforehand unless already
1384 // suspended because we may not be able to change it after suspension.
1385 if (mScreenBrightnessRampAnimator.isAnimating()
1386 && mPowerState.getScreenState() != Display.STATE_ON_SUSPEND) {
1387 return;
1388 }
1389
1390 // If not already suspending, temporarily set the state to on until the
1391 // screen on is unblocked, then suspend.
1392 if (mPowerState.getScreenState() != Display.STATE_ON_SUSPEND) {
1393 if (!setScreenState(Display.STATE_ON)) {
1394 return;
1395 }
1396 setScreenState(Display.STATE_ON_SUSPEND);
1397 }
1398
1399 // Dismiss the black surface without fanfare.
1400 mPowerState.setColorFadeLevel(1.0f);
1401 mPowerState.dismissColorFade();
Jeff Brown606e4e82014-09-18 15:22:26 -07001402 } else {
1403 // Want screen off.
Jeff Brown3ee549c2014-09-22 20:14:39 -07001404 mPendingScreenOff = true;
Narayan Kamath38819982017-07-06 18:15:30 +01001405 if (!mColorFadeEnabled) {
1406 mPowerState.setColorFadeLevel(0.0f);
1407 }
1408
Jeff Brown606e4e82014-09-18 15:22:26 -07001409 if (mPowerState.getColorFadeLevel() == 0.0f) {
1410 // Turn the screen off.
1411 // A black surface is already hiding the contents of the screen.
1412 setScreenState(Display.STATE_OFF);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001413 mPendingScreenOff = false;
Michael Lentined73854d2015-09-25 10:55:26 -07001414 mPowerState.dismissColorFadeResources();
Jeff Brown606e4e82014-09-18 15:22:26 -07001415 } else if (performScreenOffTransition
1416 && mPowerState.prepareColorFade(mContext,
1417 mColorFadeFadesConfig ?
1418 ColorFade.MODE_FADE : ColorFade.MODE_COOL_DOWN)
1419 && mPowerState.getScreenState() != Display.STATE_OFF) {
1420 // Perform the screen off animation.
1421 mColorFadeOffAnimator.start();
1422 } else {
1423 // Skip the screen off animation and add a black surface to hide the
1424 // contents of the screen.
1425 mColorFadeOffAnimator.end();
1426 }
1427 }
1428 }
1429
Jeff Brown96307042012-07-27 15:51:34 -07001430 private final Runnable mCleanListener = new Runnable() {
1431 @Override
1432 public void run() {
1433 sendUpdatePowerState();
1434 }
1435 };
1436
1437 private void setProximitySensorEnabled(boolean enable) {
1438 if (enable) {
1439 if (!mProximitySensorEnabled) {
Jeff Brownec083212013-09-11 20:45:25 -07001440 // Register the listener.
1441 // Proximity sensor state already cleared initially.
Jeff Brown96307042012-07-27 15:51:34 -07001442 mProximitySensorEnabled = true;
Jeff Brown96307042012-07-27 15:51:34 -07001443 mSensorManager.registerListener(mProximitySensorListener, mProximitySensor,
1444 SensorManager.SENSOR_DELAY_NORMAL, mHandler);
1445 }
1446 } else {
1447 if (mProximitySensorEnabled) {
Jeff Brownec083212013-09-11 20:45:25 -07001448 // Unregister the listener.
1449 // Clear the proximity sensor state for next time.
Jeff Brown96307042012-07-27 15:51:34 -07001450 mProximitySensorEnabled = false;
1451 mProximity = PROXIMITY_UNKNOWN;
Jeff Brownec083212013-09-11 20:45:25 -07001452 mPendingProximity = PROXIMITY_UNKNOWN;
Jeff Brown96307042012-07-27 15:51:34 -07001453 mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
1454 mSensorManager.unregisterListener(mProximitySensorListener);
Jeff Brownec083212013-09-11 20:45:25 -07001455 clearPendingProximityDebounceTime(); // release wake lock (must be last)
Jeff Brown96307042012-07-27 15:51:34 -07001456 }
1457 }
1458 }
1459
1460 private void handleProximitySensorEvent(long time, boolean positive) {
Jeff Brownec083212013-09-11 20:45:25 -07001461 if (mProximitySensorEnabled) {
1462 if (mPendingProximity == PROXIMITY_NEGATIVE && !positive) {
1463 return; // no change
1464 }
1465 if (mPendingProximity == PROXIMITY_POSITIVE && positive) {
1466 return; // no change
1467 }
Jeff Brown96307042012-07-27 15:51:34 -07001468
Jeff Brownec083212013-09-11 20:45:25 -07001469 // Only accept a proximity sensor reading if it remains
1470 // stable for the entire debounce delay. We hold a wake lock while
1471 // debouncing the sensor.
1472 mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED);
1473 if (positive) {
1474 mPendingProximity = PROXIMITY_POSITIVE;
1475 setPendingProximityDebounceTime(
1476 time + PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY); // acquire wake lock
1477 } else {
1478 mPendingProximity = PROXIMITY_NEGATIVE;
1479 setPendingProximityDebounceTime(
1480 time + PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY); // acquire wake lock
1481 }
1482
1483 // Debounce the new sensor reading.
1484 debounceProximitySensor();
Jeff Brown93cbbb22012-10-04 13:18:36 -07001485 }
Jeff Brown96307042012-07-27 15:51:34 -07001486 }
1487
1488 private void debounceProximitySensor() {
Jeff Brownec083212013-09-11 20:45:25 -07001489 if (mProximitySensorEnabled
1490 && mPendingProximity != PROXIMITY_UNKNOWN
1491 && mPendingProximityDebounceTime >= 0) {
Jeff Brown96307042012-07-27 15:51:34 -07001492 final long now = SystemClock.uptimeMillis();
1493 if (mPendingProximityDebounceTime <= now) {
Jeff Brownec083212013-09-11 20:45:25 -07001494 // Sensor reading accepted. Apply the change then release the wake lock.
Jeff Brown96307042012-07-27 15:51:34 -07001495 mProximity = mPendingProximity;
Jeff Brownec083212013-09-11 20:45:25 -07001496 updatePowerState();
1497 clearPendingProximityDebounceTime(); // release wake lock (must be last)
Jeff Brown96307042012-07-27 15:51:34 -07001498 } else {
Jeff Brownec083212013-09-11 20:45:25 -07001499 // Need to wait a little longer.
1500 // Debounce again later. We continue holding a wake lock while waiting.
Jeff Brown96307042012-07-27 15:51:34 -07001501 Message msg = mHandler.obtainMessage(MSG_PROXIMITY_SENSOR_DEBOUNCED);
Jeff Brown96307042012-07-27 15:51:34 -07001502 mHandler.sendMessageAtTime(msg, mPendingProximityDebounceTime);
1503 }
1504 }
1505 }
1506
Jeff Brownec083212013-09-11 20:45:25 -07001507 private void clearPendingProximityDebounceTime() {
1508 if (mPendingProximityDebounceTime >= 0) {
1509 mPendingProximityDebounceTime = -1;
Jeff Brown131206b2014-04-08 17:27:14 -07001510 mCallbacks.releaseSuspendBlocker(); // release wake lock
Jeff Brownec083212013-09-11 20:45:25 -07001511 }
1512 }
1513
1514 private void setPendingProximityDebounceTime(long debounceTime) {
1515 if (mPendingProximityDebounceTime < 0) {
Jeff Brown131206b2014-04-08 17:27:14 -07001516 mCallbacks.acquireSuspendBlocker(); // acquire wake lock
Jeff Brownec083212013-09-11 20:45:25 -07001517 }
1518 mPendingProximityDebounceTime = debounceTime;
1519 }
1520
Jeff Brownec083212013-09-11 20:45:25 -07001521 private void sendOnStateChangedWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001522 mCallbacks.acquireSuspendBlocker();
1523 mHandler.post(mOnStateChangedRunnable);
Jeff Brown96307042012-07-27 15:51:34 -07001524 }
1525
Kenny Guyecc978f2018-03-14 17:30:20 +00001526 private void handleSettingsChange(boolean userSwitch) {
Michael Wrightd8460232018-01-16 18:04:59 +00001527 mPendingScreenBrightnessSetting = getScreenBrightnessSetting();
Kenny Guyecc978f2018-03-14 17:30:20 +00001528 if (userSwitch) {
1529 // Don't treat user switches as user initiated change.
1530 mCurrentScreenBrightnessSetting = mPendingScreenBrightnessSetting;
Dan Gittikc1352252018-04-27 17:48:31 +01001531 if (mAutomaticBrightnessController != null) {
1532 mAutomaticBrightnessController.resetShortTermModel();
1533 }
Kenny Guyecc978f2018-03-14 17:30:20 +00001534 }
Michael Wrightd8460232018-01-16 18:04:59 +00001535 mPendingAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting();
1536 // We don't bother with a pending variable for VR screen brightness since we just
1537 // immediately adapt to it.
1538 mScreenBrightnessForVr = getScreenBrightnessForVrSetting();
1539 sendUpdatePowerState();
1540 }
1541
1542 private float getAutoBrightnessAdjustmentSetting() {
1543 final float adj = Settings.System.getFloatForUser(mContext.getContentResolver(),
1544 Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.0f, UserHandle.USER_CURRENT);
1545 return Float.isNaN(adj) ? 0.0f : clampAutoBrightnessAdjustment(adj);
1546 }
1547
1548 private int getScreenBrightnessSetting() {
1549 final int brightness = Settings.System.getIntForUser(mContext.getContentResolver(),
1550 Settings.System.SCREEN_BRIGHTNESS, mScreenBrightnessDefault,
1551 UserHandle.USER_CURRENT);
1552 return clampAbsoluteBrightness(brightness);
1553 }
1554
1555 private int getScreenBrightnessForVrSetting() {
1556 final int brightness = Settings.System.getIntForUser(mContext.getContentResolver(),
1557 Settings.System.SCREEN_BRIGHTNESS_FOR_VR, mScreenBrightnessForVrDefault,
1558 UserHandle.USER_CURRENT);
Santos Cordonb12c7e12018-04-13 17:10:54 -07001559 return clampScreenBrightnessForVr(brightness);
Michael Wrightd8460232018-01-16 18:04:59 +00001560 }
1561
1562 private void putScreenBrightnessSetting(int brightness) {
1563 mCurrentScreenBrightnessSetting = brightness;
1564 Settings.System.putIntForUser(mContext.getContentResolver(),
Michael Wright617564f2018-01-25 22:20:54 +00001565 Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT);
1566 }
1567
1568 private void putAutoBrightnessAdjustmentSetting(float adjustment) {
1569 mAutoBrightnessAdjustment = adjustment;
1570 Settings.System.putFloatForUser(mContext.getContentResolver(),
1571 Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, adjustment, UserHandle.USER_CURRENT);
Michael Wrightd8460232018-01-16 18:04:59 +00001572 }
1573
1574 private boolean updateAutoBrightnessAdjustment() {
1575 if (Float.isNaN(mPendingAutoBrightnessAdjustment)) {
1576 return false;
1577 }
1578 if (mAutoBrightnessAdjustment == mPendingAutoBrightnessAdjustment) {
Michael Wright9fdf66b2018-05-22 15:35:05 +01001579 mPendingAutoBrightnessAdjustment = Float.NaN;
Michael Wrightd8460232018-01-16 18:04:59 +00001580 return false;
1581 }
1582 mAutoBrightnessAdjustment = mPendingAutoBrightnessAdjustment;
1583 mPendingAutoBrightnessAdjustment = Float.NaN;
1584 return true;
1585 }
1586
1587 private boolean updateUserSetScreenBrightness() {
1588 if (mPendingScreenBrightnessSetting < 0) {
1589 return false;
1590 }
1591 if (mCurrentScreenBrightnessSetting == mPendingScreenBrightnessSetting) {
Michael Wright44dc8b12018-01-25 23:25:20 +00001592 mPendingScreenBrightnessSetting = -1;
Dan Gittik3d2c5af2018-07-12 14:22:08 +01001593 mTemporaryScreenBrightness = -1;
Michael Wrightd8460232018-01-16 18:04:59 +00001594 return false;
1595 }
Kenny Guyad9a6ea2018-01-31 15:57:15 +00001596 mCurrentScreenBrightnessSetting = mPendingScreenBrightnessSetting;
Michael Wrightd8460232018-01-16 18:04:59 +00001597 mLastUserSetScreenBrightness = mPendingScreenBrightnessSetting;
1598 mPendingScreenBrightnessSetting = -1;
Dan Gittik3d2c5af2018-07-12 14:22:08 +01001599 mTemporaryScreenBrightness = -1;
Michael Wrightd8460232018-01-16 18:04:59 +00001600 return true;
1601 }
1602
Kenny Guy53d06612018-01-30 14:19:13 +00001603 private void notifyBrightnessChanged(int brightness, boolean userInitiated,
1604 boolean hadUserDataPoint) {
Michael Wrightd8460232018-01-16 18:04:59 +00001605 final float brightnessInNits = convertToNits(brightness);
Kenny Guyad9a6ea2018-01-31 15:57:15 +00001606 if (mPowerRequest.useAutoBrightness && brightnessInNits >= 0.0f
1607 && mAutomaticBrightnessController != null) {
Michael Wrightd8460232018-01-16 18:04:59 +00001608 // We only want to track changes on devices that can actually map the display backlight
1609 // values into a physical brightness unit since the value provided by the API is in
1610 // nits and not using the arbitrary backlight units.
Kenny Guy53d06612018-01-30 14:19:13 +00001611 final float powerFactor = mPowerRequest.lowPowerMode
1612 ? mPowerRequest.screenLowPowerBrightnessFactor
1613 : 1.0f;
1614 mBrightnessTracker.notifyBrightnessChanged(brightnessInNits, userInitiated,
1615 powerFactor, hadUserDataPoint,
1616 mAutomaticBrightnessController.isDefaultConfig());
Michael Wrightd8460232018-01-16 18:04:59 +00001617 }
1618 }
1619
1620 private float convertToNits(int backlight) {
Michael Wright144aac92017-12-21 18:37:41 +00001621 if (mBrightnessMapper != null) {
Michael Wrightd8460232018-01-16 18:04:59 +00001622 return mBrightnessMapper.convertToNits(backlight);
Michael Wright144aac92017-12-21 18:37:41 +00001623 } else {
1624 return -1.0f;
1625 }
1626 }
1627
Jeff Brown96307042012-07-27 15:51:34 -07001628 private final Runnable mOnStateChangedRunnable = new Runnable() {
1629 @Override
1630 public void run() {
1631 mCallbacks.onStateChanged();
Jeff Brown131206b2014-04-08 17:27:14 -07001632 mCallbacks.releaseSuspendBlocker();
Jeff Brown96307042012-07-27 15:51:34 -07001633 }
1634 };
1635
Jeff Brownec083212013-09-11 20:45:25 -07001636 private void sendOnProximityPositiveWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001637 mCallbacks.acquireSuspendBlocker();
1638 mHandler.post(mOnProximityPositiveRunnable);
Jeff Brown93cbbb22012-10-04 13:18:36 -07001639 }
1640
1641 private final Runnable mOnProximityPositiveRunnable = new Runnable() {
1642 @Override
1643 public void run() {
1644 mCallbacks.onProximityPositive();
Jeff Brown131206b2014-04-08 17:27:14 -07001645 mCallbacks.releaseSuspendBlocker();
Jeff Brown93cbbb22012-10-04 13:18:36 -07001646 }
1647 };
1648
Jeff Brownec083212013-09-11 20:45:25 -07001649 private void sendOnProximityNegativeWithWakelock() {
Jeff Brown131206b2014-04-08 17:27:14 -07001650 mCallbacks.acquireSuspendBlocker();
1651 mHandler.post(mOnProximityNegativeRunnable);
Jeff Brown96307042012-07-27 15:51:34 -07001652 }
1653
1654 private final Runnable mOnProximityNegativeRunnable = new Runnable() {
1655 @Override
1656 public void run() {
1657 mCallbacks.onProximityNegative();
Jeff Brown131206b2014-04-08 17:27:14 -07001658 mCallbacks.releaseSuspendBlocker();
Jeff Brown96307042012-07-27 15:51:34 -07001659 }
1660 };
1661
Jeff Brownbd6e1502012-08-28 03:27:37 -07001662 public void dump(final PrintWriter pw) {
Jeff Brown96307042012-07-27 15:51:34 -07001663 synchronized (mLock) {
1664 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001665 pw.println("Display Power Controller Locked State:");
Jeff Brown96307042012-07-27 15:51:34 -07001666 pw.println(" mDisplayReadyLocked=" + mDisplayReadyLocked);
1667 pw.println(" mPendingRequestLocked=" + mPendingRequestLocked);
1668 pw.println(" mPendingRequestChangedLocked=" + mPendingRequestChangedLocked);
1669 pw.println(" mPendingWaitForNegativeProximityLocked="
1670 + mPendingWaitForNegativeProximityLocked);
1671 pw.println(" mPendingUpdatePowerStateLocked=" + mPendingUpdatePowerStateLocked);
1672 }
1673
1674 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001675 pw.println("Display Power Controller Configuration:");
Jeff Brown26875502014-01-30 21:47:47 -08001676 pw.println(" mScreenBrightnessDozeConfig=" + mScreenBrightnessDozeConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001677 pw.println(" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig);
Jeff Brownb76eebff2012-10-05 22:26:44 -07001678 pw.println(" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum);
1679 pw.println(" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum);
Michael Wright2155bc22018-05-01 00:38:32 +01001680 pw.println(" mScreenBrightnessDefault=" + mScreenBrightnessDefault);
1681 pw.println(" mScreenBrightnessForVrRangeMinimum=" + mScreenBrightnessForVrRangeMinimum);
1682 pw.println(" mScreenBrightnessForVrRangeMaximum=" + mScreenBrightnessForVrRangeMaximum);
1683 pw.println(" mScreenBrightnessForVrDefault=" + mScreenBrightnessForVrDefault);
Jeff Brown2175e9c2014-09-12 16:11:07 -07001684 pw.println(" mUseSoftwareAutoBrightnessConfig=" + mUseSoftwareAutoBrightnessConfig);
Filip Gruszczynskia15aa7d2014-10-28 14:12:40 -07001685 pw.println(" mAllowAutoBrightnessWhileDozingConfig=" +
1686 mAllowAutoBrightnessWhileDozingConfig);
Michael Wright2155bc22018-05-01 00:38:32 +01001687 pw.println(" mBrightnessRampRateFast=" + mBrightnessRampRateFast);
1688 pw.println(" mBrightnessRampRateSlow=" + mBrightnessRampRateSlow);
1689 pw.println(" mSkipScreenOnBrightnessRamp=" + mSkipScreenOnBrightnessRamp);
Jeff Brown2175e9c2014-09-12 16:11:07 -07001690 pw.println(" mColorFadeFadesConfig=" + mColorFadeFadesConfig);
Michael Wright2155bc22018-05-01 00:38:32 +01001691 pw.println(" mColorFadeEnabled=" + mColorFadeEnabled);
1692 pw.println(" mDisplayBlanksAfterDozeConfig=" + mDisplayBlanksAfterDozeConfig);
1693 pw.println(" mBrightnessBucketsInDozeConfig=" + mBrightnessBucketsInDozeConfig);
Jeff Brown96307042012-07-27 15:51:34 -07001694
Jeff Brownbd6e1502012-08-28 03:27:37 -07001695 mHandler.runWithScissors(new Runnable() {
1696 @Override
1697 public void run() {
1698 dumpLocal(pw);
Jeff Brown96307042012-07-27 15:51:34 -07001699 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -07001700 }, 1000);
Jeff Brown96307042012-07-27 15:51:34 -07001701 }
1702
1703 private void dumpLocal(PrintWriter pw) {
1704 pw.println();
Jeff Brown131206b2014-04-08 17:27:14 -07001705 pw.println("Display Power Controller Thread State:");
Jeff Brown96307042012-07-27 15:51:34 -07001706 pw.println(" mPowerRequest=" + mPowerRequest);
Michael Wright2155bc22018-05-01 00:38:32 +01001707 pw.println(" mUnfinishedBusiness=" + mUnfinishedBusiness);
Jeff Brown96307042012-07-27 15:51:34 -07001708 pw.println(" mWaitingForNegativeProximity=" + mWaitingForNegativeProximity);
Jeff Brown96307042012-07-27 15:51:34 -07001709 pw.println(" mProximitySensor=" + mProximitySensor);
1710 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1711 pw.println(" mProximityThreshold=" + mProximityThreshold);
1712 pw.println(" mProximity=" + proximityToString(mProximity));
1713 pw.println(" mPendingProximity=" + proximityToString(mPendingProximity));
1714 pw.println(" mPendingProximityDebounceTime="
1715 + TimeUtils.formatUptime(mPendingProximityDebounceTime));
1716 pw.println(" mScreenOffBecauseOfProximity=" + mScreenOffBecauseOfProximity);
Michael Wrightd8460232018-01-16 18:04:59 +00001717 pw.println(" mLastUserSetScreenBrightness=" + mLastUserSetScreenBrightness);
1718 pw.println(" mCurrentScreenBrightnessSetting=" + mCurrentScreenBrightnessSetting);
1719 pw.println(" mPendingScreenBrightnessSetting=" + mPendingScreenBrightnessSetting);
Michael Wright2155bc22018-05-01 00:38:32 +01001720 pw.println(" mTemporaryScreenBrightness=" + mTemporaryScreenBrightness);
Michael Wrightd8460232018-01-16 18:04:59 +00001721 pw.println(" mAutoBrightnessAdjustment=" + mAutoBrightnessAdjustment);
Michael Wright2155bc22018-05-01 00:38:32 +01001722 pw.println(" mTemporaryAutoBrightnessAdjustment=" + mTemporaryAutoBrightnessAdjustment);
Michael Wrightd8460232018-01-16 18:04:59 +00001723 pw.println(" mPendingAutoBrightnessAdjustment=" + mPendingAutoBrightnessAdjustment);
Michael Wright2155bc22018-05-01 00:38:32 +01001724 pw.println(" mScreenBrightnessForVr=" + mScreenBrightnessForVr);
Jeff Brown970d4132014-07-19 11:33:47 -07001725 pw.println(" mAppliedAutoBrightness=" + mAppliedAutoBrightness);
1726 pw.println(" mAppliedDimming=" + mAppliedDimming);
1727 pw.println(" mAppliedLowPower=" + mAppliedLowPower);
Michael Wright2155bc22018-05-01 00:38:32 +01001728 pw.println(" mAppliedScreenBrightnessOverride=" + mAppliedScreenBrightnessOverride);
1729 pw.println(" mAppliedTemporaryBrightness=" + mAppliedTemporaryBrightness);
1730 pw.println(" mDozing=" + mDozing);
1731 pw.println(" mSkipRampState=" + skipRampStateToString(mSkipRampState));
1732 pw.println(" mInitialAutoBrightness=" + mInitialAutoBrightness);
1733 pw.println(" mScreenOnBlockStartRealTime=" + mScreenOnBlockStartRealTime);
1734 pw.println(" mScreenOffBlockStartRealTime=" + mScreenOffBlockStartRealTime);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001735 pw.println(" mPendingScreenOnUnblocker=" + mPendingScreenOnUnblocker);
Michael Wright2155bc22018-05-01 00:38:32 +01001736 pw.println(" mPendingScreenOffUnblocker=" + mPendingScreenOffUnblocker);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001737 pw.println(" mPendingScreenOff=" + mPendingScreenOff);
Michael Wrightc3e6af82017-07-25 22:31:03 +01001738 pw.println(" mReportedToPolicy=" +
1739 reportedToPolicyToString(mReportedScreenStateToPolicy));
Jeff Brown96307042012-07-27 15:51:34 -07001740
Luis Hector Chavezb48942e2018-07-27 09:12:23 -07001741 if (mScreenBrightnessRampAnimator != null) {
1742 pw.println(" mScreenBrightnessRampAnimator.isAnimating()=" +
1743 mScreenBrightnessRampAnimator.isAnimating());
1744 }
Jeff Brown42558692014-05-20 22:02:46 -07001745
Michael Lentine0839adb2014-07-29 18:47:56 -07001746 if (mColorFadeOnAnimator != null) {
1747 pw.println(" mColorFadeOnAnimator.isStarted()=" +
1748 mColorFadeOnAnimator.isStarted());
Jeff Brown96307042012-07-27 15:51:34 -07001749 }
Michael Lentine0839adb2014-07-29 18:47:56 -07001750 if (mColorFadeOffAnimator != null) {
1751 pw.println(" mColorFadeOffAnimator.isStarted()=" +
1752 mColorFadeOffAnimator.isStarted());
Jeff Brown96307042012-07-27 15:51:34 -07001753 }
1754
1755 if (mPowerState != null) {
1756 mPowerState.dump(pw);
1757 }
Michael Wright639c8be2014-01-17 18:29:12 -08001758
1759 if (mAutomaticBrightnessController != null) {
1760 mAutomaticBrightnessController.dump(pw);
1761 }
1762
Michael Wright144aac92017-12-21 18:37:41 +00001763 if (mBrightnessTracker != null) {
1764 pw.println();
1765 mBrightnessTracker.dump(pw);
1766 }
Dan Gittik8dbd7e92018-12-03 15:35:53 +00001767
1768 pw.println();
1769 if (mDisplayWhiteBalanceController != null) {
1770 mDisplayWhiteBalanceController.dump(pw);
1771 mDisplayWhiteBalanceSettings.dump(pw);
1772 }
Jeff Brown96307042012-07-27 15:51:34 -07001773 }
1774
1775 private static String proximityToString(int state) {
1776 switch (state) {
1777 case PROXIMITY_UNKNOWN:
1778 return "Unknown";
1779 case PROXIMITY_NEGATIVE:
1780 return "Negative";
1781 case PROXIMITY_POSITIVE:
1782 return "Positive";
1783 default:
1784 return Integer.toString(state);
1785 }
1786 }
1787
Jorim Jaggi0d210f62015-07-10 14:24:44 -07001788 private static String reportedToPolicyToString(int state) {
1789 switch (state) {
1790 case REPORTED_TO_POLICY_SCREEN_OFF:
1791 return "REPORTED_TO_POLICY_SCREEN_OFF";
1792 case REPORTED_TO_POLICY_SCREEN_TURNING_ON:
1793 return "REPORTED_TO_POLICY_SCREEN_TURNING_ON";
1794 case REPORTED_TO_POLICY_SCREEN_ON:
1795 return "REPORTED_TO_POLICY_SCREEN_ON";
1796 default:
1797 return Integer.toString(state);
1798 }
1799 }
1800
Michael Wright2155bc22018-05-01 00:38:32 +01001801 private static String skipRampStateToString(int state) {
1802 switch (state) {
1803 case RAMP_STATE_SKIP_NONE:
1804 return "RAMP_STATE_SKIP_NONE";
1805 case RAMP_STATE_SKIP_INITIAL:
1806 return "RAMP_STATE_SKIP_INITIAL";
1807 case RAMP_STATE_SKIP_AUTOBRIGHT:
1808 return "RAMP_STATE_SKIP_AUTOBRIGHT";
1809 default:
1810 return Integer.toString(state);
1811 }
1812 }
1813
Michael Wright639c8be2014-01-17 18:29:12 -08001814 private static int clampAbsoluteBrightness(int value) {
1815 return MathUtils.constrain(value, PowerManager.BRIGHTNESS_OFF, PowerManager.BRIGHTNESS_ON);
Jeff Brown96307042012-07-27 15:51:34 -07001816 }
1817
Michael Wrightd8460232018-01-16 18:04:59 +00001818 private static float clampAutoBrightnessAdjustment(float value) {
1819 return MathUtils.constrain(value, -1.0f, 1.0f);
1820 }
1821
Jeff Brown96307042012-07-27 15:51:34 -07001822 private final class DisplayControllerHandler extends Handler {
1823 public DisplayControllerHandler(Looper looper) {
Jeff Browna2910d02012-08-25 12:29:46 -07001824 super(looper, null, true /*async*/);
Jeff Brown96307042012-07-27 15:51:34 -07001825 }
1826
1827 @Override
1828 public void handleMessage(Message msg) {
1829 switch (msg.what) {
1830 case MSG_UPDATE_POWER_STATE:
1831 updatePowerState();
1832 break;
1833
1834 case MSG_PROXIMITY_SENSOR_DEBOUNCED:
1835 debounceProximitySensor();
1836 break;
Jeff Brown3ee549c2014-09-22 20:14:39 -07001837
1838 case MSG_SCREEN_ON_UNBLOCKED:
1839 if (mPendingScreenOnUnblocker == msg.obj) {
1840 unblockScreenOn();
1841 updatePowerState();
1842 }
1843 break;
Jorim Jaggi51304d72017-05-17 17:25:32 +02001844 case MSG_SCREEN_OFF_UNBLOCKED:
1845 if (mPendingScreenOffUnblocker == msg.obj) {
1846 unblockScreenOff();
1847 updatePowerState();
1848 }
1849 break;
Michael Wrighteef0e132017-11-21 17:57:52 +00001850 case MSG_CONFIGURE_BRIGHTNESS:
Michael Wrightd5df3612018-01-02 12:44:52 +00001851 mBrightnessConfiguration = (BrightnessConfiguration)msg.obj;
Michael Wrighteef0e132017-11-21 17:57:52 +00001852 updatePowerState();
1853 break;
Michael Wrightd8460232018-01-16 18:04:59 +00001854
1855 case MSG_SET_TEMPORARY_BRIGHTNESS:
1856 // TODO: Should we have a a timeout for the temporary brightness?
1857 mTemporaryScreenBrightness = msg.arg1;
1858 updatePowerState();
1859 break;
1860
1861 case MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT:
1862 mTemporaryAutoBrightnessAdjustment = Float.intBitsToFloat(msg.arg1);
1863 updatePowerState();
1864 break;
Jeff Brown96307042012-07-27 15:51:34 -07001865 }
1866 }
1867 }
1868
1869 private final SensorEventListener mProximitySensorListener = new SensorEventListener() {
1870 @Override
1871 public void onSensorChanged(SensorEvent event) {
1872 if (mProximitySensorEnabled) {
1873 final long time = SystemClock.uptimeMillis();
1874 final float distance = event.values[0];
1875 boolean positive = distance >= 0.0f && distance < mProximityThreshold;
1876 handleProximitySensorEvent(time, positive);
1877 }
1878 }
1879
1880 @Override
1881 public void onAccuracyChanged(Sensor sensor, int accuracy) {
1882 // Not used.
1883 }
1884 };
Jeff Brown3ee549c2014-09-22 20:14:39 -07001885
Michael Wrightd8460232018-01-16 18:04:59 +00001886
1887 private final class SettingsObserver extends ContentObserver {
1888 public SettingsObserver(Handler handler) {
1889 super(handler);
1890 }
1891
1892 @Override
1893 public void onChange(boolean selfChange, Uri uri) {
Kenny Guyecc978f2018-03-14 17:30:20 +00001894 handleSettingsChange(false /* userSwitch */);
Michael Wrightd8460232018-01-16 18:04:59 +00001895 }
1896 }
1897
Jeff Brown3ee549c2014-09-22 20:14:39 -07001898 private final class ScreenOnUnblocker implements WindowManagerPolicy.ScreenOnListener {
1899 @Override
1900 public void onScreenOn() {
1901 Message msg = mHandler.obtainMessage(MSG_SCREEN_ON_UNBLOCKED, this);
Jeff Brown3ee549c2014-09-22 20:14:39 -07001902 mHandler.sendMessage(msg);
1903 }
1904 }
Jorim Jaggi51304d72017-05-17 17:25:32 +02001905
1906 private final class ScreenOffUnblocker implements WindowManagerPolicy.ScreenOffListener {
Jorim Jaggi51304d72017-05-17 17:25:32 +02001907 @Override
1908 public void onScreenOff() {
1909 Message msg = mHandler.obtainMessage(MSG_SCREEN_OFF_UNBLOCKED, this);
Jorim Jaggi51304d72017-05-17 17:25:32 +02001910 mHandler.sendMessage(msg);
1911 }
1912 }
Dan Gittika5a2d632019-01-09 14:25:29 +00001913
1914 void setAutoBrightnessLoggingEnabled(boolean enabled) {
1915 if (mAutomaticBrightnessController != null) {
1916 mAutomaticBrightnessController.setLoggingEnabled(enabled);
1917 }
1918 }
Dan Gittik8dbd7e92018-12-03 15:35:53 +00001919
1920 @Override // DisplayWhiteBalanceController.Callbacks
1921 public void updateWhiteBalance() {
1922 sendUpdatePowerState();
1923 }
1924
1925 void setDisplayWhiteBalanceLoggingEnabled(boolean enabled) {
1926 if (mDisplayWhiteBalanceController != null) {
1927 mDisplayWhiteBalanceController.setLoggingEnabled(enabled);
1928 mDisplayWhiteBalanceSettings.setLoggingEnabled(enabled);
1929 }
1930 }
1931
1932 void setAmbientColorTemperatureOverride(float cct) {
1933 if (mDisplayWhiteBalanceController != null) {
1934 mDisplayWhiteBalanceController.setAmbientColorTemperatureOverride(cct);
1935 // The ambient color temperature override is only applied when the ambient color
1936 // temperature changes or is updated, so it doesn't necessarily change the screen color
1937 // temperature immediately. So, let's make it!
1938 sendUpdatePowerState();
1939 }
1940 }
Jeff Brown96307042012-07-27 15:51:34 -07001941}