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