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