Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 17 | package com.android.server.display; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 18 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 19 | import com.android.internal.app.IBatteryStats; |
| 20 | import com.android.server.LocalServices; |
| 21 | import com.android.server.am.BatteryStatsService; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 22 | |
| 23 | import android.animation.Animator; |
| 24 | import android.animation.ObjectAnimator; |
| 25 | import android.content.Context; |
| 26 | import android.content.res.Resources; |
| 27 | import android.hardware.Sensor; |
| 28 | import android.hardware.SensorEvent; |
| 29 | import android.hardware.SensorEventListener; |
| 30 | import android.hardware.SensorManager; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 31 | import android.hardware.display.DisplayManagerInternal.DisplayPowerCallbacks; |
| 32 | import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.Looper; |
| 35 | import android.os.Message; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 36 | import android.os.PowerManager; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 37 | import android.os.RemoteException; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 38 | import android.os.SystemClock; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 39 | import android.os.Trace; |
Michael Wright | 41a5cdf | 2013-11-13 16:18:32 -0800 | [diff] [blame] | 40 | import android.util.MathUtils; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 41 | import android.util.Slog; |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 42 | import android.util.Spline; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 43 | import android.util.TimeUtils; |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 44 | import android.view.Display; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 45 | import android.view.WindowManagerPolicy; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 46 | |
| 47 | import java.io.PrintWriter; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Controls the power state of the display. |
| 51 | * |
| 52 | * Handles the proximity sensor, light sensor, and animations between states |
| 53 | * including the screen off animation. |
| 54 | * |
| 55 | * This component acts independently of the rest of the power manager service. |
| 56 | * In particular, it does not share any state and it only communicates |
| 57 | * via asynchronous callbacks to inform the power manager that something has |
| 58 | * changed. |
| 59 | * |
| 60 | * Everything this class does internally is serialized on its handler although |
| 61 | * it may be accessed by other threads from the outside. |
| 62 | * |
| 63 | * Note that the power manager service guarantees that it will hold a suspend |
| 64 | * blocker as long as the display is not ready. So most of the work done here |
| 65 | * does not need to worry about holding a suspend blocker unless it happens |
| 66 | * independently of the display ready signal. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 67 | * |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 68 | * For debugging, you can make the color fade and brightness animations run |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 69 | * slower by changing the "animator duration scale" option in Development Settings. |
| 70 | */ |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 71 | final class DisplayPowerController implements AutomaticBrightnessController.Callbacks { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 72 | private static final String TAG = "DisplayPowerController"; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 73 | private static final String SCREEN_ON_BLOCKED_TRACE_NAME = "Screen on blocked"; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 74 | private static final String SCREEN_OFF_BLOCKED_TRACE_NAME = "Screen off blocked"; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 75 | |
Julius D'souza | 428aed0 | 2016-08-07 19:08:30 -0700 | [diff] [blame] | 76 | private static final boolean DEBUG = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 77 | private static final boolean DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 78 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 79 | // If true, uses the color fade on animation. |
Jeff Brown | 13c589b | 2012-08-16 16:20:54 -0700 | [diff] [blame] | 80 | // We might want to turn this off if we cannot get a guarantee that the screen |
| 81 | // 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] | 82 | // screen state returns. Playing the animation can also be somewhat slow. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 83 | private static final boolean USE_COLOR_FADE_ON_ANIMATION = false; |
Jeff Brown | 13c589b | 2012-08-16 16:20:54 -0700 | [diff] [blame] | 84 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 85 | // The minimum reduction in brightness when dimmed. |
| 86 | private static final int SCREEN_DIM_MINIMUM_REDUCTION = 10; |
| 87 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 88 | private static final int COLOR_FADE_ON_ANIMATION_DURATION_MILLIS = 250; |
Michael Lentine | 0c9a62d | 2014-08-20 09:19:07 -0700 | [diff] [blame] | 89 | private static final int COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS = 400; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 90 | |
| 91 | private static final int MSG_UPDATE_POWER_STATE = 1; |
| 92 | private static final int MSG_PROXIMITY_SENSOR_DEBOUNCED = 2; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 93 | private static final int MSG_SCREEN_ON_UNBLOCKED = 3; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 94 | private static final int MSG_SCREEN_OFF_UNBLOCKED = 4; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 95 | |
| 96 | private static final int PROXIMITY_UNKNOWN = -1; |
| 97 | private static final int PROXIMITY_NEGATIVE = 0; |
| 98 | private static final int PROXIMITY_POSITIVE = 1; |
| 99 | |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 100 | // Proximity sensor debounce delay in milliseconds for positive or negative transitions. |
| 101 | private static final int PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY = 0; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 102 | private static final int PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY = 250; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 103 | |
| 104 | // Trigger proximity if distance is less than 5 cm. |
| 105 | private static final float TYPICAL_PROXIMITY_THRESHOLD = 5.0f; |
| 106 | |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 107 | // State machine constants for tracking initial brightness ramp skipping when enabled. |
| 108 | private static final int RAMP_STATE_SKIP_NONE = 0; |
| 109 | private static final int RAMP_STATE_SKIP_INITIAL = 1; |
| 110 | private static final int RAMP_STATE_SKIP_AUTOBRIGHT = 2; |
| 111 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 112 | private static final int REPORTED_TO_POLICY_SCREEN_OFF = 0; |
| 113 | private static final int REPORTED_TO_POLICY_SCREEN_TURNING_ON = 1; |
| 114 | private static final int REPORTED_TO_POLICY_SCREEN_ON = 2; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 115 | private static final int REPORTED_TO_POLICY_SCREEN_TURNING_OFF = 3; |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 116 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 117 | private final Object mLock = new Object(); |
| 118 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 119 | private final Context mContext; |
| 120 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 121 | // Our handler. |
| 122 | private final DisplayControllerHandler mHandler; |
| 123 | |
| 124 | // Asynchronous callbacks into the power manager service. |
| 125 | // Only invoked from the handler thread while no locks are held. |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 126 | private final DisplayPowerCallbacks mCallbacks; |
| 127 | |
| 128 | // Battery stats. |
| 129 | private final IBatteryStats mBatteryStats; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 130 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 131 | // The sensor manager. |
| 132 | private final SensorManager mSensorManager; |
| 133 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 134 | // The window manager policy. |
| 135 | private final WindowManagerPolicy mWindowManagerPolicy; |
| 136 | |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 137 | // The display blanker. |
| 138 | private final DisplayBlanker mBlanker; |
| 139 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 140 | // The proximity sensor, or null if not available or needed. |
| 141 | private Sensor mProximitySensor; |
| 142 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 143 | // The doze screen brightness. |
| 144 | private final int mScreenBrightnessDozeConfig; |
| 145 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 146 | // The dim screen brightness. |
| 147 | private final int mScreenBrightnessDimConfig; |
| 148 | |
Jeff Brown | 1bfd0f4 | 2014-08-22 01:59:06 -0700 | [diff] [blame] | 149 | // The minimum screen brightness to use in a very dark room. |
| 150 | private final int mScreenBrightnessDarkConfig; |
| 151 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 152 | // The minimum allowed brightness. |
| 153 | private final int mScreenBrightnessRangeMinimum; |
| 154 | |
| 155 | // The maximum allowed brightness. |
| 156 | private final int mScreenBrightnessRangeMaximum; |
| 157 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 158 | // True if auto-brightness should be used. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 159 | private boolean mUseSoftwareAutoBrightnessConfig; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 160 | |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 161 | // True if should use light sensor to automatically determine doze screen brightness. |
| 162 | private final boolean mAllowAutoBrightnessWhileDozingConfig; |
| 163 | |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 164 | // 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] | 165 | // a stylish color fade animation instead. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 166 | private boolean mColorFadeFadesConfig; |
Jeff Brown | a52772f | 2012-10-04 18:38:09 -0700 | [diff] [blame] | 167 | |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 168 | // True if we need to fake a transition to off when coming out of a doze state. |
| 169 | // Some display hardware will blank itself when coming out of doze in order to hide |
| 170 | // artifacts. For these displays we fake a transition into OFF so that policy can appropriately |
| 171 | // blank itself and begin an appropriate power on animation. |
| 172 | private boolean mDisplayBlanksAfterDozeConfig; |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 173 | |
Michael Wright | 65d3e70 | 2017-07-26 01:10:59 +0100 | [diff] [blame] | 174 | // True if there are only buckets of brightness values when the display is in the doze state, |
| 175 | // rather than a full range of values. If this is true, then we'll avoid animating the screen |
| 176 | // brightness since it'd likely be multiple jarring brightness transitions instead of just one |
| 177 | // to reach the final state. |
| 178 | private boolean mBrightnessBucketsInDozeConfig; |
| 179 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 180 | // The pending power request. |
| 181 | // Initially null until the first call to requestPowerState. |
| 182 | // Guarded by mLock. |
| 183 | private DisplayPowerRequest mPendingRequestLocked; |
| 184 | |
| 185 | // True if a request has been made to wait for the proximity sensor to go negative. |
| 186 | // Guarded by mLock. |
| 187 | private boolean mPendingWaitForNegativeProximityLocked; |
| 188 | |
| 189 | // True if the pending power request or wait for negative proximity flag |
| 190 | // has been changed since the last update occurred. |
| 191 | // Guarded by mLock. |
| 192 | private boolean mPendingRequestChangedLocked; |
| 193 | |
| 194 | // Set to true when the important parts of the pending power request have been applied. |
| 195 | // The important parts are mainly the screen state. Brightness changes may occur |
| 196 | // concurrently. |
| 197 | // Guarded by mLock. |
| 198 | private boolean mDisplayReadyLocked; |
| 199 | |
| 200 | // Set to true if a power state update is required. |
| 201 | // Guarded by mLock. |
| 202 | private boolean mPendingUpdatePowerStateLocked; |
| 203 | |
| 204 | /* The following state must only be accessed by the handler thread. */ |
| 205 | |
| 206 | // The currently requested power state. |
| 207 | // The power controller will progressively update its internal state to match |
| 208 | // the requested power state. Initially null until the first update. |
| 209 | private DisplayPowerRequest mPowerRequest; |
| 210 | |
| 211 | // The current power state. |
| 212 | // Must only be accessed on the handler thread. |
| 213 | private DisplayPowerState mPowerState; |
| 214 | |
| 215 | // True if the device should wait for negative proximity sensor before |
| 216 | // waking up the screen. This is set to false as soon as a negative |
| 217 | // proximity sensor measurement is observed or when the device is forced to |
| 218 | // go to sleep by the user. While true, the screen remains off. |
| 219 | private boolean mWaitingForNegativeProximity; |
| 220 | |
| 221 | // The actual proximity sensor threshold value. |
| 222 | private float mProximityThreshold; |
| 223 | |
| 224 | // Set to true if the proximity sensor listener has been registered |
| 225 | // with the sensor manager. |
| 226 | private boolean mProximitySensorEnabled; |
| 227 | |
| 228 | // The debounced proximity sensor state. |
| 229 | private int mProximity = PROXIMITY_UNKNOWN; |
| 230 | |
| 231 | // The raw non-debounced proximity sensor state. |
| 232 | private int mPendingProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 233 | private long mPendingProximityDebounceTime = -1; // -1 if fully debounced |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 234 | |
| 235 | // True if the screen was turned off because of the proximity sensor. |
| 236 | // When the screen turns on again, we report user activity to the power manager. |
| 237 | private boolean mScreenOffBecauseOfProximity; |
| 238 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 239 | // The currently active screen on unblocker. This field is non-null whenever |
| 240 | // we are waiting for a callback to release it and unblock the screen. |
| 241 | private ScreenOnUnblocker mPendingScreenOnUnblocker; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 242 | private ScreenOffUnblocker mPendingScreenOffUnblocker; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 243 | |
| 244 | // True if we were in the process of turning off the screen. |
| 245 | // This allows us to recover more gracefully from situations where we abort |
| 246 | // turning off the screen. |
| 247 | private boolean mPendingScreenOff; |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 248 | |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 249 | // True if we have unfinished business and are holding a suspend blocker. |
| 250 | private boolean mUnfinishedBusiness; |
| 251 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 252 | // The elapsed real time when the screen on was blocked. |
| 253 | private long mScreenOnBlockStartRealTime; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 254 | private long mScreenOffBlockStartRealTime; |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 255 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 256 | // Screen state we reported to policy. Must be one of REPORTED_TO_POLICY_SCREEN_* fields. |
| 257 | private int mReportedScreenStateToPolicy; |
Jeff Brown | e1633ad | 2015-06-22 19:24:24 -0700 | [diff] [blame] | 258 | |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 259 | // If the last recorded screen state was dozing or not. |
| 260 | private boolean mDozing; |
| 261 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 262 | // Remembers whether certain kinds of brightness adjustments |
| 263 | // were recently applied so that we can decide how to transition. |
| 264 | private boolean mAppliedAutoBrightness; |
| 265 | private boolean mAppliedDimming; |
| 266 | private boolean mAppliedLowPower; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 267 | |
Julius D'souza | feadad1 | 2016-08-05 14:34:38 -0700 | [diff] [blame] | 268 | // Brightness animation ramp rates in brightness units per second |
Prashant Malani | 99e6d43 | 2016-03-29 16:32:37 -0700 | [diff] [blame] | 269 | private final int mBrightnessRampRateFast; |
Julius D'souza | feadad1 | 2016-08-05 14:34:38 -0700 | [diff] [blame] | 270 | private final int mBrightnessRampRateSlow; |
Prashant Malani | 99e6d43 | 2016-03-29 16:32:37 -0700 | [diff] [blame] | 271 | |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 272 | // Whether or not to skip the initial brightness ramps into STATE_ON. |
| 273 | private final boolean mSkipScreenOnBrightnessRamp; |
| 274 | |
| 275 | // A record of state for skipping brightness ramps. |
| 276 | private int mSkipRampState = RAMP_STATE_SKIP_NONE; |
| 277 | |
| 278 | // The first autobrightness value set when entering RAMP_STATE_SKIP_INITIAL. |
| 279 | private int mInitialAutoBrightness; |
| 280 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 281 | // The controller for the automatic brightness level. |
| 282 | private AutomaticBrightnessController mAutomaticBrightnessController; |
| 283 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 284 | // Animators. |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 285 | private ObjectAnimator mColorFadeOnAnimator; |
| 286 | private ObjectAnimator mColorFadeOffAnimator; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 287 | private RampAnimator<DisplayPowerState> mScreenBrightnessRampAnimator; |
| 288 | |
| 289 | /** |
| 290 | * Creates the display power controller. |
| 291 | */ |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 292 | public DisplayPowerController(Context context, |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 293 | DisplayPowerCallbacks callbacks, Handler handler, |
| 294 | SensorManager sensorManager, DisplayBlanker blanker) { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 295 | mHandler = new DisplayControllerHandler(handler.getLooper()); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 296 | mCallbacks = callbacks; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 297 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 298 | mBatteryStats = BatteryStatsService.getService(); |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 299 | mSensorManager = sensorManager; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 300 | mWindowManagerPolicy = LocalServices.getService(WindowManagerPolicy.class); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 301 | mBlanker = blanker; |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 302 | mContext = context; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 303 | |
| 304 | final Resources resources = context.getResources(); |
Jeff Brown | 1bfd0f4 | 2014-08-22 01:59:06 -0700 | [diff] [blame] | 305 | final int screenBrightnessSettingMinimum = clampAbsoluteBrightness(resources.getInteger( |
| 306 | com.android.internal.R.integer.config_screenBrightnessSettingMinimum)); |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 307 | |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 308 | mScreenBrightnessDozeConfig = clampAbsoluteBrightness(resources.getInteger( |
| 309 | com.android.internal.R.integer.config_screenBrightnessDoze)); |
| 310 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 311 | mScreenBrightnessDimConfig = clampAbsoluteBrightness(resources.getInteger( |
| 312 | com.android.internal.R.integer.config_screenBrightnessDim)); |
| 313 | |
Jeff Brown | 1bfd0f4 | 2014-08-22 01:59:06 -0700 | [diff] [blame] | 314 | mScreenBrightnessDarkConfig = clampAbsoluteBrightness(resources.getInteger( |
| 315 | com.android.internal.R.integer.config_screenBrightnessDark)); |
| 316 | if (mScreenBrightnessDarkConfig > mScreenBrightnessDimConfig) { |
| 317 | Slog.w(TAG, "Expected config_screenBrightnessDark (" |
| 318 | + mScreenBrightnessDarkConfig + ") to be less than or equal to " |
| 319 | + "config_screenBrightnessDim (" + mScreenBrightnessDimConfig + ")."); |
| 320 | } |
liulvping | d01db54 | 2016-10-20 19:51:38 +0800 | [diff] [blame] | 321 | if (mScreenBrightnessDarkConfig > screenBrightnessSettingMinimum) { |
Jeff Brown | 1bfd0f4 | 2014-08-22 01:59:06 -0700 | [diff] [blame] | 322 | Slog.w(TAG, "Expected config_screenBrightnessDark (" |
| 323 | + mScreenBrightnessDarkConfig + ") to be less than or equal to " |
| 324 | + "config_screenBrightnessSettingMinimum (" |
| 325 | + screenBrightnessSettingMinimum + ")."); |
| 326 | } |
| 327 | |
| 328 | int screenBrightnessRangeMinimum = Math.min(Math.min( |
| 329 | screenBrightnessSettingMinimum, mScreenBrightnessDimConfig), |
| 330 | mScreenBrightnessDarkConfig); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 331 | |
| 332 | mScreenBrightnessRangeMaximum = PowerManager.BRIGHTNESS_ON; |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 333 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 334 | mUseSoftwareAutoBrightnessConfig = resources.getBoolean( |
| 335 | com.android.internal.R.bool.config_automatic_brightness_available); |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 336 | |
| 337 | mAllowAutoBrightnessWhileDozingConfig = resources.getBoolean( |
| 338 | com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing); |
| 339 | |
Prashant Malani | 99e6d43 | 2016-03-29 16:32:37 -0700 | [diff] [blame] | 340 | mBrightnessRampRateFast = resources.getInteger( |
| 341 | com.android.internal.R.integer.config_brightness_ramp_rate_fast); |
Julius D'souza | feadad1 | 2016-08-05 14:34:38 -0700 | [diff] [blame] | 342 | mBrightnessRampRateSlow = resources.getInteger( |
| 343 | com.android.internal.R.integer.config_brightness_ramp_rate_slow); |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 344 | mSkipScreenOnBrightnessRamp = resources.getBoolean( |
| 345 | com.android.internal.R.bool.config_skipScreenOnBrightnessRamp); |
Prashant Malani | 99e6d43 | 2016-03-29 16:32:37 -0700 | [diff] [blame] | 346 | |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 347 | int lightSensorRate = resources.getInteger( |
| 348 | com.android.internal.R.integer.config_autoBrightnessLightSensorRate); |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 349 | int initialLightSensorRate = resources.getInteger( |
| 350 | com.android.internal.R.integer.config_autoBrightnessInitialLightSensorRate); |
| 351 | if (initialLightSensorRate == -1) { |
| 352 | initialLightSensorRate = lightSensorRate; |
| 353 | } else if (initialLightSensorRate > lightSensorRate) { |
| 354 | Slog.w(TAG, "Expected config_autoBrightnessInitialLightSensorRate (" |
| 355 | + initialLightSensorRate + ") to be less than or equal to " |
| 356 | + "config_autoBrightnessLightSensorRate (" + lightSensorRate + ")."); |
| 357 | } |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 358 | long brighteningLightDebounce = resources.getInteger( |
| 359 | com.android.internal.R.integer.config_autoBrightnessBrighteningLightDebounce); |
| 360 | long darkeningLightDebounce = resources.getInteger( |
| 361 | com.android.internal.R.integer.config_autoBrightnessDarkeningLightDebounce); |
| 362 | boolean autoBrightnessResetAmbientLuxAfterWarmUp = resources.getBoolean( |
| 363 | com.android.internal.R.bool.config_autoBrightnessResetAmbientLuxAfterWarmUp); |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 364 | int ambientLightHorizon = resources.getInteger( |
| 365 | com.android.internal.R.integer.config_autoBrightnessAmbientLightHorizon); |
| 366 | float autoBrightnessAdjustmentMaxGamma = resources.getFraction( |
| 367 | com.android.internal.R.fraction.config_autoBrightnessAdjustmentMaxGamma, |
| 368 | 1, 1); |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 369 | |
Julius D'souza | 428aed0 | 2016-08-07 19:08:30 -0700 | [diff] [blame] | 370 | int[] brightLevels = resources.getIntArray( |
| 371 | com.android.internal.R.array.config_dynamicHysteresisBrightLevels); |
| 372 | int[] darkLevels = resources.getIntArray( |
| 373 | com.android.internal.R.array.config_dynamicHysteresisDarkLevels); |
| 374 | int[] luxLevels = resources.getIntArray( |
| 375 | com.android.internal.R.array.config_dynamicHysteresisLuxLevels); |
| 376 | HysteresisLevels dynamicHysteresis = new HysteresisLevels( |
| 377 | brightLevels, darkLevels, luxLevels); |
| 378 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 379 | if (mUseSoftwareAutoBrightnessConfig) { |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 380 | int[] lux = resources.getIntArray( |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 381 | com.android.internal.R.array.config_autoBrightnessLevels); |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 382 | int[] screenBrightness = resources.getIntArray( |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 383 | com.android.internal.R.array.config_autoBrightnessLcdBacklightValues); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 384 | int lightSensorWarmUpTimeConfig = resources.getInteger( |
| 385 | com.android.internal.R.integer.config_lightSensorWarmupTime); |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 386 | final float dozeScaleFactor = resources.getFraction( |
| 387 | com.android.internal.R.fraction.config_screenAutoBrightnessDozeScaleFactor, |
| 388 | 1, 1); |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 389 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 390 | Spline screenAutoBrightnessSpline = createAutoBrightnessSpline(lux, screenBrightness); |
| 391 | if (screenAutoBrightnessSpline == null) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 392 | Slog.e(TAG, "Error in config.xml. config_autoBrightnessLcdBacklightValues " |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 393 | + "(size " + screenBrightness.length + ") " |
| 394 | + "must be monotic and have exactly one more entry than " |
| 395 | + "config_autoBrightnessLevels (size " + lux.length + ") " |
| 396 | + "which must be strictly increasing. " |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 397 | + "Auto-brightness will be disabled."); |
| 398 | mUseSoftwareAutoBrightnessConfig = false; |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 399 | } else { |
Jeff Brown | 1bfd0f4 | 2014-08-22 01:59:06 -0700 | [diff] [blame] | 400 | int bottom = clampAbsoluteBrightness(screenBrightness[0]); |
| 401 | if (mScreenBrightnessDarkConfig > bottom) { |
| 402 | Slog.w(TAG, "config_screenBrightnessDark (" + mScreenBrightnessDarkConfig |
| 403 | + ") should be less than or equal to the first value of " |
| 404 | + "config_autoBrightnessLcdBacklightValues (" |
| 405 | + bottom + ")."); |
| 406 | } |
| 407 | if (bottom < screenBrightnessRangeMinimum) { |
| 408 | screenBrightnessRangeMinimum = bottom; |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 409 | } |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 410 | mAutomaticBrightnessController = new AutomaticBrightnessController(this, |
| 411 | handler.getLooper(), sensorManager, screenAutoBrightnessSpline, |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 412 | lightSensorWarmUpTimeConfig, screenBrightnessRangeMinimum, |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 413 | mScreenBrightnessRangeMaximum, dozeScaleFactor, lightSensorRate, |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 414 | initialLightSensorRate, brighteningLightDebounce, darkeningLightDebounce, |
Julius D'souza | 428aed0 | 2016-08-07 19:08:30 -0700 | [diff] [blame] | 415 | autoBrightnessResetAmbientLuxAfterWarmUp, ambientLightHorizon, |
| 416 | autoBrightnessAdjustmentMaxGamma, dynamicHysteresis); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 417 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 420 | mScreenBrightnessRangeMinimum = screenBrightnessRangeMinimum; |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 421 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 422 | mColorFadeFadesConfig = resources.getBoolean( |
Jeff Brown | a52772f | 2012-10-04 18:38:09 -0700 | [diff] [blame] | 423 | com.android.internal.R.bool.config_animateScreenLights); |
| 424 | |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 425 | mDisplayBlanksAfterDozeConfig = resources.getBoolean( |
| 426 | com.android.internal.R.bool.config_displayBlanksAfterDoze); |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 427 | |
Michael Wright | 65d3e70 | 2017-07-26 01:10:59 +0100 | [diff] [blame] | 428 | mBrightnessBucketsInDozeConfig = resources.getBoolean( |
| 429 | com.android.internal.R.bool.config_displayBrightnessBucketsInDoze); |
| 430 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 431 | if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) { |
| 432 | mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); |
| 433 | if (mProximitySensor != null) { |
| 434 | mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(), |
| 435 | TYPICAL_PROXIMITY_THRESHOLD); |
| 436 | } |
| 437 | } |
| 438 | |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 441 | /** |
| 442 | * Returns true if the proximity sensor screen-off function is available. |
| 443 | */ |
| 444 | public boolean isProximitySensorAvailable() { |
| 445 | return mProximitySensor != null; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Requests a new power state. |
| 450 | * The controller makes a copy of the provided object and then |
| 451 | * begins adjusting the power state to match what was requested. |
| 452 | * |
| 453 | * @param request The requested power state. |
| 454 | * @param waitForNegativeProximity If true, issues a request to wait for |
| 455 | * negative proximity before turning the screen back on, assuming the screen |
| 456 | * was turned off by the proximity sensor. |
| 457 | * @return True if display is ready, false if there are important changes that must |
| 458 | * 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] | 459 | * should grab a wake lock, watch for {@link DisplayPowerCallbacks#onStateChanged()} |
| 460 | * then try the request again later until the state converges. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 461 | */ |
| 462 | public boolean requestPowerState(DisplayPowerRequest request, |
| 463 | boolean waitForNegativeProximity) { |
| 464 | if (DEBUG) { |
| 465 | Slog.d(TAG, "requestPowerState: " |
| 466 | + request + ", waitForNegativeProximity=" + waitForNegativeProximity); |
| 467 | } |
| 468 | |
| 469 | synchronized (mLock) { |
| 470 | boolean changed = false; |
| 471 | |
| 472 | if (waitForNegativeProximity |
| 473 | && !mPendingWaitForNegativeProximityLocked) { |
| 474 | mPendingWaitForNegativeProximityLocked = true; |
| 475 | changed = true; |
| 476 | } |
| 477 | |
| 478 | if (mPendingRequestLocked == null) { |
| 479 | mPendingRequestLocked = new DisplayPowerRequest(request); |
| 480 | changed = true; |
| 481 | } else if (!mPendingRequestLocked.equals(request)) { |
| 482 | mPendingRequestLocked.copyFrom(request); |
| 483 | changed = true; |
| 484 | } |
| 485 | |
| 486 | if (changed) { |
| 487 | mDisplayReadyLocked = false; |
| 488 | } |
| 489 | |
| 490 | if (changed && !mPendingRequestChangedLocked) { |
| 491 | mPendingRequestChangedLocked = true; |
| 492 | sendUpdatePowerStateLocked(); |
| 493 | } |
| 494 | |
| 495 | return mDisplayReadyLocked; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | private void sendUpdatePowerState() { |
| 500 | synchronized (mLock) { |
| 501 | sendUpdatePowerStateLocked(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | private void sendUpdatePowerStateLocked() { |
| 506 | if (!mPendingUpdatePowerStateLocked) { |
| 507 | mPendingUpdatePowerStateLocked = true; |
| 508 | Message msg = mHandler.obtainMessage(MSG_UPDATE_POWER_STATE); |
| 509 | msg.setAsynchronous(true); |
| 510 | mHandler.sendMessage(msg); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private void initialize() { |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 515 | // Initialize the power state object for the default display. |
| 516 | // In the future, we might manage multiple displays independently. |
| 517 | mPowerState = new DisplayPowerState(mBlanker, |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 518 | new ColorFade(Display.DEFAULT_DISPLAY)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 519 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 520 | mColorFadeOnAnimator = ObjectAnimator.ofFloat( |
| 521 | mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 0.0f, 1.0f); |
| 522 | mColorFadeOnAnimator.setDuration(COLOR_FADE_ON_ANIMATION_DURATION_MILLIS); |
| 523 | mColorFadeOnAnimator.addListener(mAnimatorListener); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 524 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 525 | mColorFadeOffAnimator = ObjectAnimator.ofFloat( |
| 526 | mPowerState, DisplayPowerState.COLOR_FADE_LEVEL, 1.0f, 0.0f); |
| 527 | mColorFadeOffAnimator.setDuration(COLOR_FADE_OFF_ANIMATION_DURATION_MILLIS); |
| 528 | mColorFadeOffAnimator.addListener(mAnimatorListener); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 529 | |
| 530 | mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>( |
| 531 | mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS); |
Jeff Brown | 4255869 | 2014-05-20 22:02:46 -0700 | [diff] [blame] | 532 | mScreenBrightnessRampAnimator.setListener(mRampAnimatorListener); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 533 | |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 534 | // Initialize screen state for battery stats. |
| 535 | try { |
Jeff Brown | e95c3cd | 2014-05-02 16:59:26 -0700 | [diff] [blame] | 536 | mBatteryStats.noteScreenState(mPowerState.getScreenState()); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 537 | mBatteryStats.noteScreenBrightness(mPowerState.getScreenBrightness()); |
| 538 | } catch (RemoteException ex) { |
| 539 | // same process |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 540 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | private final Animator.AnimatorListener mAnimatorListener = new Animator.AnimatorListener() { |
| 544 | @Override |
| 545 | public void onAnimationStart(Animator animation) { |
| 546 | } |
| 547 | @Override |
| 548 | public void onAnimationEnd(Animator animation) { |
| 549 | sendUpdatePowerState(); |
| 550 | } |
| 551 | @Override |
| 552 | public void onAnimationRepeat(Animator animation) { |
| 553 | } |
| 554 | @Override |
| 555 | public void onAnimationCancel(Animator animation) { |
| 556 | } |
| 557 | }; |
| 558 | |
Jeff Brown | 4255869 | 2014-05-20 22:02:46 -0700 | [diff] [blame] | 559 | private final RampAnimator.Listener mRampAnimatorListener = new RampAnimator.Listener() { |
| 560 | @Override |
| 561 | public void onAnimationEnd() { |
| 562 | sendUpdatePowerState(); |
| 563 | } |
| 564 | }; |
| 565 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 566 | private void updatePowerState() { |
| 567 | // Update the power state request. |
| 568 | final boolean mustNotify; |
| 569 | boolean mustInitialize = false; |
Adrian Roos | 6da87ab | 2014-05-16 23:09:41 +0200 | [diff] [blame] | 570 | boolean autoBrightnessAdjustmentChanged = false; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 571 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 572 | synchronized (mLock) { |
| 573 | mPendingUpdatePowerStateLocked = false; |
| 574 | if (mPendingRequestLocked == null) { |
| 575 | return; // wait until first actual power request |
| 576 | } |
| 577 | |
| 578 | if (mPowerRequest == null) { |
| 579 | mPowerRequest = new DisplayPowerRequest(mPendingRequestLocked); |
| 580 | mWaitingForNegativeProximity = mPendingWaitForNegativeProximityLocked; |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 581 | mPendingWaitForNegativeProximityLocked = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 582 | mPendingRequestChangedLocked = false; |
| 583 | mustInitialize = true; |
| 584 | } else if (mPendingRequestChangedLocked) { |
Adrian Roos | 6da87ab | 2014-05-16 23:09:41 +0200 | [diff] [blame] | 585 | autoBrightnessAdjustmentChanged = (mPowerRequest.screenAutoBrightnessAdjustment |
| 586 | != mPendingRequestLocked.screenAutoBrightnessAdjustment); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 587 | mPowerRequest.copyFrom(mPendingRequestLocked); |
| 588 | mWaitingForNegativeProximity |= mPendingWaitForNegativeProximityLocked; |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 589 | mPendingWaitForNegativeProximityLocked = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 590 | mPendingRequestChangedLocked = false; |
| 591 | mDisplayReadyLocked = false; |
| 592 | } |
| 593 | |
| 594 | mustNotify = !mDisplayReadyLocked; |
| 595 | } |
| 596 | |
| 597 | // Initialize things the first time the power state is changed. |
| 598 | if (mustInitialize) { |
| 599 | initialize(); |
| 600 | } |
| 601 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 602 | // Compute the basic display state using the policy. |
| 603 | // We might override this below based on other factors. |
| 604 | int state; |
| 605 | int brightness = PowerManager.BRIGHTNESS_DEFAULT; |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 606 | boolean performScreenOffTransition = false; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 607 | switch (mPowerRequest.policy) { |
| 608 | case DisplayPowerRequest.POLICY_OFF: |
| 609 | state = Display.STATE_OFF; |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 610 | performScreenOffTransition = true; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 611 | break; |
| 612 | case DisplayPowerRequest.POLICY_DOZE: |
| 613 | if (mPowerRequest.dozeScreenState != Display.STATE_UNKNOWN) { |
| 614 | state = mPowerRequest.dozeScreenState; |
| 615 | } else { |
| 616 | state = Display.STATE_DOZE; |
| 617 | } |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 618 | if (!mAllowAutoBrightnessWhileDozingConfig) { |
| 619 | brightness = mPowerRequest.dozeScreenBrightness; |
| 620 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 621 | break; |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 622 | case DisplayPowerRequest.POLICY_VR: |
| 623 | state = Display.STATE_VR; |
| 624 | break; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 625 | case DisplayPowerRequest.POLICY_DIM: |
| 626 | case DisplayPowerRequest.POLICY_BRIGHT: |
| 627 | default: |
| 628 | state = Display.STATE_ON; |
| 629 | break; |
| 630 | } |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 631 | assert(state != Display.STATE_UNKNOWN); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 632 | |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 633 | // Apply the proximity sensor. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 634 | if (mProximitySensor != null) { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 635 | if (mPowerRequest.useProximitySensor && state != Display.STATE_OFF) { |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 636 | setProximitySensorEnabled(true); |
| 637 | if (!mScreenOffBecauseOfProximity |
| 638 | && mProximity == PROXIMITY_POSITIVE) { |
| 639 | mScreenOffBecauseOfProximity = true; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 640 | sendOnProximityPositiveWithWakelock(); |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 641 | } |
| 642 | } else if (mWaitingForNegativeProximity |
| 643 | && mScreenOffBecauseOfProximity |
| 644 | && mProximity == PROXIMITY_POSITIVE |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 645 | && state != Display.STATE_OFF) { |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 646 | setProximitySensorEnabled(true); |
| 647 | } else { |
| 648 | setProximitySensorEnabled(false); |
| 649 | mWaitingForNegativeProximity = false; |
| 650 | } |
| 651 | if (mScreenOffBecauseOfProximity |
| 652 | && mProximity != PROXIMITY_POSITIVE) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 653 | mScreenOffBecauseOfProximity = false; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 654 | sendOnProximityNegativeWithWakelock(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 655 | } |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 656 | } else { |
| 657 | mWaitingForNegativeProximity = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 658 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 659 | if (mScreenOffBecauseOfProximity) { |
| 660 | state = Display.STATE_OFF; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Jeff Brown | bf4e414 | 2014-10-02 13:08:05 -0700 | [diff] [blame] | 663 | // Animate the screen state change unless already animating. |
| 664 | // The transition may be deferred, so after this point we will use the |
| 665 | // actual state instead of the desired one. |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 666 | final int oldState = mPowerState.getScreenState(); |
Jeff Brown | bf4e414 | 2014-10-02 13:08:05 -0700 | [diff] [blame] | 667 | animateScreenStateChange(state, performScreenOffTransition); |
| 668 | state = mPowerState.getScreenState(); |
| 669 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 670 | // Use zero brightness when screen is off. |
| 671 | if (state == Display.STATE_OFF) { |
| 672 | brightness = PowerManager.BRIGHTNESS_OFF; |
| 673 | } |
| 674 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 675 | // Configure auto-brightness. |
| 676 | boolean autoBrightnessEnabled = false; |
| 677 | if (mAutomaticBrightnessController != null) { |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 678 | final boolean autoBrightnessEnabledInDoze = mAllowAutoBrightnessWhileDozingConfig |
| 679 | && (state == Display.STATE_DOZE || state == Display.STATE_DOZE_SUSPEND); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 680 | autoBrightnessEnabled = mPowerRequest.useAutoBrightness |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 681 | && (state == Display.STATE_ON || autoBrightnessEnabledInDoze) |
| 682 | && brightness < 0; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 683 | final boolean userInitiatedChange = autoBrightnessAdjustmentChanged |
| 684 | && mPowerRequest.brightnessSetByUser; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 685 | mAutomaticBrightnessController.configure(autoBrightnessEnabled, |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 686 | mPowerRequest.screenAutoBrightnessAdjustment, state != Display.STATE_ON, |
Justin Klaassen | 5483cea | 2017-02-02 09:02:35 -0800 | [diff] [blame] | 687 | userInitiatedChange); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Jeff Brown | 7b5be5e | 2014-11-12 18:45:31 -0800 | [diff] [blame] | 690 | // Apply brightness boost. |
| 691 | // We do this here after configuring auto-brightness so that we don't |
| 692 | // disable the light sensor during this temporary state. That way when |
| 693 | // boost ends we will be able to resume normal auto-brightness behavior |
| 694 | // without any delay. |
| 695 | if (mPowerRequest.boostScreenBrightness |
| 696 | && brightness != PowerManager.BRIGHTNESS_OFF) { |
| 697 | brightness = PowerManager.BRIGHTNESS_ON; |
| 698 | } |
| 699 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 700 | // Apply auto-brightness. |
| 701 | boolean slowChange = false; |
| 702 | if (brightness < 0) { |
| 703 | if (autoBrightnessEnabled) { |
| 704 | brightness = mAutomaticBrightnessController.getAutomaticScreenBrightness(); |
| 705 | } |
| 706 | if (brightness >= 0) { |
| 707 | // Use current auto-brightness value and slowly adjust to changes. |
| 708 | brightness = clampScreenBrightness(brightness); |
| 709 | if (mAppliedAutoBrightness && !autoBrightnessAdjustmentChanged) { |
| 710 | slowChange = true; // slowly adapt to auto-brightness |
| 711 | } |
| 712 | mAppliedAutoBrightness = true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 713 | } else { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 714 | mAppliedAutoBrightness = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 715 | } |
| 716 | } else { |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 717 | mAppliedAutoBrightness = false; |
| 718 | } |
| 719 | |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 720 | // Use default brightness when dozing unless overridden. |
| 721 | if (brightness < 0 && (state == Display.STATE_DOZE |
| 722 | || state == Display.STATE_DOZE_SUSPEND)) { |
| 723 | brightness = mScreenBrightnessDozeConfig; |
| 724 | } |
| 725 | |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 726 | // Apply manual brightness. |
| 727 | // Use the current brightness setting from the request, which is expected |
| 728 | // provide a nominal default value for the case where auto-brightness |
| 729 | // is not ready yet. |
| 730 | if (brightness < 0) { |
| 731 | brightness = clampScreenBrightness(mPowerRequest.screenBrightness); |
| 732 | } |
| 733 | |
| 734 | // Apply dimming by at least some minimum amount when user activity |
| 735 | // timeout is about to expire. |
| 736 | if (mPowerRequest.policy == DisplayPowerRequest.POLICY_DIM) { |
Jeff Brown | 5c8ea08 | 2014-07-24 19:30:31 -0700 | [diff] [blame] | 737 | if (brightness > mScreenBrightnessRangeMinimum) { |
| 738 | brightness = Math.max(Math.min(brightness - SCREEN_DIM_MINIMUM_REDUCTION, |
| 739 | mScreenBrightnessDimConfig), mScreenBrightnessRangeMinimum); |
| 740 | } |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 741 | if (!mAppliedDimming) { |
| 742 | slowChange = false; |
| 743 | } |
| 744 | mAppliedDimming = true; |
mochangming | f0cb46c | 2015-12-29 13:55:24 +0800 | [diff] [blame] | 745 | } else if (mAppliedDimming) { |
| 746 | slowChange = false; |
| 747 | mAppliedDimming = false; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 748 | } |
| 749 | |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 750 | // If low power mode is enabled, scale brightness by screenLowPowerBrightnessFactor |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 751 | // as long as it is above the minimum threshold. |
| 752 | if (mPowerRequest.lowPowerMode) { |
| 753 | if (brightness > mScreenBrightnessRangeMinimum) { |
jackqdyulei | 92681e8 | 2017-02-28 11:26:28 -0800 | [diff] [blame] | 754 | final float brightnessFactor = |
| 755 | Math.min(mPowerRequest.screenLowPowerBrightnessFactor, 1); |
| 756 | final int lowPowerBrightness = (int) (brightness * brightnessFactor); |
| 757 | brightness = Math.max(lowPowerBrightness, mScreenBrightnessRangeMinimum); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 758 | } |
| 759 | if (!mAppliedLowPower) { |
| 760 | slowChange = false; |
| 761 | } |
| 762 | mAppliedLowPower = true; |
mochangming | f0cb46c | 2015-12-29 13:55:24 +0800 | [diff] [blame] | 763 | } else if (mAppliedLowPower) { |
| 764 | slowChange = false; |
| 765 | mAppliedLowPower = false; |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 768 | // Animate the screen brightness when the screen is on or dozing. |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 769 | // 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] | 770 | if (!mPendingScreenOff) { |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 771 | if (mSkipScreenOnBrightnessRamp) { |
| 772 | |
| 773 | if (state == Display.STATE_ON) { |
| 774 | if (mSkipRampState == RAMP_STATE_SKIP_NONE && mDozing) { |
| 775 | mInitialAutoBrightness = brightness; |
| 776 | mSkipRampState = RAMP_STATE_SKIP_INITIAL; |
| 777 | } else if (mSkipRampState == RAMP_STATE_SKIP_INITIAL |
| 778 | && mUseSoftwareAutoBrightnessConfig |
| 779 | && brightness != mInitialAutoBrightness) { |
| 780 | mSkipRampState = RAMP_STATE_SKIP_AUTOBRIGHT; |
| 781 | } else if (mSkipRampState == RAMP_STATE_SKIP_AUTOBRIGHT) { |
| 782 | mSkipRampState = RAMP_STATE_SKIP_NONE; |
| 783 | } |
| 784 | } else { |
| 785 | mSkipRampState = RAMP_STATE_SKIP_NONE; |
| 786 | } |
| 787 | } |
| 788 | |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 789 | boolean wasOrWillBeInVr = (state == Display.STATE_VR || oldState == Display.STATE_VR); |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 790 | if ((state == Display.STATE_ON |
| 791 | && mSkipRampState == RAMP_STATE_SKIP_NONE |
Michael Wright | 65d3e70 | 2017-07-26 01:10:59 +0100 | [diff] [blame] | 792 | || state == Display.STATE_DOZE && !mBrightnessBucketsInDozeConfig) |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 793 | && !wasOrWillBeInVr) { |
Prashant Malani | 3353824 | 2014-11-13 14:04:00 -0800 | [diff] [blame] | 794 | animateScreenBrightness(brightness, |
Julius D'souza | feadad1 | 2016-08-05 14:34:38 -0700 | [diff] [blame] | 795 | slowChange ? mBrightnessRampRateSlow : mBrightnessRampRateFast); |
Prashant Malani | 3353824 | 2014-11-13 14:04:00 -0800 | [diff] [blame] | 796 | } else { |
| 797 | animateScreenBrightness(brightness, 0); |
| 798 | } |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | // Determine whether the display is ready for use in the newly requested state. |
| 802 | // Note that we do not wait for the brightness ramp animation to complete before |
| 803 | // reporting the display is ready because we only need to ensure the screen is in the |
| 804 | // right power state even as it continues to converge on the desired brightness. |
| 805 | final boolean ready = mPendingScreenOnUnblocker == null |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 806 | && !mColorFadeOnAnimator.isStarted() |
| 807 | && !mColorFadeOffAnimator.isStarted() |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 808 | && mPowerState.waitUntilClean(mCleanListener); |
| 809 | final boolean finished = ready |
| 810 | && !mScreenBrightnessRampAnimator.isAnimating(); |
| 811 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 812 | // Notify policy about screen turned on. |
| 813 | if (ready && state != Display.STATE_OFF |
| 814 | && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_ON) { |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 815 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_ON); |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 816 | mWindowManagerPolicy.screenTurnedOn(); |
| 817 | } |
| 818 | |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 819 | // Grab a wake lock if we have unfinished business. |
| 820 | if (!finished && !mUnfinishedBusiness) { |
| 821 | if (DEBUG) { |
| 822 | Slog.d(TAG, "Unfinished business..."); |
| 823 | } |
| 824 | mCallbacks.acquireSuspendBlocker(); |
| 825 | mUnfinishedBusiness = true; |
| 826 | } |
| 827 | |
| 828 | // Notify the power manager when ready. |
| 829 | if (ready && mustNotify) { |
| 830 | // Send state change. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 831 | synchronized (mLock) { |
| 832 | if (!mPendingRequestChangedLocked) { |
| 833 | mDisplayReadyLocked = true; |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 834 | |
| 835 | if (DEBUG) { |
| 836 | Slog.d(TAG, "Display ready!"); |
| 837 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 840 | sendOnStateChangedWithWakelock(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 841 | } |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 842 | |
| 843 | // Release the wake lock when we have no unfinished business. |
| 844 | if (finished && mUnfinishedBusiness) { |
| 845 | if (DEBUG) { |
| 846 | Slog.d(TAG, "Finished business..."); |
| 847 | } |
| 848 | mUnfinishedBusiness = false; |
| 849 | mCallbacks.releaseSuspendBlocker(); |
| 850 | } |
Julius D'souza | d5105dd | 2017-06-02 11:03:53 -0700 | [diff] [blame] | 851 | |
| 852 | // Record if dozing for future comparison. |
| 853 | mDozing = state != Display.STATE_ON; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 856 | @Override |
| 857 | public void updateBrightness() { |
| 858 | sendUpdatePowerState(); |
| 859 | } |
| 860 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 861 | private void blockScreenOn() { |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 862 | if (mPendingScreenOnUnblocker == null) { |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 863 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0); |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 864 | mPendingScreenOnUnblocker = new ScreenOnUnblocker(); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 865 | mScreenOnBlockStartRealTime = SystemClock.elapsedRealtime(); |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 866 | Slog.i(TAG, "Blocking screen on until initial contents have been drawn."); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | |
| 870 | private void unblockScreenOn() { |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 871 | if (mPendingScreenOnUnblocker != null) { |
| 872 | mPendingScreenOnUnblocker = null; |
Jeff Brown | 2d8a390 | 2014-03-11 23:02:35 -0700 | [diff] [blame] | 873 | long delay = SystemClock.elapsedRealtime() - mScreenOnBlockStartRealTime; |
Jeff Brown | 3edf527 | 2014-08-14 19:25:14 -0700 | [diff] [blame] | 874 | Slog.i(TAG, "Unblocked screen on after " + delay + " ms"); |
| 875 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_ON_BLOCKED_TRACE_NAME, 0); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 879 | private void blockScreenOff() { |
| 880 | if (mPendingScreenOffUnblocker == null) { |
| 881 | Trace.asyncTraceBegin(Trace.TRACE_TAG_POWER, SCREEN_OFF_BLOCKED_TRACE_NAME, 0); |
| 882 | mPendingScreenOffUnblocker = new ScreenOffUnblocker(); |
| 883 | mScreenOffBlockStartRealTime = SystemClock.elapsedRealtime(); |
| 884 | Slog.i(TAG, "Blocking screen off"); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | private void unblockScreenOff() { |
| 889 | if (mPendingScreenOffUnblocker != null) { |
| 890 | mPendingScreenOffUnblocker = null; |
| 891 | long delay = SystemClock.elapsedRealtime() - mScreenOffBlockStartRealTime; |
| 892 | Slog.i(TAG, "Unblocked screen off after " + delay + " ms"); |
| 893 | Trace.asyncTraceEnd(Trace.TRACE_TAG_POWER, SCREEN_OFF_BLOCKED_TRACE_NAME, 0); |
| 894 | } |
| 895 | } |
| 896 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 897 | private boolean setScreenState(int state) { |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 898 | return setScreenState(state, false /*reportOnly*/); |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 899 | } |
| 900 | |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 901 | private boolean setScreenState(int state, boolean reportOnly) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 902 | final boolean isOff = (state == Display.STATE_OFF); |
Jeff Brown | 037c33e | 2014-04-09 00:31:55 -0700 | [diff] [blame] | 903 | if (mPowerState.getScreenState() != state) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 904 | |
| 905 | // If we are trying to turn screen off, give policy a chance to do something before we |
| 906 | // actually turn the screen off. |
| 907 | if (isOff && !mScreenOffBecauseOfProximity) { |
| 908 | if (mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_ON) { |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 909 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_TURNING_OFF); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 910 | blockScreenOff(); |
| 911 | mWindowManagerPolicy.screenTurningOff(mPendingScreenOffUnblocker); |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 912 | unblockScreenOff(); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 913 | } else if (mPendingScreenOffUnblocker != null) { |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 914 | // Abort doing the state change until screen off is unblocked. |
| 915 | return false; |
| 916 | } |
| 917 | } |
| 918 | |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 919 | if (!reportOnly) { |
| 920 | mPowerState.setScreenState(state); |
| 921 | // Tell battery stats about the transition. |
| 922 | try { |
| 923 | mBatteryStats.noteScreenState(state); |
| 924 | } catch (RemoteException ex) { |
| 925 | // same process |
| 926 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 927 | } |
| 928 | } |
Jeff Brown | e1633ad | 2015-06-22 19:24:24 -0700 | [diff] [blame] | 929 | |
| 930 | // Tell the window manager policy when the screen is turned off or on unless it's due |
| 931 | // to the proximity sensor. We temporarily block turning the screen on until the |
| 932 | // window manager is ready by leaving a black surface covering the screen. |
| 933 | // This surface is essentially the final state of the color fade animation and |
| 934 | // it is only removed once the window manager tells us that the activity has |
| 935 | // finished drawing underneath. |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 936 | if (isOff && mReportedScreenStateToPolicy != REPORTED_TO_POLICY_SCREEN_OFF |
| 937 | && !mScreenOffBecauseOfProximity) { |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 938 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_OFF); |
Jeff Brown | e1633ad | 2015-06-22 19:24:24 -0700 | [diff] [blame] | 939 | unblockScreenOn(); |
| 940 | mWindowManagerPolicy.screenTurnedOff(); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 941 | } else if (!isOff |
| 942 | && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_TURNING_OFF) { |
| 943 | |
| 944 | // We told policy already that screen was turning off, but now we changed our minds. |
| 945 | // Complete the full state transition on -> turningOff -> off. |
| 946 | unblockScreenOff(); |
| 947 | mWindowManagerPolicy.screenTurnedOff(); |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 948 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_OFF); |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 949 | } |
| 950 | if (!isOff && mReportedScreenStateToPolicy == REPORTED_TO_POLICY_SCREEN_OFF) { |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 951 | setReportedScreenState(REPORTED_TO_POLICY_SCREEN_TURNING_ON); |
Jeff Brown | e1633ad | 2015-06-22 19:24:24 -0700 | [diff] [blame] | 952 | if (mPowerState.getColorFadeLevel() == 0.0f) { |
| 953 | blockScreenOn(); |
| 954 | } else { |
| 955 | unblockScreenOn(); |
| 956 | } |
| 957 | mWindowManagerPolicy.screenTurningOn(mPendingScreenOnUnblocker); |
| 958 | } |
| 959 | |
| 960 | // Return true if the screen isn't blocked. |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 961 | return mPendingScreenOnUnblocker == null; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 964 | private void setReportedScreenState(int state) { |
| 965 | Trace.traceCounter(Trace.TRACE_TAG_POWER, "ReportedScreenStateToPolicy", state); |
| 966 | mReportedScreenStateToPolicy = state; |
| 967 | } |
| 968 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 969 | private int clampScreenBrightness(int value) { |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 970 | return MathUtils.constrain( |
| 971 | value, mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 974 | private void animateScreenBrightness(int target, int rate) { |
Jeff Brown | 0a43477 | 2014-09-30 14:42:27 -0700 | [diff] [blame] | 975 | if (DEBUG) { |
| 976 | Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate); |
| 977 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 978 | if (mScreenBrightnessRampAnimator.animateTo(target, rate)) { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 979 | try { |
| 980 | mBatteryStats.noteScreenBrightness(target); |
| 981 | } catch (RemoteException ex) { |
| 982 | // same process |
| 983 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 987 | private void animateScreenStateChange(int target, boolean performScreenOffTransition) { |
| 988 | // If there is already an animation in progress, don't interfere with it. |
| 989 | if (mColorFadeOnAnimator.isStarted() |
| 990 | || mColorFadeOffAnimator.isStarted()) { |
Chong Zhang | b131c70 | 2016-02-16 16:31:48 -0800 | [diff] [blame] | 991 | if (target != Display.STATE_ON) { |
| 992 | return; |
| 993 | } |
| 994 | // If display state changed to on, proceed and stop the color fade and turn screen on. |
| 995 | mPendingScreenOff = false; |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 998 | if (mDisplayBlanksAfterDozeConfig |
| 999 | && Display.isDozeState(mPowerState.getScreenState()) |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 1000 | && !Display.isDozeState(target)) { |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 1001 | // Skip the screen off animation and add a black surface to hide the |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1002 | // contents of the screen. |
| 1003 | mPowerState.prepareColorFade(mContext, |
| 1004 | mColorFadeFadesConfig ? ColorFade.MODE_FADE : ColorFade.MODE_WARM_UP); |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 1005 | mColorFadeOffAnimator.end(); |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1006 | // Some display hardware will blank itself on the transition between doze and non-doze |
| 1007 | // but still on display states. In this case we want to report to policy that the |
| 1008 | // display has turned off so it can prepare the appropriate power on animation, but we |
| 1009 | // don't want to actually transition to the fully off state since that takes |
| 1010 | // significantly longer to transition from. |
| 1011 | setScreenState(Display.STATE_OFF, target != Display.STATE_OFF /*reportOnly*/); |
Michael Wright | 05e76fe | 2017-07-20 18:18:33 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1014 | // If we were in the process of turning off the screen but didn't quite |
| 1015 | // finish. Then finish up now to prevent a jarring transition back |
| 1016 | // to screen on if we skipped blocking screen on as usual. |
| 1017 | if (mPendingScreenOff && target != Display.STATE_OFF) { |
| 1018 | setScreenState(Display.STATE_OFF); |
| 1019 | mPendingScreenOff = false; |
Michael Lentine | d73854d | 2015-09-25 10:55:26 -0700 | [diff] [blame] | 1020 | mPowerState.dismissColorFadeResources(); |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | if (target == Display.STATE_ON) { |
| 1024 | // Want screen on. The contents of the screen may not yet |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1025 | // be visible if the color fade has not been dismissed because |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1026 | // its last frame of animation is solid black. |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1027 | if (!setScreenState(Display.STATE_ON)) { |
| 1028 | return; // screen on blocked |
| 1029 | } |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1030 | if (USE_COLOR_FADE_ON_ANIMATION && mPowerRequest.isBrightOrDim()) { |
| 1031 | // Perform screen on animation. |
| 1032 | if (mPowerState.getColorFadeLevel() == 1.0f) { |
| 1033 | mPowerState.dismissColorFade(); |
| 1034 | } else if (mPowerState.prepareColorFade(mContext, |
| 1035 | mColorFadeFadesConfig ? |
| 1036 | ColorFade.MODE_FADE : |
| 1037 | ColorFade.MODE_WARM_UP)) { |
| 1038 | mColorFadeOnAnimator.start(); |
| 1039 | } else { |
| 1040 | mColorFadeOnAnimator.end(); |
| 1041 | } |
| 1042 | } else { |
| 1043 | // Skip screen on animation. |
| 1044 | mPowerState.setColorFadeLevel(1.0f); |
| 1045 | mPowerState.dismissColorFade(); |
| 1046 | } |
Santos Cordon | 3107d29 | 2016-09-20 15:50:35 -0700 | [diff] [blame] | 1047 | } else if (target == Display.STATE_VR) { |
| 1048 | // Wait for brightness animation to complete beforehand when entering VR |
| 1049 | // from screen on to prevent a perceptible jump because brightness may operate |
| 1050 | // differently when the display is configured for dozing. |
| 1051 | if (mScreenBrightnessRampAnimator.isAnimating() |
| 1052 | && mPowerState.getScreenState() == Display.STATE_ON) { |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | // Set screen state. |
| 1057 | if (!setScreenState(Display.STATE_VR)) { |
| 1058 | return; // screen on blocked |
| 1059 | } |
| 1060 | |
| 1061 | // Dismiss the black surface without fanfare. |
| 1062 | mPowerState.setColorFadeLevel(1.0f); |
| 1063 | mPowerState.dismissColorFade(); |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1064 | } else if (target == Display.STATE_DOZE) { |
| 1065 | // Want screen dozing. |
| 1066 | // Wait for brightness animation to complete beforehand when entering doze |
| 1067 | // from screen on to prevent a perceptible jump because brightness may operate |
| 1068 | // differently when the display is configured for dozing. |
| 1069 | if (mScreenBrightnessRampAnimator.isAnimating() |
| 1070 | && mPowerState.getScreenState() == Display.STATE_ON) { |
| 1071 | return; |
| 1072 | } |
| 1073 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1074 | // Set screen state. |
| 1075 | if (!setScreenState(Display.STATE_DOZE)) { |
| 1076 | return; // screen on blocked |
| 1077 | } |
| 1078 | |
| 1079 | // Dismiss the black surface without fanfare. |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1080 | mPowerState.setColorFadeLevel(1.0f); |
| 1081 | mPowerState.dismissColorFade(); |
| 1082 | } else if (target == Display.STATE_DOZE_SUSPEND) { |
| 1083 | // Want screen dozing and suspended. |
| 1084 | // Wait for brightness animation to complete beforehand unless already |
| 1085 | // suspended because we may not be able to change it after suspension. |
| 1086 | if (mScreenBrightnessRampAnimator.isAnimating() |
| 1087 | && mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) { |
| 1088 | return; |
| 1089 | } |
| 1090 | |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1091 | // If not already suspending, temporarily set the state to doze until the |
| 1092 | // screen on is unblocked, then suspend. |
| 1093 | if (mPowerState.getScreenState() != Display.STATE_DOZE_SUSPEND) { |
| 1094 | if (!setScreenState(Display.STATE_DOZE)) { |
| 1095 | return; // screen on blocked |
| 1096 | } |
| 1097 | setScreenState(Display.STATE_DOZE_SUSPEND); // already on so can't block |
| 1098 | } |
| 1099 | |
| 1100 | // Dismiss the black surface without fanfare. |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1101 | mPowerState.setColorFadeLevel(1.0f); |
| 1102 | mPowerState.dismissColorFade(); |
| 1103 | } else { |
| 1104 | // Want screen off. |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1105 | mPendingScreenOff = true; |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1106 | if (mPowerState.getColorFadeLevel() == 0.0f) { |
| 1107 | // Turn the screen off. |
| 1108 | // A black surface is already hiding the contents of the screen. |
| 1109 | setScreenState(Display.STATE_OFF); |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1110 | mPendingScreenOff = false; |
Michael Lentine | d73854d | 2015-09-25 10:55:26 -0700 | [diff] [blame] | 1111 | mPowerState.dismissColorFadeResources(); |
Jeff Brown | 606e4e8 | 2014-09-18 15:22:26 -0700 | [diff] [blame] | 1112 | } else if (performScreenOffTransition |
| 1113 | && mPowerState.prepareColorFade(mContext, |
| 1114 | mColorFadeFadesConfig ? |
| 1115 | ColorFade.MODE_FADE : ColorFade.MODE_COOL_DOWN) |
| 1116 | && mPowerState.getScreenState() != Display.STATE_OFF) { |
| 1117 | // Perform the screen off animation. |
| 1118 | mColorFadeOffAnimator.start(); |
| 1119 | } else { |
| 1120 | // Skip the screen off animation and add a black surface to hide the |
| 1121 | // contents of the screen. |
| 1122 | mColorFadeOffAnimator.end(); |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1127 | private final Runnable mCleanListener = new Runnable() { |
| 1128 | @Override |
| 1129 | public void run() { |
| 1130 | sendUpdatePowerState(); |
| 1131 | } |
| 1132 | }; |
| 1133 | |
| 1134 | private void setProximitySensorEnabled(boolean enable) { |
| 1135 | if (enable) { |
| 1136 | if (!mProximitySensorEnabled) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1137 | // Register the listener. |
| 1138 | // Proximity sensor state already cleared initially. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1139 | mProximitySensorEnabled = true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1140 | mSensorManager.registerListener(mProximitySensorListener, mProximitySensor, |
| 1141 | SensorManager.SENSOR_DELAY_NORMAL, mHandler); |
| 1142 | } |
| 1143 | } else { |
| 1144 | if (mProximitySensorEnabled) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1145 | // Unregister the listener. |
| 1146 | // Clear the proximity sensor state for next time. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1147 | mProximitySensorEnabled = false; |
| 1148 | mProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1149 | mPendingProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1150 | mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
| 1151 | mSensorManager.unregisterListener(mProximitySensorListener); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1152 | clearPendingProximityDebounceTime(); // release wake lock (must be last) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | private void handleProximitySensorEvent(long time, boolean positive) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1158 | if (mProximitySensorEnabled) { |
| 1159 | if (mPendingProximity == PROXIMITY_NEGATIVE && !positive) { |
| 1160 | return; // no change |
| 1161 | } |
| 1162 | if (mPendingProximity == PROXIMITY_POSITIVE && positive) { |
| 1163 | return; // no change |
| 1164 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1165 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1166 | // Only accept a proximity sensor reading if it remains |
| 1167 | // stable for the entire debounce delay. We hold a wake lock while |
| 1168 | // debouncing the sensor. |
| 1169 | mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
| 1170 | if (positive) { |
| 1171 | mPendingProximity = PROXIMITY_POSITIVE; |
| 1172 | setPendingProximityDebounceTime( |
| 1173 | time + PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY); // acquire wake lock |
| 1174 | } else { |
| 1175 | mPendingProximity = PROXIMITY_NEGATIVE; |
| 1176 | setPendingProximityDebounceTime( |
| 1177 | time + PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY); // acquire wake lock |
| 1178 | } |
| 1179 | |
| 1180 | // Debounce the new sensor reading. |
| 1181 | debounceProximitySensor(); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1182 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | private void debounceProximitySensor() { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1186 | if (mProximitySensorEnabled |
| 1187 | && mPendingProximity != PROXIMITY_UNKNOWN |
| 1188 | && mPendingProximityDebounceTime >= 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1189 | final long now = SystemClock.uptimeMillis(); |
| 1190 | if (mPendingProximityDebounceTime <= now) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1191 | // Sensor reading accepted. Apply the change then release the wake lock. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1192 | mProximity = mPendingProximity; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1193 | updatePowerState(); |
| 1194 | clearPendingProximityDebounceTime(); // release wake lock (must be last) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1195 | } else { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1196 | // Need to wait a little longer. |
| 1197 | // Debounce again later. We continue holding a wake lock while waiting. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1198 | Message msg = mHandler.obtainMessage(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
| 1199 | msg.setAsynchronous(true); |
| 1200 | mHandler.sendMessageAtTime(msg, mPendingProximityDebounceTime); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1205 | private void clearPendingProximityDebounceTime() { |
| 1206 | if (mPendingProximityDebounceTime >= 0) { |
| 1207 | mPendingProximityDebounceTime = -1; |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1208 | mCallbacks.releaseSuspendBlocker(); // release wake lock |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | private void setPendingProximityDebounceTime(long debounceTime) { |
| 1213 | if (mPendingProximityDebounceTime < 0) { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1214 | mCallbacks.acquireSuspendBlocker(); // acquire wake lock |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1215 | } |
| 1216 | mPendingProximityDebounceTime = debounceTime; |
| 1217 | } |
| 1218 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1219 | private void sendOnStateChangedWithWakelock() { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1220 | mCallbacks.acquireSuspendBlocker(); |
| 1221 | mHandler.post(mOnStateChangedRunnable); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | private final Runnable mOnStateChangedRunnable = new Runnable() { |
| 1225 | @Override |
| 1226 | public void run() { |
| 1227 | mCallbacks.onStateChanged(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1228 | mCallbacks.releaseSuspendBlocker(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1229 | } |
| 1230 | }; |
| 1231 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1232 | private void sendOnProximityPositiveWithWakelock() { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1233 | mCallbacks.acquireSuspendBlocker(); |
| 1234 | mHandler.post(mOnProximityPositiveRunnable); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | private final Runnable mOnProximityPositiveRunnable = new Runnable() { |
| 1238 | @Override |
| 1239 | public void run() { |
| 1240 | mCallbacks.onProximityPositive(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1241 | mCallbacks.releaseSuspendBlocker(); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1242 | } |
| 1243 | }; |
| 1244 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1245 | private void sendOnProximityNegativeWithWakelock() { |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1246 | mCallbacks.acquireSuspendBlocker(); |
| 1247 | mHandler.post(mOnProximityNegativeRunnable); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | private final Runnable mOnProximityNegativeRunnable = new Runnable() { |
| 1251 | @Override |
| 1252 | public void run() { |
| 1253 | mCallbacks.onProximityNegative(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1254 | mCallbacks.releaseSuspendBlocker(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1255 | } |
| 1256 | }; |
| 1257 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1258 | public void dump(final PrintWriter pw) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1259 | synchronized (mLock) { |
| 1260 | pw.println(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1261 | pw.println("Display Power Controller Locked State:"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1262 | pw.println(" mDisplayReadyLocked=" + mDisplayReadyLocked); |
| 1263 | pw.println(" mPendingRequestLocked=" + mPendingRequestLocked); |
| 1264 | pw.println(" mPendingRequestChangedLocked=" + mPendingRequestChangedLocked); |
| 1265 | pw.println(" mPendingWaitForNegativeProximityLocked=" |
| 1266 | + mPendingWaitForNegativeProximityLocked); |
| 1267 | pw.println(" mPendingUpdatePowerStateLocked=" + mPendingUpdatePowerStateLocked); |
| 1268 | } |
| 1269 | |
| 1270 | pw.println(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1271 | pw.println("Display Power Controller Configuration:"); |
Jeff Brown | 2687550 | 2014-01-30 21:47:47 -0800 | [diff] [blame] | 1272 | pw.println(" mScreenBrightnessDozeConfig=" + mScreenBrightnessDozeConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1273 | pw.println(" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig); |
Jeff Brown | 1bfd0f4 | 2014-08-22 01:59:06 -0700 | [diff] [blame] | 1274 | pw.println(" mScreenBrightnessDarkConfig=" + mScreenBrightnessDarkConfig); |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 1275 | pw.println(" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum); |
| 1276 | pw.println(" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum); |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1277 | pw.println(" mUseSoftwareAutoBrightnessConfig=" + mUseSoftwareAutoBrightnessConfig); |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 1278 | pw.println(" mAllowAutoBrightnessWhileDozingConfig=" + |
| 1279 | mAllowAutoBrightnessWhileDozingConfig); |
Jeff Brown | 2175e9c | 2014-09-12 16:11:07 -0700 | [diff] [blame] | 1280 | pw.println(" mColorFadeFadesConfig=" + mColorFadeFadesConfig); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1281 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1282 | mHandler.runWithScissors(new Runnable() { |
| 1283 | @Override |
| 1284 | public void run() { |
| 1285 | dumpLocal(pw); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1286 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1287 | }, 1000); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | private void dumpLocal(PrintWriter pw) { |
| 1291 | pw.println(); |
Jeff Brown | 131206b | 2014-04-08 17:27:14 -0700 | [diff] [blame] | 1292 | pw.println("Display Power Controller Thread State:"); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1293 | pw.println(" mPowerRequest=" + mPowerRequest); |
| 1294 | pw.println(" mWaitingForNegativeProximity=" + mWaitingForNegativeProximity); |
| 1295 | |
| 1296 | pw.println(" mProximitySensor=" + mProximitySensor); |
| 1297 | pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled); |
| 1298 | pw.println(" mProximityThreshold=" + mProximityThreshold); |
| 1299 | pw.println(" mProximity=" + proximityToString(mProximity)); |
| 1300 | pw.println(" mPendingProximity=" + proximityToString(mPendingProximity)); |
| 1301 | pw.println(" mPendingProximityDebounceTime=" |
| 1302 | + TimeUtils.formatUptime(mPendingProximityDebounceTime)); |
| 1303 | pw.println(" mScreenOffBecauseOfProximity=" + mScreenOffBecauseOfProximity); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 1304 | pw.println(" mAppliedAutoBrightness=" + mAppliedAutoBrightness); |
| 1305 | pw.println(" mAppliedDimming=" + mAppliedDimming); |
| 1306 | pw.println(" mAppliedLowPower=" + mAppliedLowPower); |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1307 | pw.println(" mPendingScreenOnUnblocker=" + mPendingScreenOnUnblocker); |
| 1308 | pw.println(" mPendingScreenOff=" + mPendingScreenOff); |
Michael Wright | 410fd1b | 2017-07-25 22:31:03 +0100 | [diff] [blame] | 1309 | pw.println(" mReportedToPolicy=" + |
| 1310 | reportedToPolicyToString(mReportedScreenStateToPolicy)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1311 | |
Jeff Brown | 4255869 | 2014-05-20 22:02:46 -0700 | [diff] [blame] | 1312 | pw.println(" mScreenBrightnessRampAnimator.isAnimating()=" + |
| 1313 | mScreenBrightnessRampAnimator.isAnimating()); |
| 1314 | |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 1315 | if (mColorFadeOnAnimator != null) { |
| 1316 | pw.println(" mColorFadeOnAnimator.isStarted()=" + |
| 1317 | mColorFadeOnAnimator.isStarted()); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1318 | } |
Michael Lentine | 0839adb | 2014-07-29 18:47:56 -0700 | [diff] [blame] | 1319 | if (mColorFadeOffAnimator != null) { |
| 1320 | pw.println(" mColorFadeOffAnimator.isStarted()=" + |
| 1321 | mColorFadeOffAnimator.isStarted()); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | if (mPowerState != null) { |
| 1325 | mPowerState.dump(pw); |
| 1326 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1327 | |
| 1328 | if (mAutomaticBrightnessController != null) { |
| 1329 | mAutomaticBrightnessController.dump(pw); |
| 1330 | } |
| 1331 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | private static String proximityToString(int state) { |
| 1335 | switch (state) { |
| 1336 | case PROXIMITY_UNKNOWN: |
| 1337 | return "Unknown"; |
| 1338 | case PROXIMITY_NEGATIVE: |
| 1339 | return "Negative"; |
| 1340 | case PROXIMITY_POSITIVE: |
| 1341 | return "Positive"; |
| 1342 | default: |
| 1343 | return Integer.toString(state); |
| 1344 | } |
| 1345 | } |
| 1346 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 1347 | private static String reportedToPolicyToString(int state) { |
| 1348 | switch (state) { |
| 1349 | case REPORTED_TO_POLICY_SCREEN_OFF: |
| 1350 | return "REPORTED_TO_POLICY_SCREEN_OFF"; |
| 1351 | case REPORTED_TO_POLICY_SCREEN_TURNING_ON: |
| 1352 | return "REPORTED_TO_POLICY_SCREEN_TURNING_ON"; |
| 1353 | case REPORTED_TO_POLICY_SCREEN_ON: |
| 1354 | return "REPORTED_TO_POLICY_SCREEN_ON"; |
| 1355 | default: |
| 1356 | return Integer.toString(state); |
| 1357 | } |
| 1358 | } |
| 1359 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1360 | private static Spline createAutoBrightnessSpline(int[] lux, int[] brightness) { |
Erik Wolsheimer | ad82aca | 2016-06-28 14:51:07 -0700 | [diff] [blame] | 1361 | if (lux == null || lux.length == 0 || brightness == null || brightness.length == 0) { |
| 1362 | Slog.e(TAG, "Could not create auto-brightness spline."); |
| 1363 | return null; |
| 1364 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1365 | try { |
| 1366 | final int n = brightness.length; |
| 1367 | float[] x = new float[n]; |
| 1368 | float[] y = new float[n]; |
| 1369 | y[0] = normalizeAbsoluteBrightness(brightness[0]); |
| 1370 | for (int i = 1; i < n; i++) { |
| 1371 | x[i] = lux[i - 1]; |
| 1372 | y[i] = normalizeAbsoluteBrightness(brightness[i]); |
| 1373 | } |
| 1374 | |
Michael Wright | 3e9a134 | 2014-08-28 17:42:16 -0700 | [diff] [blame] | 1375 | Spline spline = Spline.createSpline(x, y); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1376 | if (DEBUG) { |
| 1377 | Slog.d(TAG, "Auto-brightness spline: " + spline); |
| 1378 | for (float v = 1f; v < lux[lux.length - 1] * 1.25f; v *= 1.25f) { |
| 1379 | Slog.d(TAG, String.format(" %7.1f: %7.1f", v, spline.interpolate(v))); |
| 1380 | } |
| 1381 | } |
| 1382 | return spline; |
| 1383 | } catch (IllegalArgumentException ex) { |
| 1384 | Slog.e(TAG, "Could not create auto-brightness spline.", ex); |
| 1385 | return null; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1386 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | private static float normalizeAbsoluteBrightness(int value) { |
| 1390 | return (float)clampAbsoluteBrightness(value) / PowerManager.BRIGHTNESS_ON; |
| 1391 | } |
| 1392 | |
| 1393 | private static int clampAbsoluteBrightness(int value) { |
| 1394 | return MathUtils.constrain(value, PowerManager.BRIGHTNESS_OFF, PowerManager.BRIGHTNESS_ON); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1397 | private final class DisplayControllerHandler extends Handler { |
| 1398 | public DisplayControllerHandler(Looper looper) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 1399 | super(looper, null, true /*async*/); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | @Override |
| 1403 | public void handleMessage(Message msg) { |
| 1404 | switch (msg.what) { |
| 1405 | case MSG_UPDATE_POWER_STATE: |
| 1406 | updatePowerState(); |
| 1407 | break; |
| 1408 | |
| 1409 | case MSG_PROXIMITY_SENSOR_DEBOUNCED: |
| 1410 | debounceProximitySensor(); |
| 1411 | break; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1412 | |
| 1413 | case MSG_SCREEN_ON_UNBLOCKED: |
| 1414 | if (mPendingScreenOnUnblocker == msg.obj) { |
| 1415 | unblockScreenOn(); |
| 1416 | updatePowerState(); |
| 1417 | } |
| 1418 | break; |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1419 | case MSG_SCREEN_OFF_UNBLOCKED: |
| 1420 | if (mPendingScreenOffUnblocker == msg.obj) { |
| 1421 | unblockScreenOff(); |
| 1422 | updatePowerState(); |
| 1423 | } |
| 1424 | break; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | private final SensorEventListener mProximitySensorListener = new SensorEventListener() { |
| 1430 | @Override |
| 1431 | public void onSensorChanged(SensorEvent event) { |
| 1432 | if (mProximitySensorEnabled) { |
| 1433 | final long time = SystemClock.uptimeMillis(); |
| 1434 | final float distance = event.values[0]; |
| 1435 | boolean positive = distance >= 0.0f && distance < mProximityThreshold; |
| 1436 | handleProximitySensorEvent(time, positive); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | @Override |
| 1441 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 1442 | // Not used. |
| 1443 | } |
| 1444 | }; |
Jeff Brown | 3ee549c | 2014-09-22 20:14:39 -0700 | [diff] [blame] | 1445 | |
| 1446 | private final class ScreenOnUnblocker implements WindowManagerPolicy.ScreenOnListener { |
| 1447 | @Override |
| 1448 | public void onScreenOn() { |
| 1449 | Message msg = mHandler.obtainMessage(MSG_SCREEN_ON_UNBLOCKED, this); |
| 1450 | msg.setAsynchronous(true); |
| 1451 | mHandler.sendMessage(msg); |
| 1452 | } |
| 1453 | } |
Jorim Jaggi | 51304d7 | 2017-05-17 17:25:32 +0200 | [diff] [blame] | 1454 | |
| 1455 | private final class ScreenOffUnblocker implements WindowManagerPolicy.ScreenOffListener { |
| 1456 | |
| 1457 | @Override |
| 1458 | public void onScreenOff() { |
| 1459 | Message msg = mHandler.obtainMessage(MSG_SCREEN_OFF_UNBLOCKED, this); |
| 1460 | msg.setAsynchronous(true); |
| 1461 | mHandler.sendMessage(msg); |
| 1462 | } |
| 1463 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1464 | } |