Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1 | /* |
| 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 Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 17 | package com.android.server.display; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 18 | |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 20 | import com.android.internal.app.IBatteryStats; |
| 21 | import com.android.server.LocalServices; |
| 22 | import com.android.server.am.BatteryStatsService; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 23 | import com.android.server.policy.WindowManagerPolicy; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 24 | |
| 25 | import android.animation.Animator; |
| 26 | import android.animation.ObjectAnimator; |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 27 | import android.annotation.Nullable; |
| 28 | import android.annotation.UserIdInt; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 30 | import android.content.pm.ParceledListSlice; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 32 | import android.database.ContentObserver; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 33 | import android.hardware.Sensor; |
| 34 | import android.hardware.SensorEvent; |
| 35 | import android.hardware.SensorEventListener; |
| 36 | import android.hardware.SensorManager; |
Peeyush Agarwal | cc155dd | 2018-01-10 11:51:33 +0000 | [diff] [blame] | 37 | import android.hardware.display.AmbientBrightnessDayStats; |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 38 | import android.hardware.display.BrightnessChangeEvent; |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 39 | import android.hardware.display.BrightnessConfiguration; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 40 | import android.hardware.display.DisplayManagerInternal.DisplayPowerCallbacks; |
| 41 | import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 42 | import android.net.Uri; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 43 | import android.os.Handler; |
| 44 | import android.os.Looper; |
| 45 | import android.os.Message; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 46 | import android.os.PowerManager; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 47 | import android.os.RemoteException; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 48 | import android.os.SystemClock; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 49 | import android.os.Trace; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 50 | import android.os.UserHandle; |
| 51 | import android.provider.Settings; |
Michael Wright | 41a5cdf | 2013-11-13 16:18:32 -0800 | [diff] [blame] | 52 | import android.util.MathUtils; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 53 | import android.util.Slog; |
| 54 | import android.util.TimeUtils; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 55 | import android.view.Display; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 56 | |
| 57 | import java.io.PrintWriter; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Controls the power state of the display. |
| 61 | * |
| 62 | * Handles the proximity sensor, light sensor, and animations between states |
| 63 | * including the screen off animation. |
| 64 | * |
| 65 | * This component acts independently of the rest of the power manager service. |
| 66 | * In particular, it does not share any state and it only communicates |
| 67 | * via asynchronous callbacks to inform the power manager that something has |
| 68 | * changed. |
| 69 | * |
| 70 | * Everything this class does internally is serialized on its handler although |
| 71 | * it may be accessed by other threads from the outside. |
| 72 | * |
| 73 | * Note that the power manager service guarantees that it will hold a suspend |
| 74 | * blocker as long as the display is not ready. So most of the work done here |
| 75 | * does not need to worry about holding a suspend blocker unless it happens |
| 76 | * independently of the display ready signal. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 77 | * |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 78 | * For debugging, you can make the color fade and brightness animations run |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 79 | * slower by changing the "animator duration scale" option in Development Settings. |
| 80 | */ |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 81 | final class DisplayPowerController implements AutomaticBrightnessController.Callbacks { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 82 | private static final String TAG = "DisplayPowerController"; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 83 | private static final String SCREEN_ON_BLOCKED_TRACE_NAME = "Screen on blocked"; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 84 | private static final String SCREEN_OFF_BLOCKED_TRACE_NAME = "Screen off blocked"; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 85 | |
Julius D'souza | 428aed0 | 2016-08-07 19:08:30 -0700 | [diff] [blame] | 86 | private static final boolean DEBUG = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 87 | private static final boolean DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 88 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 89 | // If true, uses the color fade on animation. |
Jeff Brown | 13c589b | 2012-08-16 16:20:54 -0700 | [diff] [blame] | 90 | // We might want to turn this off if we cannot get a guarantee that the screen |
| 91 | // actually turns on and starts showing new content after the call to set the |
Jeff Brown | 5356c7dc | 2012-08-20 20:17:36 -0700 | [diff] [blame] | 92 | // screen state returns. Playing the animation can also be somewhat slow. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 93 | private static final boolean USE_COLOR_FADE_ON_ANIMATION = false; |
Jeff Brown | 13c589b | 2012-08-16 16:20:54 -0700 | [diff] [blame] | 94 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 95 | // The minimum reduction in brightness when dimmed. |
| 96 | private static final int SCREEN_DIM_MINIMUM_REDUCTION = 10; |
| 97 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 98 | private static final int COLOR_FADE_ON_ANIMATION_DURATION_MILLIS = 250; |
Michael Lentine | 0c9a62d | 2014-08-20 09:19:07 -0700 | [diff] [blame] | 99 | private static final int COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS = 400; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 100 | |
| 101 | private static final int MSG_UPDATE_POWER_STATE = 1; |
| 102 | private static final int MSG_PROXIMITY_SENSOR_DEBOUNCED = 2; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 103 | private static final int MSG_SCREEN_ON_UNBLOCKED = 3; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 104 | private static final int MSG_SCREEN_OFF_UNBLOCKED = 4; |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 105 | private static final int MSG_CONFIGURE_BRIGHTNESS = 5; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 106 | private static final int MSG_SET_TEMPORARY_BRIGHTNESS = 6; |
| 107 | private static final int MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT = 7; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 108 | |
| 109 | private static final int PROXIMITY_UNKNOWN = -1; |
| 110 | private static final int PROXIMITY_NEGATIVE = 0; |
| 111 | private static final int PROXIMITY_POSITIVE = 1; |
| 112 | |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 113 | // Proximity sensor debounce delay in milliseconds for positive or negative transitions. |
| 114 | private static final int PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY = 0; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 115 | private static final int PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY = 250; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 116 | |
| 117 | // Trigger proximity if distance is less than 5 cm. |
| 118 | private static final float TYPICAL_PROXIMITY_THRESHOLD = 5.0f; |
| 119 | |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 120 | // State machine constants for tracking initial brightness ramp skipping when enabled. |
| 121 | private static final int RAMP_STATE_SKIP_NONE = 0; |
| 122 | private static final int RAMP_STATE_SKIP_INITIAL = 1; |
| 123 | private static final int RAMP_STATE_SKIP_AUTOBRIGHT = 2; |
| 124 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 125 | private static final int REPORTED_TO_POLICY_SCREEN_OFF = 0; |
| 126 | private static final int REPORTED_TO_POLICY_SCREEN_TURNING_ON = 1; |
| 127 | private static final int REPORTED_TO_POLICY_SCREEN_ON = 2; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 128 | private static final int REPORTED_TO_POLICY_SCREEN_TURNING_OFF = 3; |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 129 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 130 | private final Object mLock = new Object(); |
| 131 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 132 | private final Context mContext; |
| 133 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 134 | // Our handler. |
| 135 | private final DisplayControllerHandler mHandler; |
| 136 | |
| 137 | // Asynchronous callbacks into the power manager service. |
| 138 | // Only invoked from the handler thread while no locks are held. |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 139 | private final DisplayPowerCallbacks mCallbacks; |
| 140 | |
| 141 | // Battery stats. |
| 142 | private final IBatteryStats mBatteryStats; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 143 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 144 | // The sensor manager. |
| 145 | private final SensorManager mSensorManager; |
| 146 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 147 | // The window manager policy. |
| 148 | private final WindowManagerPolicy mWindowManagerPolicy; |
| 149 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 150 | // The display blanker. |
| 151 | private final DisplayBlanker mBlanker; |
| 152 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 153 | // Tracker for brightness changes. |
| 154 | private final BrightnessTracker mBrightnessTracker; |
| 155 | |
| 156 | // Tracker for brightness settings changes. |
| 157 | private final SettingsObserver mSettingsObserver; |
| 158 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 159 | // The proximity sensor, or null if not available or needed. |
| 160 | private Sensor mProximitySensor; |
| 161 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 162 | // The doze screen brightness. |
| 163 | private final int mScreenBrightnessDozeConfig; |
| 164 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 165 | // The dim screen brightness. |
| 166 | private final int mScreenBrightnessDimConfig; |
| 167 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 168 | // The minimum allowed brightness. |
| 169 | private final int mScreenBrightnessRangeMinimum; |
| 170 | |
| 171 | // The maximum allowed brightness. |
| 172 | private final int mScreenBrightnessRangeMaximum; |
| 173 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 174 | // The default screen brightness. |
| 175 | private final int mScreenBrightnessDefault; |
| 176 | |
| 177 | // The default screen brightness for VR. |
| 178 | private final int mScreenBrightnessForVrDefault; |
| 179 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 180 | // True if auto-brightness should be used. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 181 | private boolean mUseSoftwareAutoBrightnessConfig; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 182 | |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 183 | // True if should use light sensor to automatically determine doze screen brightness. |
| 184 | private final boolean mAllowAutoBrightnessWhileDozingConfig; |
| 185 | |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 186 | // Whether or not the color fade on screen on / off is enabled. |
| 187 | private final boolean mColorFadeEnabled; |
| 188 | |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 189 | // True if we should fade the screen while turning it off, false if we should play |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 190 | // a stylish color fade animation instead. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 191 | private boolean mColorFadeFadesConfig; |
Jeff Brown | a52772f | 2012-10-04 18:38:09 -0700 | [diff] [blame] | 192 | |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 193 | // True if we need to fake a transition to off when coming out of a doze state. |
| 194 | // Some display hardware will blank itself when coming out of doze in order to hide |
| 195 | // artifacts. For these displays we fake a transition into OFF so that policy can appropriately |
| 196 | // blank itself and begin an appropriate power on animation. |
| 197 | private boolean mDisplayBlanksAfterDozeConfig; |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 198 | |
Michael Wright | 63a4006 | 2017-07-26 01:10:59 +0100 | [diff] [blame] | 199 | // True if there are only buckets of brightness values when the display is in the doze state, |
| 200 | // rather than a full range of values. If this is true, then we'll avoid animating the screen |
| 201 | // brightness since it'd likely be multiple jarring brightness transitions instead of just one |
| 202 | // to reach the final state. |
| 203 | private boolean mBrightnessBucketsInDozeConfig; |
| 204 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 205 | // The pending power request. |
| 206 | // Initially null until the first call to requestPowerState. |
| 207 | // Guarded by mLock. |
| 208 | private DisplayPowerRequest mPendingRequestLocked; |
| 209 | |
| 210 | // True if a request has been made to wait for the proximity sensor to go negative. |
| 211 | // Guarded by mLock. |
| 212 | private boolean mPendingWaitForNegativeProximityLocked; |
| 213 | |
| 214 | // True if the pending power request or wait for negative proximity flag |
| 215 | // has been changed since the last update occurred. |
| 216 | // Guarded by mLock. |
| 217 | private boolean mPendingRequestChangedLocked; |
| 218 | |
| 219 | // Set to true when the important parts of the pending power request have been applied. |
| 220 | // The important parts are mainly the screen state. Brightness changes may occur |
| 221 | // concurrently. |
| 222 | // Guarded by mLock. |
| 223 | private boolean mDisplayReadyLocked; |
| 224 | |
| 225 | // Set to true if a power state update is required. |
| 226 | // Guarded by mLock. |
| 227 | private boolean mPendingUpdatePowerStateLocked; |
| 228 | |
| 229 | /* The following state must only be accessed by the handler thread. */ |
| 230 | |
| 231 | // The currently requested power state. |
| 232 | // The power controller will progressively update its internal state to match |
| 233 | // the requested power state. Initially null until the first update. |
| 234 | private DisplayPowerRequest mPowerRequest; |
| 235 | |
| 236 | // The current power state. |
| 237 | // Must only be accessed on the handler thread. |
| 238 | private DisplayPowerState mPowerState; |
| 239 | |
| 240 | // True if the device should wait for negative proximity sensor before |
| 241 | // waking up the screen. This is set to false as soon as a negative |
| 242 | // proximity sensor measurement is observed or when the device is forced to |
| 243 | // go to sleep by the user. While true, the screen remains off. |
| 244 | private boolean mWaitingForNegativeProximity; |
| 245 | |
| 246 | // The actual proximity sensor threshold value. |
| 247 | private float mProximityThreshold; |
| 248 | |
| 249 | // Set to true if the proximity sensor listener has been registered |
| 250 | // with the sensor manager. |
| 251 | private boolean mProximitySensorEnabled; |
| 252 | |
| 253 | // The debounced proximity sensor state. |
| 254 | private int mProximity = PROXIMITY_UNKNOWN; |
| 255 | |
| 256 | // The raw non-debounced proximity sensor state. |
| 257 | private int mPendingProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 258 | private long mPendingProximityDebounceTime = -1; // -1 if fully debounced |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 259 | |
| 260 | // True if the screen was turned off because of the proximity sensor. |
| 261 | // When the screen turns on again, we report user activity to the power manager. |
| 262 | private boolean mScreenOffBecauseOfProximity; |
| 263 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 264 | // The currently active screen on unblocker. This field is non-null whenever |
| 265 | // we are waiting for a callback to release it and unblock the screen. |
| 266 | private ScreenOnUnblocker mPendingScreenOnUnblocker; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 267 | private ScreenOffUnblocker mPendingScreenOffUnblocker; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 268 | |
| 269 | // True if we were in the process of turning off the screen. |
| 270 | // This allows us to recover more gracefully from situations where we abort |
| 271 | // turning off the screen. |
| 272 | private boolean mPendingScreenOff; |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 273 | |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 274 | // True if we have unfinished business and are holding a suspend blocker. |
| 275 | private boolean mUnfinishedBusiness; |
| 276 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 277 | // The elapsed real time when the screen on was blocked. |
| 278 | private long mScreenOnBlockStartRealTime; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 279 | private long mScreenOffBlockStartRealTime; |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 280 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 281 | // Screen state we reported to policy. Must be one of REPORTED_TO_POLICY_SCREEN_* fields. |
| 282 | private int mReportedScreenStateToPolicy; |
Jeff Brown | e1633ad | 2015-06-22 19:24:24 -0700 | [diff] [blame] | 283 | |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 284 | // If the last recorded screen state was dozing or not. |
| 285 | private boolean mDozing; |
| 286 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 287 | // Remembers whether certain kinds of brightness adjustments |
| 288 | // were recently applied so that we can decide how to transition. |
| 289 | private boolean mAppliedAutoBrightness; |
| 290 | private boolean mAppliedDimming; |
| 291 | private boolean mAppliedLowPower; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 292 | |
Julius D'souza | feadad1 | 2016-08-05 14:34:38 -0700 | [diff] [blame] | 293 | // Brightness animation ramp rates in brightness units per second |
Prashant Malani | 99e6d43 | 2016-03-29 16:32:37 -0700 | [diff] [blame] | 294 | private final int mBrightnessRampRateFast; |
Julius D'souza | feadad1 | 2016-08-05 14:34:38 -0700 | [diff] [blame] | 295 | private final int mBrightnessRampRateSlow; |
Prashant Malani | 99e6d43 | 2016-03-29 16:32:37 -0700 | [diff] [blame] | 296 | |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 297 | // Whether or not to skip the initial brightness ramps into STATE_ON. |
| 298 | private final boolean mSkipScreenOnBrightnessRamp; |
| 299 | |
| 300 | // A record of state for skipping brightness ramps. |
| 301 | private int mSkipRampState = RAMP_STATE_SKIP_NONE; |
| 302 | |
| 303 | // The first autobrightness value set when entering RAMP_STATE_SKIP_INITIAL. |
| 304 | private int mInitialAutoBrightness; |
| 305 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 306 | // The controller for the automatic brightness level. |
| 307 | private AutomaticBrightnessController mAutomaticBrightnessController; |
| 308 | |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 309 | // The mapper between ambient lux, display backlight values, and display brightness. |
| 310 | @Nullable |
| 311 | private BrightnessMappingStrategy mBrightnessMapper; |
| 312 | |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 313 | // The current brightness configuration. |
Michael Wright | d5df361 | 2018-01-02 12:44:52 +0000 | [diff] [blame] | 314 | @Nullable |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 315 | private BrightnessConfiguration mBrightnessConfiguration; |
| 316 | |
Michael Wright | d5df361 | 2018-01-02 12:44:52 +0000 | [diff] [blame] | 317 | // The last brightness that was set by the user and not temporary. Set to -1 when a brightness |
| 318 | // has yet to be recorded. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 319 | private int mLastUserSetScreenBrightness; |
| 320 | |
| 321 | // The screen brightenss setting has changed but not taken effect yet. If this is different |
| 322 | // from the current screen brightness setting then this is coming from something other than us |
| 323 | // and should be considered a user interaction. |
| 324 | private int mPendingScreenBrightnessSetting; |
| 325 | |
| 326 | // The last observed screen brightness setting, either set by us or by the settings app on |
| 327 | // behalf of the user. |
| 328 | private int mCurrentScreenBrightnessSetting; |
| 329 | |
| 330 | // The temporary screen brightness. Typically set when a user is interacting with the |
| 331 | // brightness slider but hasn't settled on a choice yet. Set to -1 when there's no temporary |
| 332 | // brightness set. |
| 333 | private int mTemporaryScreenBrightness; |
| 334 | |
| 335 | // The current screen brightness while in VR mode. |
| 336 | private int mScreenBrightnessForVr; |
Michael Wright | d5df361 | 2018-01-02 12:44:52 +0000 | [diff] [blame] | 337 | |
| 338 | // The last auto brightness adjustment that was set by the user and not temporary. Set to |
| 339 | // Float.NaN when an auto-brightness adjustment hasn't been recorded yet. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 340 | private float mAutoBrightnessAdjustment; |
| 341 | |
| 342 | // The pending auto brightness adjustment that will take effect on the next power state update. |
| 343 | private float mPendingAutoBrightnessAdjustment; |
| 344 | |
| 345 | // The temporary auto brightness adjustment. Typically set when a user is interacting with the |
| 346 | // adjustment slider but hasn't settled on a choice yet. Set to Float.NaN when there's no |
| 347 | // temporary adjustment set. |
| 348 | private float mTemporaryAutoBrightnessAdjustment; |
Michael Wright | d5df361 | 2018-01-02 12:44:52 +0000 | [diff] [blame] | 349 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 350 | // Animators. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 351 | private ObjectAnimator mColorFadeOnAnimator; |
| 352 | private ObjectAnimator mColorFadeOffAnimator; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 353 | private RampAnimator<DisplayPowerState> mScreenBrightnessRampAnimator; |
| 354 | |
| 355 | /** |
| 356 | * Creates the display power controller. |
| 357 | */ |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 358 | public DisplayPowerController(Context context, |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 359 | DisplayPowerCallbacks callbacks, Handler handler, |
| 360 | SensorManager sensorManager, DisplayBlanker blanker) { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 361 | mHandler = new DisplayControllerHandler(handler.getLooper()); |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 362 | mBrightnessTracker = new BrightnessTracker(context, null); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 363 | mSettingsObserver = new SettingsObserver(mHandler); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 364 | mCallbacks = callbacks; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 365 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 366 | mBatteryStats = BatteryStatsService.getService(); |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 367 | mSensorManager = sensorManager; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 368 | mWindowManagerPolicy = LocalServices.getService(WindowManagerPolicy.class); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 369 | mBlanker = blanker; |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 370 | mContext = context; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 371 | |
| 372 | final Resources resources = context.getResources(); |
Jeff Brown | 1bfd0f4 | 2014-08-22 01:59:06 -0700 | [diff] [blame] | 373 | final int screenBrightnessSettingMinimum = clampAbsoluteBrightness(resources.getInteger( |
| 374 | com.android.internal.R.integer.config_screenBrightnessSettingMinimum)); |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 375 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 376 | mScreenBrightnessDozeConfig = clampAbsoluteBrightness(resources.getInteger( |
| 377 | com.android.internal.R.integer.config_screenBrightnessDoze)); |
| 378 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 379 | mScreenBrightnessDimConfig = clampAbsoluteBrightness(resources.getInteger( |
| 380 | com.android.internal.R.integer.config_screenBrightnessDim)); |
| 381 | |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 382 | mScreenBrightnessRangeMinimum = |
| 383 | Math.min(screenBrightnessSettingMinimum, mScreenBrightnessDimConfig); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 384 | |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 385 | mScreenBrightnessRangeMaximum = clampAbsoluteBrightness(resources.getInteger( |
| 386 | com.android.internal.R.integer.config_screenBrightnessSettingMaximum)); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 387 | mScreenBrightnessDefault = clampAbsoluteBrightness(resources.getInteger( |
| 388 | com.android.internal.R.integer.config_screenBrightnessSettingDefault)); |
| 389 | mScreenBrightnessForVrDefault = clampAbsoluteBrightness(resources.getInteger( |
| 390 | com.android.internal.R.integer.config_screenBrightnessForVrSettingDefault)); |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 391 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 392 | mUseSoftwareAutoBrightnessConfig = resources.getBoolean( |
| 393 | com.android.internal.R.bool.config_automatic_brightness_available); |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 394 | |
| 395 | mAllowAutoBrightnessWhileDozingConfig = resources.getBoolean( |
| 396 | com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing); |
| 397 | |
Prashant Malani | 99e6d43 | 2016-03-29 16:32:37 -0700 | [diff] [blame] | 398 | mBrightnessRampRateFast = resources.getInteger( |
| 399 | com.android.internal.R.integer.config_brightness_ramp_rate_fast); |
Julius D'souza | feadad1 | 2016-08-05 14:34:38 -0700 | [diff] [blame] | 400 | mBrightnessRampRateSlow = resources.getInteger( |
| 401 | com.android.internal.R.integer.config_brightness_ramp_rate_slow); |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 402 | mSkipScreenOnBrightnessRamp = resources.getBoolean( |
| 403 | com.android.internal.R.bool.config_skipScreenOnBrightnessRamp); |
Prashant Malani | 99e6d43 | 2016-03-29 16:32:37 -0700 | [diff] [blame] | 404 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 405 | if (mUseSoftwareAutoBrightnessConfig) { |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 406 | final float dozeScaleFactor = resources.getFraction( |
| 407 | com.android.internal.R.fraction.config_screenAutoBrightnessDozeScaleFactor, |
| 408 | 1, 1); |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 409 | |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 410 | int[] brightLevels = resources.getIntArray( |
| 411 | com.android.internal.R.array.config_dynamicHysteresisBrightLevels); |
| 412 | int[] darkLevels = resources.getIntArray( |
| 413 | com.android.internal.R.array.config_dynamicHysteresisDarkLevels); |
| 414 | int[] luxHysteresisLevels = resources.getIntArray( |
| 415 | com.android.internal.R.array.config_dynamicHysteresisLuxLevels); |
| 416 | HysteresisLevels dynamicHysteresis = new HysteresisLevels( |
| 417 | brightLevels, darkLevels, luxHysteresisLevels); |
| 418 | |
| 419 | long brighteningLightDebounce = resources.getInteger( |
| 420 | com.android.internal.R.integer.config_autoBrightnessBrighteningLightDebounce); |
| 421 | long darkeningLightDebounce = resources.getInteger( |
| 422 | com.android.internal.R.integer.config_autoBrightnessDarkeningLightDebounce); |
| 423 | boolean autoBrightnessResetAmbientLuxAfterWarmUp = resources.getBoolean( |
| 424 | com.android.internal.R.bool.config_autoBrightnessResetAmbientLuxAfterWarmUp); |
| 425 | int ambientLightHorizon = resources.getInteger( |
| 426 | com.android.internal.R.integer.config_autoBrightnessAmbientLightHorizon); |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 427 | |
| 428 | int lightSensorWarmUpTimeConfig = resources.getInteger( |
| 429 | com.android.internal.R.integer.config_lightSensorWarmupTime); |
| 430 | int lightSensorRate = resources.getInteger( |
| 431 | com.android.internal.R.integer.config_autoBrightnessLightSensorRate); |
| 432 | int initialLightSensorRate = resources.getInteger( |
| 433 | com.android.internal.R.integer.config_autoBrightnessInitialLightSensorRate); |
| 434 | if (initialLightSensorRate == -1) { |
| 435 | initialLightSensorRate = lightSensorRate; |
| 436 | } else if (initialLightSensorRate > lightSensorRate) { |
| 437 | Slog.w(TAG, "Expected config_autoBrightnessInitialLightSensorRate (" |
| 438 | + initialLightSensorRate + ") to be less than or equal to " |
| 439 | + "config_autoBrightnessLightSensorRate (" + lightSensorRate + ")."); |
| 440 | } |
| 441 | |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 442 | mBrightnessMapper = BrightnessMappingStrategy.create(resources); |
| 443 | if (mBrightnessMapper != null) { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 444 | mAutomaticBrightnessController = new AutomaticBrightnessController(this, |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 445 | handler.getLooper(), sensorManager, mBrightnessMapper, |
| 446 | lightSensorWarmUpTimeConfig, mScreenBrightnessRangeMinimum, |
| 447 | mScreenBrightnessRangeMaximum, dozeScaleFactor, lightSensorRate, |
| 448 | initialLightSensorRate, brighteningLightDebounce, darkeningLightDebounce, |
Julius D'souza | 428aed0 | 2016-08-07 19:08:30 -0700 | [diff] [blame] | 449 | autoBrightnessResetAmbientLuxAfterWarmUp, ambientLightHorizon, |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 450 | dynamicHysteresis); |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 451 | } else { |
| 452 | mUseSoftwareAutoBrightnessConfig = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 453 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 456 | mColorFadeEnabled = !ActivityManager.isLowRamDeviceStatic(); |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 457 | mColorFadeFadesConfig = resources.getBoolean( |
Jeff Brown | a52772f | 2012-10-04 18:38:09 -0700 | [diff] [blame] | 458 | com.android.internal.R.bool.config_animateScreenLights); |
| 459 | |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 460 | mDisplayBlanksAfterDozeConfig = resources.getBoolean( |
| 461 | com.android.internal.R.bool.config_displayBlanksAfterDoze); |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 462 | |
Michael Wright | 63a4006 | 2017-07-26 01:10:59 +0100 | [diff] [blame] | 463 | mBrightnessBucketsInDozeConfig = resources.getBoolean( |
| 464 | com.android.internal.R.bool.config_displayBrightnessBucketsInDoze); |
| 465 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 466 | if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) { |
| 467 | mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); |
| 468 | if (mProximitySensor != null) { |
| 469 | mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(), |
| 470 | TYPICAL_PROXIMITY_THRESHOLD); |
| 471 | } |
| 472 | } |
| 473 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 474 | mCurrentScreenBrightnessSetting = getScreenBrightnessSetting(); |
| 475 | mScreenBrightnessForVr = getScreenBrightnessForVrSetting(); |
| 476 | mAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting(); |
| 477 | mTemporaryScreenBrightness = -1; |
Kenny Guy | ad9a6ea | 2018-01-31 15:57:15 +0000 | [diff] [blame] | 478 | mPendingScreenBrightnessSetting = -1; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 479 | mTemporaryAutoBrightnessAdjustment = Float.NaN; |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Returns true if the proximity sensor screen-off function is available. |
| 484 | */ |
| 485 | public boolean isProximitySensorAvailable() { |
| 486 | return mProximitySensor != null; |
| 487 | } |
| 488 | |
| 489 | /** |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 490 | * Get the {@link BrightnessChangeEvent}s for the specified user. |
| 491 | * @param userId userId to fetch data for |
| 492 | * @param includePackage if false will null out the package name in events |
| 493 | */ |
| 494 | public ParceledListSlice<BrightnessChangeEvent> getBrightnessEvents( |
| 495 | @UserIdInt int userId, boolean includePackage) { |
| 496 | return mBrightnessTracker.getEvents(userId, includePackage); |
| 497 | } |
| 498 | |
Peeyush Agarwal | cc155dd | 2018-01-10 11:51:33 +0000 | [diff] [blame] | 499 | public void onSwitchUser(@UserIdInt int newUserId) { |
Kenny Guy | ecc978f | 2018-03-14 17:30:20 +0000 | [diff] [blame] | 500 | handleSettingsChange(true /* userSwitch */); |
Peeyush Agarwal | cc155dd | 2018-01-10 11:51:33 +0000 | [diff] [blame] | 501 | mBrightnessTracker.onSwitchUser(newUserId); |
| 502 | } |
| 503 | |
| 504 | public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats( |
| 505 | @UserIdInt int userId) { |
| 506 | return mBrightnessTracker.getAmbientBrightnessStats(userId); |
| 507 | } |
| 508 | |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 509 | /** |
Peeyush Agarwal | cc155dd | 2018-01-10 11:51:33 +0000 | [diff] [blame] | 510 | * Persist the brightness slider events and ambient brightness stats to disk. |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 511 | */ |
Peeyush Agarwal | cc155dd | 2018-01-10 11:51:33 +0000 | [diff] [blame] | 512 | public void persistBrightnessTrackerState() { |
| 513 | mBrightnessTracker.persistBrightnessTrackerState(); |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /** |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 517 | * Requests a new power state. |
| 518 | * The controller makes a copy of the provided object and then |
| 519 | * begins adjusting the power state to match what was requested. |
| 520 | * |
| 521 | * @param request The requested power state. |
| 522 | * @param waitForNegativeProximity If true, issues a request to wait for |
| 523 | * negative proximity before turning the screen back on, assuming the screen |
| 524 | * was turned off by the proximity sensor. |
| 525 | * @return True if display is ready, false if there are important changes that must |
| 526 | * be made asynchronously (such as turning the screen on), in which case the caller |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 527 | * should grab a wake lock, watch for {@link DisplayPowerCallbacks#onStateChanged()} |
| 528 | * then try the request again later until the state converges. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 529 | */ |
| 530 | public boolean requestPowerState(DisplayPowerRequest request, |
| 531 | boolean waitForNegativeProximity) { |
| 532 | if (DEBUG) { |
| 533 | Slog.d(TAG, "requestPowerState: " |
| 534 | + request + ", waitForNegativeProximity=" + waitForNegativeProximity); |
| 535 | } |
| 536 | |
| 537 | synchronized (mLock) { |
| 538 | boolean changed = false; |
| 539 | |
| 540 | if (waitForNegativeProximity |
| 541 | && !mPendingWaitForNegativeProximityLocked) { |
| 542 | mPendingWaitForNegativeProximityLocked = true; |
| 543 | changed = true; |
| 544 | } |
| 545 | |
| 546 | if (mPendingRequestLocked == null) { |
| 547 | mPendingRequestLocked = new DisplayPowerRequest(request); |
| 548 | changed = true; |
| 549 | } else if (!mPendingRequestLocked.equals(request)) { |
| 550 | mPendingRequestLocked.copyFrom(request); |
| 551 | changed = true; |
| 552 | } |
| 553 | |
| 554 | if (changed) { |
| 555 | mDisplayReadyLocked = false; |
| 556 | } |
| 557 | |
| 558 | if (changed && !mPendingRequestChangedLocked) { |
| 559 | mPendingRequestChangedLocked = true; |
| 560 | sendUpdatePowerStateLocked(); |
| 561 | } |
| 562 | |
| 563 | return mDisplayReadyLocked; |
| 564 | } |
| 565 | } |
| 566 | |
Kenny Guy | 6d1009f | 2018-03-14 14:28:23 +0000 | [diff] [blame] | 567 | public BrightnessConfiguration getDefaultBrightnessConfiguration() { |
| 568 | return mAutomaticBrightnessController.getDefaultConfig(); |
| 569 | } |
| 570 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 571 | private void sendUpdatePowerState() { |
| 572 | synchronized (mLock) { |
| 573 | sendUpdatePowerStateLocked(); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | private void sendUpdatePowerStateLocked() { |
| 578 | if (!mPendingUpdatePowerStateLocked) { |
| 579 | mPendingUpdatePowerStateLocked = true; |
| 580 | Message msg = mHandler.obtainMessage(MSG_UPDATE_POWER_STATE); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 581 | mHandler.sendMessage(msg); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | private void initialize() { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 586 | // Initialize the power state object for the default display. |
| 587 | // In the future, we might manage multiple displays independently. |
| 588 | mPowerState = new DisplayPowerState(mBlanker, |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 589 | mColorFadeEnabled ? new ColorFade(Display.DEFAULT_DISPLAY) : null); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 590 | |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 591 | if (mColorFadeEnabled) { |
| 592 | mColorFadeOnAnimator = ObjectAnimator.ofFloat( |
| 593 | mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 0.0f, 1.0f); |
| 594 | mColorFadeOnAnimator.setDuration(COLOR_FADE_ON_ANIMATION_DURATION_MILLIS); |
| 595 | mColorFadeOnAnimator.addListener(mAnimatorListener); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 596 | |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 597 | mColorFadeOffAnimator = ObjectAnimator.ofFloat( |
| 598 | mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 1.0f, 0.0f); |
| 599 | mColorFadeOffAnimator.setDuration(COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS); |
| 600 | mColorFadeOffAnimator.addListener(mAnimatorListener); |
| 601 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 602 | |
| 603 | mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>( |
| 604 | mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS); |
Jeff Brown | 4255869 | 2014-05-20 22:02:46 -0700 | [diff] [blame] | 605 | mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 606 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 607 | // Initialize screen state for battery stats. |
| 608 | try { |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 609 | mBatteryStats.noteScreenState(mPowerState.getScreenState()); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 610 | mBatteryStats.noteScreenBrightness(mPowerState.getScreenBrightness()); |
| 611 | } catch (RemoteException ex) { |
| 612 | // same process |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 613 | } |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 614 | |
| 615 | // Initialize all of the brightness tracking state |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 616 | final float brightness = convertToNits(mPowerState.getScreenBrightness()); |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 617 | if (brightness >= 0.0f) { |
| 618 | mBrightnessTracker.start(brightness); |
| 619 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 620 | |
| 621 | mContext.getContentResolver().registerContentObserver( |
| 622 | Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), |
| 623 | false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL); |
| 624 | mContext.getContentResolver().registerContentObserver( |
| 625 | Settings.System.getUriFor(Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ), |
| 626 | false /*notifyForDescendants*/, mSettingsObserver, UserHandle.USER_ALL); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | private final Animator.AnimatorListener mAnimatorListener = new Animator.AnimatorListener() { |
| 630 | @Override |
| 631 | public void onAnimationStart(Animator animation) { |
| 632 | } |
| 633 | @Override |
| 634 | public void onAnimationEnd(Animator animation) { |
| 635 | sendUpdatePowerState(); |
| 636 | } |
| 637 | @Override |
| 638 | public void onAnimationRepeat(Animator animation) { |
| 639 | } |
| 640 | @Override |
| 641 | public void onAnimationCancel(Animator animation) { |
| 642 | } |
| 643 | }; |
| 644 | |
Jeff Brown | 4255869 | 2014-05-20 22:02:46 -0700 | [diff] [blame] | 645 | private final RampAnimator.Listener mRampAnimatorListener = new RampAnimator.Listener() { |
| 646 | @Override |
| 647 | public void onAnimationEnd() { |
| 648 | sendUpdatePowerState(); |
| 649 | } |
| 650 | }; |
| 651 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 652 | private void updatePowerState() { |
| 653 | // Update the power state request. |
| 654 | final boolean mustNotify; |
| 655 | boolean mustInitialize = false; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 656 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 657 | synchronized (mLock) { |
| 658 | mPendingUpdatePowerStateLocked = false; |
| 659 | if (mPendingRequestLocked == null) { |
| 660 | return; // wait until first actual power request |
| 661 | } |
| 662 | |
| 663 | if (mPowerRequest == null) { |
| 664 | mPowerRequest = new DisplayPowerRequest(mPendingRequestLocked); |
| 665 | mWaitingForNegativeProximity = mPendingWaitForNegativeProximityLocked; |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 666 | mPendingWaitForNegativeProximityLocked = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 667 | mPendingRequestChangedLocked = false; |
| 668 | mustInitialize = true; |
| 669 | } else if (mPendingRequestChangedLocked) { |
| 670 | mPowerRequest.copyFrom(mPendingRequestLocked); |
| 671 | mWaitingForNegativeProximity |= mPendingWaitForNegativeProximityLocked; |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 672 | mPendingWaitForNegativeProximityLocked = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 673 | mPendingRequestChangedLocked = false; |
| 674 | mDisplayReadyLocked = false; |
| 675 | } |
| 676 | |
| 677 | mustNotify = !mDisplayReadyLocked; |
| 678 | } |
| 679 | |
| 680 | // Initialize things the first time the power state is changed. |
| 681 | if (mustInitialize) { |
| 682 | initialize(); |
| 683 | } |
| 684 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 685 | // Compute the basic display state using the policy. |
| 686 | // We might override this below based on other factors. |
| 687 | int state; |
| 688 | int brightness = PowerManager.BRIGHTNESS_DEFAULT; |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 689 | boolean performScreenOffTransition = false; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 690 | switch (mPowerRequest.policy) { |
| 691 | case DisplayPowerRequest.POLICY_OFF: |
| 692 | state = Display.STATE_OFF; |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 693 | performScreenOffTransition = true; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 694 | break; |
| 695 | case DisplayPowerRequest.POLICY_DOZE: |
| 696 | if (mPowerRequest.dozeScreenState != Display.STATE_UNKNOWN) { |
| 697 | state = mPowerRequest.dozeScreenState; |
| 698 | } else { |
| 699 | state = Display.STATE_DOZE; |
| 700 | } |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 701 | if (!mAllowAutoBrightnessWhileDozingConfig) { |
| 702 | brightness = mPowerRequest.dozeScreenBrightness; |
| 703 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 704 | break; |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 705 | case DisplayPowerRequest.POLICY_VR: |
| 706 | state = Display.STATE_VR; |
| 707 | break; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 708 | case DisplayPowerRequest.POLICY_DIM: |
| 709 | case DisplayPowerRequest.POLICY_BRIGHT: |
| 710 | default: |
| 711 | state = Display.STATE_ON; |
| 712 | break; |
| 713 | } |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 714 | assert(state != Display.STATE_UNKNOWN); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 715 | |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 716 | // Apply the proximity sensor. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 717 | if (mProximitySensor != null) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 718 | if (mPowerRequest.useProximitySensor && state != Display.STATE_OFF) { |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 719 | setProximitySensorEnabled(true); |
| 720 | if (!mScreenOffBecauseOfProximity |
| 721 | && mProximity == PROXIMITY_POSITIVE) { |
| 722 | mScreenOffBecauseOfProximity = true; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 723 | sendOnProximityPositiveWithWakelock(); |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 724 | } |
| 725 | } else if (mWaitingForNegativeProximity |
| 726 | && mScreenOffBecauseOfProximity |
| 727 | && mProximity == PROXIMITY_POSITIVE |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 728 | && state != Display.STATE_OFF) { |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 729 | setProximitySensorEnabled(true); |
| 730 | } else { |
| 731 | setProximitySensorEnabled(false); |
| 732 | mWaitingForNegativeProximity = false; |
| 733 | } |
| 734 | if (mScreenOffBecauseOfProximity |
| 735 | && mProximity != PROXIMITY_POSITIVE) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 736 | mScreenOffBecauseOfProximity = false; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 737 | sendOnProximityNegativeWithWakelock(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 738 | } |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 739 | } else { |
| 740 | mWaitingForNegativeProximity = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 741 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 742 | if (mScreenOffBecauseOfProximity) { |
| 743 | state = Display.STATE_OFF; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Jeff Brown | bf4e414 | 2014-10-02 13:08:05 -0700 | [diff] [blame] | 746 | // Animate the screen state change unless already animating. |
| 747 | // The transition may be deferred, so after this point we will use the |
| 748 | // actual state instead of the desired one. |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 749 | final int oldState = mPowerState.getScreenState(); |
Jeff Brown | bf4e414 | 2014-10-02 13:08:05 -0700 | [diff] [blame] | 750 | animateScreenStateChange(state, performScreenOffTransition); |
| 751 | state = mPowerState.getScreenState(); |
| 752 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 753 | // Use zero brightness when screen is off. |
| 754 | if (state == Display.STATE_OFF) { |
| 755 | brightness = PowerManager.BRIGHTNESS_OFF; |
| 756 | } |
| 757 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 758 | // Always use the VR brightness when in the VR state. |
| 759 | if (state == Display.STATE_VR) { |
| 760 | brightness = mScreenBrightnessForVr; |
| 761 | } |
| 762 | |
| 763 | if (brightness < 0 && mPowerRequest.screenBrightnessOverride > 0) { |
| 764 | brightness = mPowerRequest.screenBrightnessOverride; |
| 765 | } |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 766 | |
| 767 | final boolean autoBrightnessEnabledInDoze = |
| 768 | mAllowAutoBrightnessWhileDozingConfig && Display.isDozeState(state); |
| 769 | final boolean autoBrightnessEnabled = mPowerRequest.useAutoBrightness |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 770 | && (state == Display.STATE_ON || autoBrightnessEnabledInDoze) |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 771 | && brightness < 0 |
| 772 | && mAutomaticBrightnessController != null; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 773 | boolean brightnessIsTemporary = false; |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 774 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 775 | final boolean userSetBrightnessChanged = updateUserSetScreenBrightness(); |
| 776 | if (userSetBrightnessChanged) { |
| 777 | mTemporaryScreenBrightness = -1; |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 780 | // Use the temporary screen brightness if there isn't an override, either from |
| 781 | // WindowManager or based on the display state. |
| 782 | if (mTemporaryScreenBrightness > 0) { |
| 783 | brightness = mTemporaryScreenBrightness; |
| 784 | brightnessIsTemporary = true; |
| 785 | } |
| 786 | |
| 787 | final boolean autoBrightnessAdjustmentChanged = updateAutoBrightnessAdjustment(); |
| 788 | if (autoBrightnessAdjustmentChanged) { |
| 789 | mTemporaryAutoBrightnessAdjustment = Float.NaN; |
| 790 | } |
| 791 | |
| 792 | // Use the autobrightness adjustment override if set. |
| 793 | final float autoBrightnessAdjustment; |
| 794 | if (!Float.isNaN(mTemporaryAutoBrightnessAdjustment)) { |
| 795 | autoBrightnessAdjustment = mTemporaryAutoBrightnessAdjustment; |
| 796 | brightnessIsTemporary = true; |
| 797 | } else { |
| 798 | autoBrightnessAdjustment = mAutoBrightnessAdjustment; |
| 799 | } |
| 800 | |
| 801 | // Apply brightness boost. |
| 802 | // We do this here after deciding whether auto-brightness is enabled so that we don't |
| 803 | // disable the light sensor during this temporary state. That way when boost ends we will |
| 804 | // be able to resume normal auto-brightness behavior without any delay. |
| 805 | if (mPowerRequest.boostScreenBrightness |
| 806 | && brightness != PowerManager.BRIGHTNESS_OFF) { |
| 807 | brightness = PowerManager.BRIGHTNESS_ON; |
| 808 | } |
| 809 | |
Kenny Guy | 53d0661 | 2018-01-30 14:19:13 +0000 | [diff] [blame] | 810 | // If the brightness is already set then it's been overridden by something other than the |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 811 | // user, or is a temporary adjustment. |
| 812 | final boolean userInitiatedChange = brightness < 0 |
| 813 | && (autoBrightnessAdjustmentChanged || userSetBrightnessChanged); |
| 814 | |
Kenny Guy | 53d0661 | 2018-01-30 14:19:13 +0000 | [diff] [blame] | 815 | boolean hadUserBrightnessPoint = false; |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 816 | // Configure auto-brightness. |
| 817 | if (mAutomaticBrightnessController != null) { |
Kenny Guy | 53d0661 | 2018-01-30 14:19:13 +0000 | [diff] [blame] | 818 | hadUserBrightnessPoint = mAutomaticBrightnessController.hasUserDataPoints(); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 819 | mAutomaticBrightnessController.configure(autoBrightnessEnabled, |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 820 | mBrightnessConfiguration, |
| 821 | mLastUserSetScreenBrightness / (float) PowerManager.BRIGHTNESS_ON, |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 822 | userSetBrightnessChanged, autoBrightnessAdjustment, |
| 823 | autoBrightnessAdjustmentChanged, mPowerRequest.policy); |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 826 | // Apply auto-brightness. |
| 827 | boolean slowChange = false; |
| 828 | if (brightness < 0) { |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 829 | float newAutoBrightnessAdjustment = autoBrightnessAdjustment; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 830 | if (autoBrightnessEnabled) { |
| 831 | brightness = mAutomaticBrightnessController.getAutomaticScreenBrightness(); |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 832 | newAutoBrightnessAdjustment = |
| 833 | mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment(); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 834 | } |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 835 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 836 | if (brightness >= 0) { |
| 837 | // Use current auto-brightness value and slowly adjust to changes. |
| 838 | brightness = clampScreenBrightness(brightness); |
| 839 | if (mAppliedAutoBrightness && !autoBrightnessAdjustmentChanged) { |
| 840 | slowChange = true; // slowly adapt to auto-brightness |
| 841 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 842 | // Tell the rest of the system about the new brightness. Note that we do this |
| 843 | // before applying the low power or dim transformations so that the slider |
| 844 | // accurately represents the full possible range, even if they range changes what |
| 845 | // it means in absolute terms. |
| 846 | putScreenBrightnessSetting(brightness); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 847 | mAppliedAutoBrightness = true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 848 | } else { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 849 | mAppliedAutoBrightness = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 850 | } |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 851 | if (autoBrightnessAdjustment != newAutoBrightnessAdjustment) { |
| 852 | // If the autobrightness controller has decided to change the adjustment value |
| 853 | // used, make sure that's reflected in settings. |
| 854 | putAutoBrightnessAdjustmentSetting(newAutoBrightnessAdjustment); |
| 855 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 856 | } else { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 857 | mAppliedAutoBrightness = false; |
| 858 | } |
| 859 | |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 860 | // Use default brightness when dozing unless overridden. |
Michael Wright | b0a1d3d | 2017-09-22 15:05:02 +0100 | [diff] [blame] | 861 | if (brightness < 0 && Display.isDozeState(state)) { |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 862 | brightness = mScreenBrightnessDozeConfig; |
| 863 | } |
| 864 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 865 | // Apply manual brightness. |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 866 | if (brightness < 0) { |
Michael Wright | 7bb2234 | 2018-01-26 00:16:41 +0000 | [diff] [blame] | 867 | brightness = clampScreenBrightness(mCurrentScreenBrightnessSetting); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 870 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 871 | // Apply dimming by at least some minimum amount when user activity |
| 872 | // timeout is about to expire. |
| 873 | if (mPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) { |
Jeff Brown | 5c8ea08 | 2014-07-24 19:30:31 -0700 | [diff] [blame] | 874 | if (brightness > mScreenBrightnessRangeMinimum) { |
| 875 | brightness = Math.max(Math.min(brightness - SCREEN_DIM_MINIMUM_REDUCTION, |
| 876 | mScreenBrightnessDimConfig), mScreenBrightnessRangeMinimum); |
| 877 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 878 | if (!mAppliedDimming) { |
| 879 | slowChange = false; |
| 880 | } |
| 881 | mAppliedDimming = true; |
mochangming | f0cb46c | 2015-12-29 13:55:24 +0800 | [diff] [blame] | 882 | } else if (mAppliedDimming) { |
| 883 | slowChange = false; |
| 884 | mAppliedDimming = false; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 885 | } |
| 886 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 887 | // If low power mode is enabled, scale brightness by screenLowPowerBrightnessFactor |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 888 | // as long as it is above the minimum threshold. |
| 889 | if (mPowerRequest.lowPowerMode) { |
| 890 | if (brightness > mScreenBrightnessRangeMinimum) { |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 891 | final float brightnessFactor = |
| 892 | Math.min(mPowerRequest.screenLowPowerBrightnessFactor, 1); |
| 893 | final int lowPowerBrightness = (int) (brightness * brightnessFactor); |
| 894 | brightness = Math.max(lowPowerBrightness, mScreenBrightnessRangeMinimum); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 895 | } |
| 896 | if (!mAppliedLowPower) { |
| 897 | slowChange = false; |
| 898 | } |
| 899 | mAppliedLowPower = true; |
mochangming | f0cb46c | 2015-12-29 13:55:24 +0800 | [diff] [blame] | 900 | } else if (mAppliedLowPower) { |
| 901 | slowChange = false; |
| 902 | mAppliedLowPower = false; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 905 | // Animate the screen brightness when the screen is on or dozing. |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 906 | // Skip the animation when the screen is off or suspended or transition to/from VR. |
Prashant Malani | 3353824 | 2014-11-13 14:04:00 -0800 | [diff] [blame] | 907 | if (!mPendingScreenOff) { |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 908 | if (mSkipScreenOnBrightnessRamp) { |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 909 | if (state == Display.STATE_ON) { |
| 910 | if (mSkipRampState == RAMP_STATE_SKIP_NONE && mDozing) { |
| 911 | mInitialAutoBrightness = brightness; |
| 912 | mSkipRampState = RAMP_STATE_SKIP_INITIAL; |
| 913 | } else if (mSkipRampState == RAMP_STATE_SKIP_INITIAL |
| 914 | && mUseSoftwareAutoBrightnessConfig |
| 915 | && brightness != mInitialAutoBrightness) { |
| 916 | mSkipRampState = RAMP_STATE_SKIP_AUTOBRIGHT; |
| 917 | } else if (mSkipRampState == RAMP_STATE_SKIP_AUTOBRIGHT) { |
| 918 | mSkipRampState = RAMP_STATE_SKIP_NONE; |
| 919 | } |
| 920 | } else { |
| 921 | mSkipRampState = RAMP_STATE_SKIP_NONE; |
| 922 | } |
| 923 | } |
| 924 | |
Michael Wright | b0a1d3d | 2017-09-22 15:05:02 +0100 | [diff] [blame] | 925 | final boolean wasOrWillBeInVr = |
| 926 | (state == Display.STATE_VR || oldState == Display.STATE_VR); |
| 927 | final boolean initialRampSkip = |
| 928 | state == Display.STATE_ON && mSkipRampState != RAMP_STATE_SKIP_NONE; |
| 929 | // While dozing, sometimes the brightness is split into buckets. Rather than animating |
| 930 | // through the buckets, which is unlikely to be smooth in the first place, just jump |
| 931 | // right to the suggested brightness. |
| 932 | final boolean hasBrightnessBuckets = |
| 933 | Display.isDozeState(state) && mBrightnessBucketsInDozeConfig; |
| 934 | // If the color fade is totally covering the screen then we can change the backlight |
| 935 | // level without it being a noticeable jump since any actual content isn't yet visible. |
| 936 | final boolean isDisplayContentVisible = |
| 937 | mColorFadeEnabled && mPowerState.getColorFadeLevel() == 1.0f; |
| 938 | if (initialRampSkip || hasBrightnessBuckets |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 939 | || wasOrWillBeInVr || !isDisplayContentVisible || brightnessIsTemporary) { |
Michael Wright | b0a1d3d | 2017-09-22 15:05:02 +0100 | [diff] [blame] | 940 | animateScreenBrightness(brightness, 0); |
| 941 | } else { |
Prashant Malani | 3353824 | 2014-11-13 14:04:00 -0800 | [diff] [blame] | 942 | animateScreenBrightness(brightness, |
Julius D'souza | feadad1 | 2016-08-05 14:34:38 -0700 | [diff] [blame] | 943 | slowChange ? mBrightnessRampRateSlow : mBrightnessRampRateFast); |
Prashant Malani | 3353824 | 2014-11-13 14:04:00 -0800 | [diff] [blame] | 944 | } |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 945 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 946 | if (!brightnessIsTemporary) { |
Kenny Guy | 53d0661 | 2018-01-30 14:19:13 +0000 | [diff] [blame] | 947 | notifyBrightnessChanged(brightness, userInitiatedChange, hadUserBrightnessPoint); |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 948 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 949 | |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | // Determine whether the display is ready for use in the newly requested state. |
| 953 | // Note that we do not wait for the brightness ramp animation to complete before |
| 954 | // reporting the display is ready because we only need to ensure the screen is in the |
| 955 | // right power state even as it continues to converge on the desired brightness. |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 956 | final boolean ready = mPendingScreenOnUnblocker == null && |
| 957 | (!mColorFadeEnabled || |
| 958 | (!mColorFadeOnAnimator.isStarted() && !mColorFadeOffAnimator.isStarted())) |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 959 | && mPowerState.waitUntilClean(mCleanListener); |
| 960 | final boolean finished = ready |
| 961 | && !mScreenBrightnessRampAnimator.isAnimating(); |
| 962 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 963 | // Notify policy about screen turned on. |
| 964 | if (ready && state != Display.STATE_OFF |
| 965 | && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_ON) { |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 966 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_ON); |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 967 | mWindowManagerPolicy.screenTurnedOn(); |
| 968 | } |
| 969 | |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 970 | // Grab a wake lock if we have unfinished business. |
| 971 | if (!finished && !mUnfinishedBusiness) { |
| 972 | if (DEBUG) { |
| 973 | Slog.d(TAG, "Unfinished business..."); |
| 974 | } |
| 975 | mCallbacks.acquireSuspendBlocker(); |
| 976 | mUnfinishedBusiness = true; |
| 977 | } |
| 978 | |
| 979 | // Notify the power manager when ready. |
| 980 | if (ready && mustNotify) { |
| 981 | // Send state change. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 982 | synchronized (mLock) { |
| 983 | if (!mPendingRequestChangedLocked) { |
| 984 | mDisplayReadyLocked = true; |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 985 | |
| 986 | if (DEBUG) { |
| 987 | Slog.d(TAG, "Display ready!"); |
| 988 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 989 | } |
| 990 | } |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 991 | sendOnStateChangedWithWakelock(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 992 | } |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 993 | |
| 994 | // Release the wake lock when we have no unfinished business. |
| 995 | if (finished && mUnfinishedBusiness) { |
| 996 | if (DEBUG) { |
| 997 | Slog.d(TAG, "Finished business..."); |
| 998 | } |
| 999 | mUnfinishedBusiness = false; |
| 1000 | mCallbacks.releaseSuspendBlocker(); |
| 1001 | } |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 1002 | |
| 1003 | // Record if dozing for future comparison. |
| 1004 | mDozing = state != Display.STATE_ON; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1007 | @Override |
| 1008 | public void updateBrightness() { |
| 1009 | sendUpdatePowerState(); |
| 1010 | } |
| 1011 | |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 1012 | public void setBrightnessConfiguration(BrightnessConfiguration c) { |
| 1013 | Message msg = mHandler.obtainMessage(MSG_CONFIGURE_BRIGHTNESS, c); |
| 1014 | msg.sendToTarget(); |
| 1015 | } |
| 1016 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1017 | public void setTemporaryBrightness(int brightness) { |
| 1018 | Message msg = mHandler.obtainMessage(MSG_SET_TEMPORARY_BRIGHTNESS, |
| 1019 | brightness, 0 /*unused*/); |
| 1020 | msg.sendToTarget(); |
| 1021 | } |
| 1022 | |
| 1023 | public void setTemporaryAutoBrightnessAdjustment(float adjustment) { |
| 1024 | Message msg = mHandler.obtainMessage(MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT, |
| 1025 | Float.floatToIntBits(adjustment), 0 /*unused*/); |
| 1026 | msg.sendToTarget(); |
| 1027 | } |
| 1028 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 1029 | private void blockScreenOn() { |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1030 | if (mPendingScreenOnUnblocker == null) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1031 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0); |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1032 | mPendingScreenOnUnblocker = new ScreenOnUnblocker(); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1033 | mScreenOnBlockStartRealTime = SystemClock.elapsedRealtime(); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1034 | Slog.i(TAG, "Blocking screen on until initial contents have been drawn."); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | private void unblockScreenOn() { |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1039 | if (mPendingScreenOnUnblocker != null) { |
| 1040 | mPendingScreenOnUnblocker = null; |
Jeff Brown | 2d8a390 | 2014-03-11 23:02:35 -0700 | [diff] [blame] | 1041 | long delay = SystemClock.elapsedRealtime() - mScreenOnBlockStartRealTime; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 1042 | Slog.i(TAG, "Unblocked screen on after " + delay + " ms"); |
| 1043 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1047 | private void blockScreenOff() { |
| 1048 | if (mPendingScreenOffUnblocker == null) { |
| 1049 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_OFF_BLOCKED_TRACE_NAME, 0); |
| 1050 | mPendingScreenOffUnblocker = new ScreenOffUnblocker(); |
| 1051 | mScreenOffBlockStartRealTime = SystemClock.elapsedRealtime(); |
| 1052 | Slog.i(TAG, "Blocking screen off"); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | private void unblockScreenOff() { |
| 1057 | if (mPendingScreenOffUnblocker != null) { |
| 1058 | mPendingScreenOffUnblocker = null; |
| 1059 | long delay = SystemClock.elapsedRealtime() - mScreenOffBlockStartRealTime; |
| 1060 | Slog.i(TAG, "Unblocked screen off after " + delay + " ms"); |
| 1061 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_OFF_BLOCKED_TRACE_NAME, 0); |
| 1062 | } |
| 1063 | } |
| 1064 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1065 | private boolean setScreenState(int state) { |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1066 | return setScreenState(state, false /*reportOnly*/); |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1069 | private boolean setScreenState(int state, boolean reportOnly) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1070 | final boolean isOff = (state == Display.STATE_OFF); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 1071 | if (mPowerState.getScreenState() != state) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1072 | |
| 1073 | // If we are trying to turn screen off, give policy a chance to do something before we |
| 1074 | // actually turn the screen off. |
| 1075 | if (isOff && !mScreenOffBecauseOfProximity) { |
| 1076 | if (mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_ON) { |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1077 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_TURNING_OFF); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1078 | blockScreenOff(); |
| 1079 | mWindowManagerPolicy.screenTurningOff(mPendingScreenOffUnblocker); |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1080 | unblockScreenOff(); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1081 | } else if (mPendingScreenOffUnblocker != null) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1082 | // Abort doing the state change until screen off is unblocked. |
| 1083 | return false; |
| 1084 | } |
| 1085 | } |
| 1086 | |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1087 | if (!reportOnly) { |
Michael Wright | b0a1d3d | 2017-09-22 15:05:02 +0100 | [diff] [blame] | 1088 | Trace.traceCounter(Trace.TRACE_TAG_POWER, "ScreenState", state); |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1089 | mPowerState.setScreenState(state); |
| 1090 | // Tell battery stats about the transition. |
| 1091 | try { |
| 1092 | mBatteryStats.noteScreenState(state); |
| 1093 | } catch (RemoteException ex) { |
| 1094 | // same process |
| 1095 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } |
Jeff Brown | e1633ad | 2015-06-22 19:24:24 -0700 | [diff] [blame] | 1098 | |
| 1099 | // Tell the window manager policy when the screen is turned off or on unless it's due |
| 1100 | // to the proximity sensor. We temporarily block turning the screen on until the |
| 1101 | // window manager is ready by leaving a black surface covering the screen. |
| 1102 | // This surface is essentially the final state of the color fade animation and |
| 1103 | // it is only removed once the window manager tells us that the activity has |
| 1104 | // finished drawing underneath. |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 1105 | if (isOff && mReportedScreenStateToPolicy != REPORTED_TO_POLICY_SCREEN_OFF |
| 1106 | && !mScreenOffBecauseOfProximity) { |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1107 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_OFF); |
Jeff Brown | e1633ad | 2015-06-22 19:24:24 -0700 | [diff] [blame] | 1108 | unblockScreenOn(); |
| 1109 | mWindowManagerPolicy.screenTurnedOff(); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1110 | } else if (!isOff |
| 1111 | && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_OFF) { |
| 1112 | |
| 1113 | // We told policy already that screen was turning off, but now we changed our minds. |
| 1114 | // Complete the full state transition on -> turningOff -> off. |
| 1115 | unblockScreenOff(); |
| 1116 | mWindowManagerPolicy.screenTurnedOff(); |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1117 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_OFF); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1118 | } |
| 1119 | if (!isOff && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_OFF) { |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1120 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_TURNING_ON); |
Jeff Brown | e1633ad | 2015-06-22 19:24:24 -0700 | [diff] [blame] | 1121 | if (mPowerState.getColorFadeLevel() == 0.0f) { |
| 1122 | blockScreenOn(); |
| 1123 | } else { |
| 1124 | unblockScreenOn(); |
| 1125 | } |
| 1126 | mWindowManagerPolicy.screenTurningOn(mPendingScreenOnUnblocker); |
| 1127 | } |
| 1128 | |
| 1129 | // Return true if the screen isn't blocked. |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1130 | return mPendingScreenOnUnblocker == null; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1133 | private void setReportedScreenState(int state) { |
| 1134 | Trace.traceCounter(Trace.TRACE_TAG_POWER, "ReportedScreenStateToPolicy", state); |
| 1135 | mReportedScreenStateToPolicy = state; |
| 1136 | } |
| 1137 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1138 | private int clampScreenBrightness(int value) { |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1139 | return MathUtils.constrain( |
| 1140 | value, mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1143 | private void animateScreenBrightness(int target, int rate) { |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 1144 | if (DEBUG) { |
| 1145 | Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate); |
| 1146 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1147 | if (mScreenBrightnessRampAnimator.animateTo(target, rate)) { |
Michael Wright | a9f37ab | 2017-08-15 17:14:20 +0100 | [diff] [blame] | 1148 | Trace.traceCounter(Trace.TRACE_TAG_POWER, "TargetScreenBrightness", target); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1149 | try { |
| 1150 | mBatteryStats.noteScreenBrightness(target); |
| 1151 | } catch (RemoteException ex) { |
| 1152 | // same process |
| 1153 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1157 | private void animateScreenStateChange(int target, boolean performScreenOffTransition) { |
| 1158 | // If there is already an animation in progress, don't interfere with it. |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 1159 | if (mColorFadeEnabled && |
| 1160 | (mColorFadeOnAnimator.isStarted() || mColorFadeOffAnimator.isStarted())) { |
Chong Zhang | b131c70 | 2016-02-16 16:31:48 -0800 | [diff] [blame] | 1161 | if (target != Display.STATE_ON) { |
| 1162 | return; |
| 1163 | } |
| 1164 | // If display state changed to on, proceed and stop the color fade and turn screen on. |
| 1165 | mPendingScreenOff = false; |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1168 | if (mDisplayBlanksAfterDozeConfig |
| 1169 | && Display.isDozeState(mPowerState.getScreenState()) |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 1170 | && !Display.isDozeState(target)) { |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 1171 | // Skip the screen off animation and add a black surface to hide the |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1172 | // contents of the screen. |
| 1173 | mPowerState.prepareColorFade(mContext, |
| 1174 | mColorFadeFadesConfig ? ColorFade.MODE_FADE : ColorFade.MODE_WARM_UP); |
Lucas Dupin | a84952b | 2017-08-15 15:56:50 -0700 | [diff] [blame] | 1175 | if (mColorFadeOffAnimator != null) { |
| 1176 | mColorFadeOffAnimator.end(); |
| 1177 | } |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1178 | // Some display hardware will blank itself on the transition between doze and non-doze |
| 1179 | // but still on display states. In this case we want to report to policy that the |
| 1180 | // display has turned off so it can prepare the appropriate power on animation, but we |
| 1181 | // don't want to actually transition to the fully off state since that takes |
| 1182 | // significantly longer to transition from. |
| 1183 | setScreenState(Display.STATE_OFF, target != Display.STATE_OFF /*reportOnly*/); |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1186 | // If we were in the process of turning off the screen but didn't quite |
| 1187 | // finish. Then finish up now to prevent a jarring transition back |
| 1188 | // to screen on if we skipped blocking screen on as usual. |
| 1189 | if (mPendingScreenOff && target != Display.STATE_OFF) { |
| 1190 | setScreenState(Display.STATE_OFF); |
| 1191 | mPendingScreenOff = false; |
Michael Lentine | d73854d | 2015-09-25 10:55:26 -0700 | [diff] [blame] | 1192 | mPowerState.dismissColorFadeResources(); |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | if (target == Display.STATE_ON) { |
| 1196 | // Want screen on. The contents of the screen may not yet |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1197 | // be visible if the color fade has not been dismissed because |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1198 | // its last frame of animation is solid black. |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1199 | if (!setScreenState(Display.STATE_ON)) { |
| 1200 | return; // screen on blocked |
| 1201 | } |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 1202 | if (USE_COLOR_FADE_ON_ANIMATION && mColorFadeEnabled && mPowerRequest.isBrightOrDim()) { |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1203 | // Perform screen on animation. |
| 1204 | if (mPowerState.getColorFadeLevel() == 1.0f) { |
| 1205 | mPowerState.dismissColorFade(); |
| 1206 | } else if (mPowerState.prepareColorFade(mContext, |
| 1207 | mColorFadeFadesConfig ? |
| 1208 | ColorFade.MODE_FADE : |
| 1209 | ColorFade.MODE_WARM_UP)) { |
| 1210 | mColorFadeOnAnimator.start(); |
| 1211 | } else { |
| 1212 | mColorFadeOnAnimator.end(); |
| 1213 | } |
| 1214 | } else { |
| 1215 | // Skip screen on animation. |
| 1216 | mPowerState.setColorFadeLevel(1.0f); |
| 1217 | mPowerState.dismissColorFade(); |
| 1218 | } |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 1219 | } else if (target == Display.STATE_VR) { |
| 1220 | // Wait for brightness animation to complete beforehand when entering VR |
| 1221 | // from screen on to prevent a perceptible jump because brightness may operate |
| 1222 | // differently when the display is configured for dozing. |
| 1223 | if (mScreenBrightnessRampAnimator.isAnimating() |
| 1224 | && mPowerState.getScreenState() == Display.STATE_ON) { |
| 1225 | return; |
| 1226 | } |
| 1227 | |
| 1228 | // Set screen state. |
| 1229 | if (!setScreenState(Display.STATE_VR)) { |
| 1230 | return; // screen on blocked |
| 1231 | } |
| 1232 | |
| 1233 | // Dismiss the black surface without fanfare. |
| 1234 | mPowerState.setColorFadeLevel(1.0f); |
| 1235 | mPowerState.dismissColorFade(); |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1236 | } else if (target == Display.STATE_DOZE) { |
| 1237 | // Want screen dozing. |
| 1238 | // Wait for brightness animation to complete beforehand when entering doze |
| 1239 | // from screen on to prevent a perceptible jump because brightness may operate |
| 1240 | // differently when the display is configured for dozing. |
| 1241 | if (mScreenBrightnessRampAnimator.isAnimating() |
| 1242 | && mPowerState.getScreenState() == Display.STATE_ON) { |
| 1243 | return; |
| 1244 | } |
| 1245 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1246 | // Set screen state. |
| 1247 | if (!setScreenState(Display.STATE_DOZE)) { |
| 1248 | return; // screen on blocked |
| 1249 | } |
| 1250 | |
| 1251 | // Dismiss the black surface without fanfare. |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1252 | mPowerState.setColorFadeLevel(1.0f); |
| 1253 | mPowerState.dismissColorFade(); |
| 1254 | } else if (target == Display.STATE_DOZE_SUSPEND) { |
| 1255 | // Want screen dozing and suspended. |
| 1256 | // Wait for brightness animation to complete beforehand unless already |
| 1257 | // suspended because we may not be able to change it after suspension. |
| 1258 | if (mScreenBrightnessRampAnimator.isAnimating() |
| 1259 | && mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) { |
| 1260 | return; |
| 1261 | } |
| 1262 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1263 | // If not already suspending, temporarily set the state to doze until the |
| 1264 | // screen on is unblocked, then suspend. |
| 1265 | if (mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) { |
| 1266 | if (!setScreenState(Display.STATE_DOZE)) { |
| 1267 | return; // screen on blocked |
| 1268 | } |
| 1269 | setScreenState(Display.STATE_DOZE_SUSPEND); // already on so can't block |
| 1270 | } |
| 1271 | |
| 1272 | // Dismiss the black surface without fanfare. |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1273 | mPowerState.setColorFadeLevel(1.0f); |
| 1274 | mPowerState.dismissColorFade(); |
Chris Phoenix | 10a4a64 | 2017-09-25 13:21:00 -0700 | [diff] [blame] | 1275 | } else if (target == Display.STATE_ON_SUSPEND) { |
| 1276 | // Want screen full-power and suspended. |
| 1277 | // Wait for brightness animation to complete beforehand unless already |
| 1278 | // suspended because we may not be able to change it after suspension. |
| 1279 | if (mScreenBrightnessRampAnimator.isAnimating() |
| 1280 | && mPowerState.getScreenState() != Display.STATE_ON_SUSPEND) { |
| 1281 | return; |
| 1282 | } |
| 1283 | |
| 1284 | // If not already suspending, temporarily set the state to on until the |
| 1285 | // screen on is unblocked, then suspend. |
| 1286 | if (mPowerState.getScreenState() != Display.STATE_ON_SUSPEND) { |
| 1287 | if (!setScreenState(Display.STATE_ON)) { |
| 1288 | return; |
| 1289 | } |
| 1290 | setScreenState(Display.STATE_ON_SUSPEND); |
| 1291 | } |
| 1292 | |
| 1293 | // Dismiss the black surface without fanfare. |
| 1294 | mPowerState.setColorFadeLevel(1.0f); |
| 1295 | mPowerState.dismissColorFade(); |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1296 | } else { |
| 1297 | // Want screen off. |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1298 | mPendingScreenOff = true; |
Narayan Kamath | 3881998 | 2017-07-06 18:15:30 +0100 | [diff] [blame] | 1299 | if (!mColorFadeEnabled) { |
| 1300 | mPowerState.setColorFadeLevel(0.0f); |
| 1301 | } |
| 1302 | |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1303 | if (mPowerState.getColorFadeLevel() == 0.0f) { |
| 1304 | // Turn the screen off. |
| 1305 | // A black surface is already hiding the contents of the screen. |
| 1306 | setScreenState(Display.STATE_OFF); |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1307 | mPendingScreenOff = false; |
Michael Lentine | d73854d | 2015-09-25 10:55:26 -0700 | [diff] [blame] | 1308 | mPowerState.dismissColorFadeResources(); |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1309 | } else if (performScreenOffTransition |
| 1310 | && mPowerState.prepareColorFade(mContext, |
| 1311 | mColorFadeFadesConfig ? |
| 1312 | ColorFade.MODE_FADE : ColorFade.MODE_COOL_DOWN) |
| 1313 | && mPowerState.getScreenState() != Display.STATE_OFF) { |
| 1314 | // Perform the screen off animation. |
| 1315 | mColorFadeOffAnimator.start(); |
| 1316 | } else { |
| 1317 | // Skip the screen off animation and add a black surface to hide the |
| 1318 | // contents of the screen. |
| 1319 | mColorFadeOffAnimator.end(); |
| 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1324 | private final Runnable mCleanListener = new Runnable() { |
| 1325 | @Override |
| 1326 | public void run() { |
| 1327 | sendUpdatePowerState(); |
| 1328 | } |
| 1329 | }; |
| 1330 | |
| 1331 | private void setProximitySensorEnabled(boolean enable) { |
| 1332 | if (enable) { |
| 1333 | if (!mProximitySensorEnabled) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1334 | // Register the listener. |
| 1335 | // Proximity sensor state already cleared initially. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1336 | mProximitySensorEnabled = true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1337 | mSensorManager.registerListener(mProximitySensorListener, mProximitySensor, |
| 1338 | SensorManager.SENSOR_DELAY_NORMAL, mHandler); |
| 1339 | } |
| 1340 | } else { |
| 1341 | if (mProximitySensorEnabled) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1342 | // Unregister the listener. |
| 1343 | // Clear the proximity sensor state for next time. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1344 | mProximitySensorEnabled = false; |
| 1345 | mProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1346 | mPendingProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1347 | mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
| 1348 | mSensorManager.unregisterListener(mProximitySensorListener); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1349 | clearPendingProximityDebounceTime(); // release wake lock (must be last) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | private void handleProximitySensorEvent(long time, boolean positive) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1355 | if (mProximitySensorEnabled) { |
| 1356 | if (mPendingProximity == PROXIMITY_NEGATIVE && !positive) { |
| 1357 | return; // no change |
| 1358 | } |
| 1359 | if (mPendingProximity == PROXIMITY_POSITIVE && positive) { |
| 1360 | return; // no change |
| 1361 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1362 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1363 | // Only accept a proximity sensor reading if it remains |
| 1364 | // stable for the entire debounce delay. We hold a wake lock while |
| 1365 | // debouncing the sensor. |
| 1366 | mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
| 1367 | if (positive) { |
| 1368 | mPendingProximity = PROXIMITY_POSITIVE; |
| 1369 | setPendingProximityDebounceTime( |
| 1370 | time + PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY); // acquire wake lock |
| 1371 | } else { |
| 1372 | mPendingProximity = PROXIMITY_NEGATIVE; |
| 1373 | setPendingProximityDebounceTime( |
| 1374 | time + PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY); // acquire wake lock |
| 1375 | } |
| 1376 | |
| 1377 | // Debounce the new sensor reading. |
| 1378 | debounceProximitySensor(); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1379 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | private void debounceProximitySensor() { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1383 | if (mProximitySensorEnabled |
| 1384 | && mPendingProximity != PROXIMITY_UNKNOWN |
| 1385 | && mPendingProximityDebounceTime >= 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1386 | final long now = SystemClock.uptimeMillis(); |
| 1387 | if (mPendingProximityDebounceTime <= now) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1388 | // Sensor reading accepted. Apply the change then release the wake lock. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1389 | mProximity = mPendingProximity; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1390 | updatePowerState(); |
| 1391 | clearPendingProximityDebounceTime(); // release wake lock (must be last) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1392 | } else { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1393 | // Need to wait a little longer. |
| 1394 | // Debounce again later. We continue holding a wake lock while waiting. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1395 | Message msg = mHandler.obtainMessage(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1396 | mHandler.sendMessageAtTime(msg, mPendingProximityDebounceTime); |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1401 | private void clearPendingProximityDebounceTime() { |
| 1402 | if (mPendingProximityDebounceTime >= 0) { |
| 1403 | mPendingProximityDebounceTime = -1; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1404 | mCallbacks.releaseSuspendBlocker(); // release wake lock |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | private void setPendingProximityDebounceTime(long debounceTime) { |
| 1409 | if (mPendingProximityDebounceTime < 0) { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1410 | mCallbacks.acquireSuspendBlocker(); // acquire wake lock |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1411 | } |
| 1412 | mPendingProximityDebounceTime = debounceTime; |
| 1413 | } |
| 1414 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1415 | private void sendOnStateChangedWithWakelock() { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1416 | mCallbacks.acquireSuspendBlocker(); |
| 1417 | mHandler.post(mOnStateChangedRunnable); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Kenny Guy | ecc978f | 2018-03-14 17:30:20 +0000 | [diff] [blame] | 1420 | private void handleSettingsChange(boolean userSwitch) { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1421 | mPendingScreenBrightnessSetting = getScreenBrightnessSetting(); |
Kenny Guy | ecc978f | 2018-03-14 17:30:20 +0000 | [diff] [blame] | 1422 | if (userSwitch) { |
| 1423 | // Don't treat user switches as user initiated change. |
| 1424 | mCurrentScreenBrightnessSetting = mPendingScreenBrightnessSetting; |
| 1425 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1426 | mPendingAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting(); |
| 1427 | // We don't bother with a pending variable for VR screen brightness since we just |
| 1428 | // immediately adapt to it. |
| 1429 | mScreenBrightnessForVr = getScreenBrightnessForVrSetting(); |
| 1430 | sendUpdatePowerState(); |
| 1431 | } |
| 1432 | |
| 1433 | private float getAutoBrightnessAdjustmentSetting() { |
| 1434 | final float adj = Settings.System.getFloatForUser(mContext.getContentResolver(), |
| 1435 | Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.0f, UserHandle.USER_CURRENT); |
| 1436 | return Float.isNaN(adj) ? 0.0f : clampAutoBrightnessAdjustment(adj); |
| 1437 | } |
| 1438 | |
| 1439 | private int getScreenBrightnessSetting() { |
| 1440 | final int brightness = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 1441 | Settings.System.SCREEN_BRIGHTNESS, mScreenBrightnessDefault, |
| 1442 | UserHandle.USER_CURRENT); |
| 1443 | return clampAbsoluteBrightness(brightness); |
| 1444 | } |
| 1445 | |
| 1446 | private int getScreenBrightnessForVrSetting() { |
| 1447 | final int brightness = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 1448 | Settings.System.SCREEN_BRIGHTNESS_FOR_VR, mScreenBrightnessForVrDefault, |
| 1449 | UserHandle.USER_CURRENT); |
| 1450 | return clampAbsoluteBrightness(brightness); |
| 1451 | } |
| 1452 | |
| 1453 | private void putScreenBrightnessSetting(int brightness) { |
| 1454 | mCurrentScreenBrightnessSetting = brightness; |
| 1455 | Settings.System.putIntForUser(mContext.getContentResolver(), |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 1456 | Settings.System.SCREEN_BRIGHTNESS, brightness, UserHandle.USER_CURRENT); |
| 1457 | } |
| 1458 | |
| 1459 | private void putAutoBrightnessAdjustmentSetting(float adjustment) { |
| 1460 | mAutoBrightnessAdjustment = adjustment; |
| 1461 | Settings.System.putFloatForUser(mContext.getContentResolver(), |
| 1462 | Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, adjustment, UserHandle.USER_CURRENT); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | private boolean updateAutoBrightnessAdjustment() { |
| 1466 | if (Float.isNaN(mPendingAutoBrightnessAdjustment)) { |
| 1467 | return false; |
| 1468 | } |
| 1469 | if (mAutoBrightnessAdjustment == mPendingAutoBrightnessAdjustment) { |
| 1470 | return false; |
| 1471 | } |
| 1472 | mAutoBrightnessAdjustment = mPendingAutoBrightnessAdjustment; |
| 1473 | mPendingAutoBrightnessAdjustment = Float.NaN; |
| 1474 | return true; |
| 1475 | } |
| 1476 | |
| 1477 | private boolean updateUserSetScreenBrightness() { |
| 1478 | if (mPendingScreenBrightnessSetting < 0) { |
| 1479 | return false; |
| 1480 | } |
| 1481 | if (mCurrentScreenBrightnessSetting == mPendingScreenBrightnessSetting) { |
Michael Wright | 44dc8b1 | 2018-01-25 23:25:20 +0000 | [diff] [blame] | 1482 | mPendingScreenBrightnessSetting = -1; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1483 | return false; |
| 1484 | } |
Kenny Guy | ad9a6ea | 2018-01-31 15:57:15 +0000 | [diff] [blame] | 1485 | mCurrentScreenBrightnessSetting = mPendingScreenBrightnessSetting; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1486 | mLastUserSetScreenBrightness = mPendingScreenBrightnessSetting; |
| 1487 | mPendingScreenBrightnessSetting = -1; |
| 1488 | return true; |
| 1489 | } |
| 1490 | |
Kenny Guy | 53d0661 | 2018-01-30 14:19:13 +0000 | [diff] [blame] | 1491 | private void notifyBrightnessChanged(int brightness, boolean userInitiated, |
| 1492 | boolean hadUserDataPoint) { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1493 | final float brightnessInNits = convertToNits(brightness); |
Kenny Guy | ad9a6ea | 2018-01-31 15:57:15 +0000 | [diff] [blame] | 1494 | if (mPowerRequest.useAutoBrightness && brightnessInNits >= 0.0f |
| 1495 | && mAutomaticBrightnessController != null) { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1496 | // We only want to track changes on devices that can actually map the display backlight |
| 1497 | // values into a physical brightness unit since the value provided by the API is in |
| 1498 | // nits and not using the arbitrary backlight units. |
Kenny Guy | 53d0661 | 2018-01-30 14:19:13 +0000 | [diff] [blame] | 1499 | final float powerFactor = mPowerRequest.lowPowerMode |
| 1500 | ? mPowerRequest.screenLowPowerBrightnessFactor |
| 1501 | : 1.0f; |
| 1502 | mBrightnessTracker.notifyBrightnessChanged(brightnessInNits, userInitiated, |
| 1503 | powerFactor, hadUserDataPoint, |
| 1504 | mAutomaticBrightnessController.isDefaultConfig()); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | private float convertToNits(int backlight) { |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 1509 | if (mBrightnessMapper != null) { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1510 | return mBrightnessMapper.convertToNits(backlight); |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 1511 | } else { |
| 1512 | return -1.0f; |
| 1513 | } |
| 1514 | } |
| 1515 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1516 | private final Runnable mOnStateChangedRunnable = new Runnable() { |
| 1517 | @Override |
| 1518 | public void run() { |
| 1519 | mCallbacks.onStateChanged(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1520 | mCallbacks.releaseSuspendBlocker(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1521 | } |
| 1522 | }; |
| 1523 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1524 | private void sendOnProximityPositiveWithWakelock() { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1525 | mCallbacks.acquireSuspendBlocker(); |
| 1526 | mHandler.post(mOnProximityPositiveRunnable); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | private final Runnable mOnProximityPositiveRunnable = new Runnable() { |
| 1530 | @Override |
| 1531 | public void run() { |
| 1532 | mCallbacks.onProximityPositive(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1533 | mCallbacks.releaseSuspendBlocker(); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1534 | } |
| 1535 | }; |
| 1536 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1537 | private void sendOnProximityNegativeWithWakelock() { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1538 | mCallbacks.acquireSuspendBlocker(); |
| 1539 | mHandler.post(mOnProximityNegativeRunnable); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | private final Runnable mOnProximityNegativeRunnable = new Runnable() { |
| 1543 | @Override |
| 1544 | public void run() { |
| 1545 | mCallbacks.onProximityNegative(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1546 | mCallbacks.releaseSuspendBlocker(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1547 | } |
| 1548 | }; |
| 1549 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1550 | public void dump(final PrintWriter pw) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1551 | synchronized (mLock) { |
| 1552 | pw.println(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1553 | pw.println("Display Power Controller Locked State:"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1554 | pw.println(" mDisplayReadyLocked=" + mDisplayReadyLocked); |
| 1555 | pw.println(" mPendingRequestLocked=" + mPendingRequestLocked); |
| 1556 | pw.println(" mPendingRequestChangedLocked=" + mPendingRequestChangedLocked); |
| 1557 | pw.println(" mPendingWaitForNegativeProximityLocked=" |
| 1558 | + mPendingWaitForNegativeProximityLocked); |
| 1559 | pw.println(" mPendingUpdatePowerStateLocked=" + mPendingUpdatePowerStateLocked); |
| 1560 | } |
| 1561 | |
| 1562 | pw.println(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1563 | pw.println("Display Power Controller Configuration:"); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1564 | pw.println(" mScreenBrightnessDozeConfig=" + mScreenBrightnessDozeConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1565 | pw.println(" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig); |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 1566 | pw.println(" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum); |
| 1567 | pw.println(" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum); |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1568 | pw.println(" mUseSoftwareAutoBrightnessConfig=" + mUseSoftwareAutoBrightnessConfig); |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 1569 | pw.println(" mAllowAutoBrightnessWhileDozingConfig=" + |
| 1570 | mAllowAutoBrightnessWhileDozingConfig); |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1571 | pw.println(" mColorFadeFadesConfig=" + mColorFadeFadesConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1572 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1573 | mHandler.runWithScissors(new Runnable() { |
| 1574 | @Override |
| 1575 | public void run() { |
| 1576 | dumpLocal(pw); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1577 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1578 | }, 1000); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | private void dumpLocal(PrintWriter pw) { |
| 1582 | pw.println(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1583 | pw.println("Display Power Controller Thread State:"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1584 | pw.println(" mPowerRequest=" + mPowerRequest); |
| 1585 | pw.println(" mWaitingForNegativeProximity=" + mWaitingForNegativeProximity); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1586 | pw.println(" mProximitySensor=" + mProximitySensor); |
| 1587 | pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled); |
| 1588 | pw.println(" mProximityThreshold=" + mProximityThreshold); |
| 1589 | pw.println(" mProximity=" + proximityToString(mProximity)); |
| 1590 | pw.println(" mPendingProximity=" + proximityToString(mPendingProximity)); |
| 1591 | pw.println(" mPendingProximityDebounceTime=" |
| 1592 | + TimeUtils.formatUptime(mPendingProximityDebounceTime)); |
| 1593 | pw.println(" mScreenOffBecauseOfProximity=" + mScreenOffBecauseOfProximity); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1594 | pw.println(" mLastUserSetScreenBrightness=" + mLastUserSetScreenBrightness); |
| 1595 | pw.println(" mCurrentScreenBrightnessSetting=" + mCurrentScreenBrightnessSetting); |
| 1596 | pw.println(" mPendingScreenBrightnessSetting=" + mPendingScreenBrightnessSetting); |
| 1597 | pw.println(" mAutoBrightnessAdjustment=" + mAutoBrightnessAdjustment); |
| 1598 | pw.println(" mPendingAutoBrightnessAdjustment=" + mPendingAutoBrightnessAdjustment); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1599 | pw.println(" mAppliedAutoBrightness=" + mAppliedAutoBrightness); |
| 1600 | pw.println(" mAppliedDimming=" + mAppliedDimming); |
| 1601 | pw.println(" mAppliedLowPower=" + mAppliedLowPower); |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1602 | pw.println(" mPendingScreenOnUnblocker=" + mPendingScreenOnUnblocker); |
| 1603 | pw.println(" mPendingScreenOff=" + mPendingScreenOff); |
Michael Wright | c3e6af8 | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1604 | pw.println(" mReportedToPolicy=" + |
| 1605 | reportedToPolicyToString(mReportedScreenStateToPolicy)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1606 | |
Jeff Brown | 4255869 | 2014-05-20 22:02:46 -0700 | [diff] [blame] | 1607 | pw.println(" mScreenBrightnessRampAnimator.isAnimating()=" + |
| 1608 | mScreenBrightnessRampAnimator.isAnimating()); |
| 1609 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 1610 | if (mColorFadeOnAnimator != null) { |
| 1611 | pw.println(" mColorFadeOnAnimator.isStarted()=" + |
| 1612 | mColorFadeOnAnimator.isStarted()); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1613 | } |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 1614 | if (mColorFadeOffAnimator != null) { |
| 1615 | pw.println(" mColorFadeOffAnimator.isStarted()=" + |
| 1616 | mColorFadeOffAnimator.isStarted()); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | if (mPowerState != null) { |
| 1620 | mPowerState.dump(pw); |
| 1621 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1622 | |
| 1623 | if (mAutomaticBrightnessController != null) { |
| 1624 | mAutomaticBrightnessController.dump(pw); |
| 1625 | } |
| 1626 | |
Michael Wright | 144aac9 | 2017-12-21 18:37:41 +0000 | [diff] [blame] | 1627 | if (mBrightnessTracker != null) { |
| 1628 | pw.println(); |
| 1629 | mBrightnessTracker.dump(pw); |
| 1630 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | private static String proximityToString(int state) { |
| 1634 | switch (state) { |
| 1635 | case PROXIMITY_UNKNOWN: |
| 1636 | return "Unknown"; |
| 1637 | case PROXIMITY_NEGATIVE: |
| 1638 | return "Negative"; |
| 1639 | case PROXIMITY_POSITIVE: |
| 1640 | return "Positive"; |
| 1641 | default: |
| 1642 | return Integer.toString(state); |
| 1643 | } |
| 1644 | } |
| 1645 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 1646 | private static String reportedToPolicyToString(int state) { |
| 1647 | switch (state) { |
| 1648 | case REPORTED_TO_POLICY_SCREEN_OFF: |
| 1649 | return "REPORTED_TO_POLICY_SCREEN_OFF"; |
| 1650 | case REPORTED_TO_POLICY_SCREEN_TURNING_ON: |
| 1651 | return "REPORTED_TO_POLICY_SCREEN_TURNING_ON"; |
| 1652 | case REPORTED_TO_POLICY_SCREEN_ON: |
| 1653 | return "REPORTED_TO_POLICY_SCREEN_ON"; |
| 1654 | default: |
| 1655 | return Integer.toString(state); |
| 1656 | } |
| 1657 | } |
| 1658 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1659 | private static int clampAbsoluteBrightness(int value) { |
| 1660 | return MathUtils.constrain(value, PowerManager.BRIGHTNESS_OFF, PowerManager.BRIGHTNESS_ON); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1663 | private static float clampAutoBrightnessAdjustment(float value) { |
| 1664 | return MathUtils.constrain(value, -1.0f, 1.0f); |
| 1665 | } |
| 1666 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1667 | private final class DisplayControllerHandler extends Handler { |
| 1668 | public DisplayControllerHandler(Looper looper) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 1669 | super(looper, null, true /*async*/); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | @Override |
| 1673 | public void handleMessage(Message msg) { |
| 1674 | switch (msg.what) { |
| 1675 | case MSG_UPDATE_POWER_STATE: |
| 1676 | updatePowerState(); |
| 1677 | break; |
| 1678 | |
| 1679 | case MSG_PROXIMITY_SENSOR_DEBOUNCED: |
| 1680 | debounceProximitySensor(); |
| 1681 | break; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1682 | |
| 1683 | case MSG_SCREEN_ON_UNBLOCKED: |
| 1684 | if (mPendingScreenOnUnblocker == msg.obj) { |
| 1685 | unblockScreenOn(); |
| 1686 | updatePowerState(); |
| 1687 | } |
| 1688 | break; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1689 | case MSG_SCREEN_OFF_UNBLOCKED: |
| 1690 | if (mPendingScreenOffUnblocker == msg.obj) { |
| 1691 | unblockScreenOff(); |
| 1692 | updatePowerState(); |
| 1693 | } |
| 1694 | break; |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 1695 | case MSG_CONFIGURE_BRIGHTNESS: |
Michael Wright | d5df361 | 2018-01-02 12:44:52 +0000 | [diff] [blame] | 1696 | mBrightnessConfiguration = (BrightnessConfiguration)msg.obj; |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 1697 | updatePowerState(); |
| 1698 | break; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1699 | |
| 1700 | case MSG_SET_TEMPORARY_BRIGHTNESS: |
| 1701 | // TODO: Should we have a a timeout for the temporary brightness? |
| 1702 | mTemporaryScreenBrightness = msg.arg1; |
| 1703 | updatePowerState(); |
| 1704 | break; |
| 1705 | |
| 1706 | case MSG_SET_TEMPORARY_AUTO_BRIGHTNESS_ADJUSTMENT: |
| 1707 | mTemporaryAutoBrightnessAdjustment = Float.intBitsToFloat(msg.arg1); |
| 1708 | updatePowerState(); |
| 1709 | break; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | private final SensorEventListener mProximitySensorListener = new SensorEventListener() { |
| 1715 | @Override |
| 1716 | public void onSensorChanged(SensorEvent event) { |
| 1717 | if (mProximitySensorEnabled) { |
| 1718 | final long time = SystemClock.uptimeMillis(); |
| 1719 | final float distance = event.values[0]; |
| 1720 | boolean positive = distance >= 0.0f && distance < mProximityThreshold; |
| 1721 | handleProximitySensorEvent(time, positive); |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | @Override |
| 1726 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 1727 | // Not used. |
| 1728 | } |
| 1729 | }; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1730 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1731 | |
| 1732 | private final class SettingsObserver extends ContentObserver { |
| 1733 | public SettingsObserver(Handler handler) { |
| 1734 | super(handler); |
| 1735 | } |
| 1736 | |
| 1737 | @Override |
| 1738 | public void onChange(boolean selfChange, Uri uri) { |
Kenny Guy | ecc978f | 2018-03-14 17:30:20 +0000 | [diff] [blame] | 1739 | handleSettingsChange(false /* userSwitch */); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1743 | private final class ScreenOnUnblocker implements WindowManagerPolicy.ScreenOnListener { |
| 1744 | @Override |
| 1745 | public void onScreenOn() { |
| 1746 | Message msg = mHandler.obtainMessage(MSG_SCREEN_ON_UNBLOCKED, this); |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1747 | mHandler.sendMessage(msg); |
| 1748 | } |
| 1749 | } |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1750 | |
| 1751 | private final class ScreenOffUnblocker implements WindowManagerPolicy.ScreenOffListener { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1752 | @Override |
| 1753 | public void onScreenOff() { |
| 1754 | Message msg = mHandler.obtainMessage(MSG_SCREEN_OFF_UNBLOCKED, this); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1755 | mHandler.sendMessage(msg); |
| 1756 | } |
| 1757 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1758 | } |