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 | |
| 17 | package com.android.server.power; |
| 18 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 19 | import com.android.server.lights.LightsManager; |
| 20 | import com.android.server.twilight.TwilightListener; |
| 21 | import com.android.server.twilight.TwilightManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 22 | import com.android.server.twilight.TwilightState; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 23 | |
| 24 | import android.animation.Animator; |
| 25 | import android.animation.ObjectAnimator; |
| 26 | import android.content.Context; |
| 27 | import android.content.res.Resources; |
| 28 | import android.hardware.Sensor; |
| 29 | import android.hardware.SensorEvent; |
| 30 | import android.hardware.SensorEventListener; |
| 31 | import android.hardware.SensorManager; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.Looper; |
| 34 | import android.os.Message; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 35 | import android.os.PowerManager; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 36 | import android.os.SystemClock; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 37 | import android.text.format.DateUtils; |
| 38 | import android.util.FloatMath; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 39 | import android.util.Slog; |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 40 | import android.util.Spline; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 41 | import android.util.TimeUtils; |
| 42 | |
| 43 | import java.io.PrintWriter; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Controls the power state of the display. |
| 47 | * |
| 48 | * Handles the proximity sensor, light sensor, and animations between states |
| 49 | * including the screen off animation. |
| 50 | * |
| 51 | * This component acts independently of the rest of the power manager service. |
| 52 | * In particular, it does not share any state and it only communicates |
| 53 | * via asynchronous callbacks to inform the power manager that something has |
| 54 | * changed. |
| 55 | * |
| 56 | * Everything this class does internally is serialized on its handler although |
| 57 | * it may be accessed by other threads from the outside. |
| 58 | * |
| 59 | * Note that the power manager service guarantees that it will hold a suspend |
| 60 | * blocker as long as the display is not ready. So most of the work done here |
| 61 | * does not need to worry about holding a suspend blocker unless it happens |
| 62 | * independently of the display ready signal. |
| 63 | * |
| 64 | * For debugging, you can make the electron beam and brightness animations run |
| 65 | * slower by changing the "animator duration scale" option in Development Settings. |
| 66 | */ |
| 67 | final class DisplayPowerController { |
| 68 | private static final String TAG = "DisplayPowerController"; |
| 69 | |
| 70 | private static boolean DEBUG = false; |
| 71 | private static final boolean DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT = false; |
| 72 | private static final boolean DEBUG_PRETEND_LIGHT_SENSOR_ABSENT = false; |
| 73 | |
Jeff Brown | 13c589b | 2012-08-16 16:20:54 -0700 | [diff] [blame] | 74 | // If true, uses the electron beam on animation. |
| 75 | // We might want to turn this off if we cannot get a guarantee that the screen |
| 76 | // 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] | 77 | // screen state returns. Playing the animation can also be somewhat slow. |
| 78 | private static final boolean USE_ELECTRON_BEAM_ON_ANIMATION = false; |
Jeff Brown | 13c589b | 2012-08-16 16:20:54 -0700 | [diff] [blame] | 79 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 80 | // If true, enables the use of the screen auto-brightness adjustment setting. |
Jeff Brown | 631938f | 2012-09-08 15:11:11 -0700 | [diff] [blame] | 81 | private static final boolean USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT = |
| 82 | PowerManager.useScreenAutoBrightnessAdjustmentFeature(); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 83 | |
| 84 | // The maximum range of gamma adjustment possible using the screen |
| 85 | // auto-brightness adjustment setting. |
| 86 | private static final float SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT_MAX_GAMMA = 3.0f; |
| 87 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 88 | // The minimum reduction in brightness when dimmed. |
| 89 | private static final int SCREEN_DIM_MINIMUM_REDUCTION = 10; |
| 90 | |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 91 | // If true, enables the use of the current time as an auto-brightness adjustment. |
| 92 | // The basic idea here is to expand the dynamic range of auto-brightness |
| 93 | // when it is especially dark outside. The light sensor tends to perform |
| 94 | // poorly at low light levels so we compensate for it by making an |
| 95 | // assumption about the environment. |
Jeff Brown | db21284 | 2012-10-01 14:33:09 -0700 | [diff] [blame] | 96 | private static final boolean USE_TWILIGHT_ADJUSTMENT = |
| 97 | PowerManager.useTwilightAdjustmentFeature(); |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 98 | |
| 99 | // Specifies the maximum magnitude of the time of day adjustment. |
| 100 | private static final float TWILIGHT_ADJUSTMENT_MAX_GAMMA = 1.5f; |
| 101 | |
| 102 | // The amount of time after or before sunrise over which to start adjusting |
| 103 | // the gamma. We want the change to happen gradually so that it is below the |
| 104 | // threshold of perceptibility and so that the adjustment has maximum effect |
| 105 | // well after dusk. |
| 106 | private static final long TWILIGHT_ADJUSTMENT_TIME = DateUtils.HOUR_IN_MILLIS * 2; |
| 107 | |
Jeff Brown | 00a8f4f | 2012-08-21 23:11:46 -0700 | [diff] [blame] | 108 | private static final int ELECTRON_BEAM_ON_ANIMATION_DURATION_MILLIS = 250; |
Jeff Brown | 3c584f2 | 2012-10-10 14:36:00 -0700 | [diff] [blame] | 109 | private static final int ELECTRON_BEAM_OFF_ANIMATION_DURATION_MILLIS = 400; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 110 | |
| 111 | private static final int MSG_UPDATE_POWER_STATE = 1; |
| 112 | private static final int MSG_PROXIMITY_SENSOR_DEBOUNCED = 2; |
| 113 | private static final int MSG_LIGHT_SENSOR_DEBOUNCED = 3; |
| 114 | |
| 115 | private static final int PROXIMITY_UNKNOWN = -1; |
| 116 | private static final int PROXIMITY_NEGATIVE = 0; |
| 117 | private static final int PROXIMITY_POSITIVE = 1; |
| 118 | |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 119 | // Proximity sensor debounce delay in milliseconds for positive or negative transitions. |
| 120 | private static final int PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY = 0; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 121 | private static final int PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY = 250; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 122 | |
| 123 | // Trigger proximity if distance is less than 5 cm. |
| 124 | private static final float TYPICAL_PROXIMITY_THRESHOLD = 5.0f; |
| 125 | |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 126 | // Light sensor event rate in milliseconds. |
| 127 | private static final int LIGHT_SENSOR_RATE_MILLIS = 1000; |
| 128 | |
| 129 | // A rate for generating synthetic light sensor events in the case where the light |
| 130 | // sensor hasn't reported any new data in a while and we need it to update the |
| 131 | // debounce filter. We only synthesize light sensor measurements when needed. |
| 132 | private static final int SYNTHETIC_LIGHT_SENSOR_RATE_MILLIS = |
| 133 | LIGHT_SENSOR_RATE_MILLIS * 2; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 134 | |
| 135 | // Brightness animation ramp rate in brightness units per second. |
| 136 | private static final int BRIGHTNESS_RAMP_RATE_FAST = 200; |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 137 | private static final int BRIGHTNESS_RAMP_RATE_SLOW = 40; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 138 | |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 139 | // IIR filter time constants in milliseconds for computing two moving averages of |
| 140 | // the light samples. One is a long-term average and the other is a short-term average. |
| 141 | // We can use these filters to assess trends in ambient brightness. |
| 142 | // The short term average gives us a filtered but relatively low latency measurement. |
| 143 | // The long term average informs us about the overall trend. |
| 144 | private static final long SHORT_TERM_AVERAGE_LIGHT_TIME_CONSTANT = 1000; |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 145 | private static final long LONG_TERM_AVERAGE_LIGHT_TIME_CONSTANT = 5000; |
Jeff Brown | 06565b6 | 2012-08-15 21:10:32 -0700 | [diff] [blame] | 146 | |
| 147 | // Stability requirements in milliseconds for accepting a new brightness |
| 148 | // level. This is used for debouncing the light sensor. Different constants |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 149 | // are used to debounce the light sensor when adapting to brighter or darker environments. |
Jeff Brown | 06565b6 | 2012-08-15 21:10:32 -0700 | [diff] [blame] | 150 | // This parameter controls how quickly brightness changes occur in response to |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 151 | // an observed change in light level that exceeds the hysteresis threshold. |
| 152 | private static final long BRIGHTENING_LIGHT_DEBOUNCE = 4000; |
| 153 | private static final long DARKENING_LIGHT_DEBOUNCE = 8000; |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 154 | |
| 155 | // Hysteresis constraints for brightening or darkening. |
| 156 | // The recent lux must have changed by at least this fraction relative to the |
| 157 | // current ambient lux before a change will be considered. |
| 158 | private static final float BRIGHTENING_LIGHT_HYSTERESIS = 0.10f; |
| 159 | private static final float DARKENING_LIGHT_HYSTERESIS = 0.20f; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 160 | |
| 161 | private final Object mLock = new Object(); |
| 162 | |
| 163 | // Notifier for sending asynchronous notifications. |
| 164 | private final Notifier mNotifier; |
| 165 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 166 | // The display suspend blocker. |
| 167 | // Held while there are pending state change notifications. |
| 168 | private final SuspendBlocker mDisplaySuspendBlocker; |
| 169 | |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 170 | // The display blanker. |
| 171 | private final DisplayBlanker mDisplayBlanker; |
| 172 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 173 | // Our handler. |
| 174 | private final DisplayControllerHandler mHandler; |
| 175 | |
| 176 | // Asynchronous callbacks into the power manager service. |
| 177 | // Only invoked from the handler thread while no locks are held. |
| 178 | private final Callbacks mCallbacks; |
| 179 | private Handler mCallbackHandler; |
| 180 | |
| 181 | // The lights service. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 182 | private final LightsManager mLights; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 183 | |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 184 | // The twilight service. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 185 | private final TwilightManager mTwilight; |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 186 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 187 | // The sensor manager. |
| 188 | private final SensorManager mSensorManager; |
| 189 | |
| 190 | // The proximity sensor, or null if not available or needed. |
| 191 | private Sensor mProximitySensor; |
| 192 | |
| 193 | // The light sensor, or null if not available or needed. |
| 194 | private Sensor mLightSensor; |
| 195 | |
| 196 | // The dim screen brightness. |
| 197 | private final int mScreenBrightnessDimConfig; |
| 198 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 199 | // The minimum allowed brightness. |
| 200 | private final int mScreenBrightnessRangeMinimum; |
| 201 | |
| 202 | // The maximum allowed brightness. |
| 203 | private final int mScreenBrightnessRangeMaximum; |
| 204 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 205 | // True if auto-brightness should be used. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 206 | private boolean mUseSoftwareAutoBrightnessConfig; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 207 | |
| 208 | // The auto-brightness spline adjustment. |
| 209 | // The brightness values have been scaled to a range of 0..1. |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 210 | private Spline mScreenAutoBrightnessSpline; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 211 | |
| 212 | // Amount of time to delay auto-brightness after screen on while waiting for |
| 213 | // the light sensor to warm-up in milliseconds. |
| 214 | // May be 0 if no warm-up is required. |
| 215 | private int mLightSensorWarmUpTimeConfig; |
| 216 | |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 217 | // True if we should fade the screen while turning it off, false if we should play |
| 218 | // a stylish electron beam animation instead. |
| 219 | private boolean mElectronBeamFadesConfig; |
Jeff Brown | a52772f | 2012-10-04 18:38:09 -0700 | [diff] [blame] | 220 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 221 | // The pending power request. |
| 222 | // Initially null until the first call to requestPowerState. |
| 223 | // Guarded by mLock. |
| 224 | private DisplayPowerRequest mPendingRequestLocked; |
| 225 | |
| 226 | // True if a request has been made to wait for the proximity sensor to go negative. |
| 227 | // Guarded by mLock. |
| 228 | private boolean mPendingWaitForNegativeProximityLocked; |
| 229 | |
| 230 | // True if the pending power request or wait for negative proximity flag |
| 231 | // has been changed since the last update occurred. |
| 232 | // Guarded by mLock. |
| 233 | private boolean mPendingRequestChangedLocked; |
| 234 | |
| 235 | // Set to true when the important parts of the pending power request have been applied. |
| 236 | // The important parts are mainly the screen state. Brightness changes may occur |
| 237 | // concurrently. |
| 238 | // Guarded by mLock. |
| 239 | private boolean mDisplayReadyLocked; |
| 240 | |
| 241 | // Set to true if a power state update is required. |
| 242 | // Guarded by mLock. |
| 243 | private boolean mPendingUpdatePowerStateLocked; |
| 244 | |
| 245 | /* The following state must only be accessed by the handler thread. */ |
| 246 | |
| 247 | // The currently requested power state. |
| 248 | // The power controller will progressively update its internal state to match |
| 249 | // the requested power state. Initially null until the first update. |
| 250 | private DisplayPowerRequest mPowerRequest; |
| 251 | |
| 252 | // The current power state. |
| 253 | // Must only be accessed on the handler thread. |
| 254 | private DisplayPowerState mPowerState; |
| 255 | |
| 256 | // True if the device should wait for negative proximity sensor before |
| 257 | // waking up the screen. This is set to false as soon as a negative |
| 258 | // proximity sensor measurement is observed or when the device is forced to |
| 259 | // go to sleep by the user. While true, the screen remains off. |
| 260 | private boolean mWaitingForNegativeProximity; |
| 261 | |
| 262 | // The actual proximity sensor threshold value. |
| 263 | private float mProximityThreshold; |
| 264 | |
| 265 | // Set to true if the proximity sensor listener has been registered |
| 266 | // with the sensor manager. |
| 267 | private boolean mProximitySensorEnabled; |
| 268 | |
| 269 | // The debounced proximity sensor state. |
| 270 | private int mProximity = PROXIMITY_UNKNOWN; |
| 271 | |
| 272 | // The raw non-debounced proximity sensor state. |
| 273 | private int mPendingProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 274 | private long mPendingProximityDebounceTime = -1; // -1 if fully debounced |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 275 | |
| 276 | // True if the screen was turned off because of the proximity sensor. |
| 277 | // When the screen turns on again, we report user activity to the power manager. |
| 278 | private boolean mScreenOffBecauseOfProximity; |
| 279 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 280 | // True if the screen on is being blocked. |
| 281 | private boolean mScreenOnWasBlocked; |
| 282 | |
| 283 | // The elapsed real time when the screen on was blocked. |
| 284 | private long mScreenOnBlockStartRealTime; |
| 285 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 286 | // Set to true if the light sensor is enabled. |
| 287 | private boolean mLightSensorEnabled; |
| 288 | |
| 289 | // The time when the light sensor was enabled. |
| 290 | private long mLightSensorEnableTime; |
| 291 | |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 292 | // The currently accepted nominal ambient light level. |
| 293 | private float mAmbientLux; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 294 | |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 295 | // True if mAmbientLux holds a valid value. |
| 296 | private boolean mAmbientLuxValid; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 297 | |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 298 | // The ambient light level threshold at which to brighten or darken the screen. |
| 299 | private float mBrighteningLuxThreshold; |
| 300 | private float mDarkeningLuxThreshold; |
| 301 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 302 | // The most recent light sample. |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 303 | private float mLastObservedLux; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 304 | |
| 305 | // The time of the most light recent sample. |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 306 | private long mLastObservedLuxTime; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 307 | |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 308 | // The number of light samples collected since the light sensor was enabled. |
| 309 | private int mRecentLightSamples; |
Jeff Brown | 06565b6 | 2012-08-15 21:10:32 -0700 | [diff] [blame] | 310 | |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 311 | // The long-term and short-term filtered light measurements. |
| 312 | private float mRecentShortTermAverageLux; |
| 313 | private float mRecentLongTermAverageLux; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 314 | |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 315 | // The direction in which the average lux is moving relative to the current ambient lux. |
| 316 | // 0 if not changing or within hysteresis threshold. |
| 317 | // 1 if brightening beyond hysteresis threshold. |
| 318 | // -1 if darkening beyond hysteresis threshold. |
| 319 | private int mDebounceLuxDirection; |
| 320 | |
| 321 | // The time when the average lux last changed direction. |
| 322 | private long mDebounceLuxTime; |
| 323 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 324 | // The screen brightness level that has been chosen by the auto-brightness |
| 325 | // algorithm. The actual brightness should ramp towards this value. |
| 326 | // We preserve this value even when we stop using the light sensor so |
| 327 | // that we can quickly revert to the previous auto-brightness level |
| 328 | // while the light sensor warms up. |
| 329 | // Use -1 if there is no current auto-brightness value available. |
| 330 | private int mScreenAutoBrightness = -1; |
| 331 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 332 | // The last screen auto-brightness gamma. (For printing in dump() only.) |
| 333 | private float mLastScreenAutoBrightnessGamma = 1.0f; |
| 334 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 335 | // True if the screen auto-brightness value is actually being used to |
| 336 | // set the display brightness. |
| 337 | private boolean mUsingScreenAutoBrightness; |
| 338 | |
| 339 | // Animators. |
| 340 | private ObjectAnimator mElectronBeamOnAnimator; |
| 341 | private ObjectAnimator mElectronBeamOffAnimator; |
| 342 | private RampAnimator<DisplayPowerState> mScreenBrightnessRampAnimator; |
| 343 | |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 344 | // Twilight changed. We might recalculate auto-brightness values. |
| 345 | private boolean mTwilightChanged; |
| 346 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 347 | /** |
| 348 | * Creates the display power controller. |
| 349 | */ |
| 350 | public DisplayPowerController(Looper looper, Context context, Notifier notifier, |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 351 | LightsManager lights, TwilightManager twilight, SensorManager sensorManager, |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 352 | SuspendBlocker displaySuspendBlocker, DisplayBlanker displayBlanker, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 353 | Callbacks callbacks, Handler callbackHandler) { |
| 354 | mHandler = new DisplayControllerHandler(looper); |
| 355 | mNotifier = notifier; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 356 | mDisplaySuspendBlocker = displaySuspendBlocker; |
Jeff Brown | 9e316a1 | 2012-10-08 19:17:06 -0700 | [diff] [blame] | 357 | mDisplayBlanker = displayBlanker; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 358 | mCallbacks = callbacks; |
| 359 | mCallbackHandler = callbackHandler; |
| 360 | |
| 361 | mLights = lights; |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 362 | mTwilight = twilight; |
Jeff Brown | 3b97159 | 2013-01-09 18:46:37 -0800 | [diff] [blame] | 363 | mSensorManager = sensorManager; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 364 | |
| 365 | final Resources resources = context.getResources(); |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 366 | |
| 367 | mScreenBrightnessDimConfig = clampAbsoluteBrightness(resources.getInteger( |
| 368 | com.android.internal.R.integer.config_screenBrightnessDim)); |
| 369 | |
| 370 | int screenBrightnessMinimum = Math.min(resources.getInteger( |
| 371 | com.android.internal.R.integer.config_screenBrightnessSettingMinimum), |
| 372 | mScreenBrightnessDimConfig); |
| 373 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 374 | mUseSoftwareAutoBrightnessConfig = resources.getBoolean( |
| 375 | com.android.internal.R.bool.config_automatic_brightness_available); |
| 376 | if (mUseSoftwareAutoBrightnessConfig) { |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 377 | int[] lux = resources.getIntArray( |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 378 | com.android.internal.R.array.config_autoBrightnessLevels); |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 379 | int[] screenBrightness = resources.getIntArray( |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 380 | com.android.internal.R.array.config_autoBrightnessLcdBacklightValues); |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 381 | |
| 382 | mScreenAutoBrightnessSpline = createAutoBrightnessSpline(lux, screenBrightness); |
| 383 | if (mScreenAutoBrightnessSpline == null) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 384 | Slog.e(TAG, "Error in config.xml. config_autoBrightnessLcdBacklightValues " |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 385 | + "(size " + screenBrightness.length + ") " |
| 386 | + "must be monotic and have exactly one more entry than " |
| 387 | + "config_autoBrightnessLevels (size " + lux.length + ") " |
| 388 | + "which must be strictly increasing. " |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 389 | + "Auto-brightness will be disabled."); |
| 390 | mUseSoftwareAutoBrightnessConfig = false; |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 391 | } else { |
| 392 | if (screenBrightness[0] < screenBrightnessMinimum) { |
| 393 | screenBrightnessMinimum = screenBrightness[0]; |
| 394 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | mLightSensorWarmUpTimeConfig = resources.getInteger( |
| 398 | com.android.internal.R.integer.config_lightSensorWarmupTime); |
| 399 | } |
| 400 | |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 401 | mScreenBrightnessRangeMinimum = clampAbsoluteBrightness(screenBrightnessMinimum); |
| 402 | mScreenBrightnessRangeMaximum = PowerManager.BRIGHTNESS_ON; |
| 403 | |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 404 | mElectronBeamFadesConfig = resources.getBoolean( |
Jeff Brown | a52772f | 2012-10-04 18:38:09 -0700 | [diff] [blame] | 405 | com.android.internal.R.bool.config_animateScreenLights); |
| 406 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 407 | if (!DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT) { |
| 408 | mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); |
| 409 | if (mProximitySensor != null) { |
| 410 | mProximityThreshold = Math.min(mProximitySensor.getMaximumRange(), |
| 411 | TYPICAL_PROXIMITY_THRESHOLD); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (mUseSoftwareAutoBrightnessConfig |
| 416 | && !DEBUG_PRETEND_LIGHT_SENSOR_ABSENT) { |
| 417 | mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); |
| 418 | } |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 419 | |
| 420 | if (mUseSoftwareAutoBrightnessConfig && USE_TWILIGHT_ADJUSTMENT) { |
| 421 | mTwilight.registerListener(mTwilightListener, mHandler); |
| 422 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 425 | private static Spline createAutoBrightnessSpline(int[] lux, int[] brightness) { |
| 426 | try { |
| 427 | final int n = brightness.length; |
| 428 | float[] x = new float[n]; |
| 429 | float[] y = new float[n]; |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 430 | y[0] = normalizeAbsoluteBrightness(brightness[0]); |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 431 | for (int i = 1; i < n; i++) { |
| 432 | x[i] = lux[i - 1]; |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 433 | y[i] = normalizeAbsoluteBrightness(brightness[i]); |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | Spline spline = Spline.createMonotoneCubicSpline(x, y); |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 437 | if (DEBUG) { |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 438 | Slog.d(TAG, "Auto-brightness spline: " + spline); |
| 439 | for (float v = 1f; v < lux[lux.length - 1] * 1.25f; v *= 1.25f) { |
| 440 | Slog.d(TAG, String.format(" %7.1f: %7.1f", v, spline.interpolate(v))); |
| 441 | } |
| 442 | } |
| 443 | return spline; |
| 444 | } catch (IllegalArgumentException ex) { |
| 445 | Slog.e(TAG, "Could not create auto-brightness spline.", ex); |
| 446 | return null; |
| 447 | } |
| 448 | } |
| 449 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 450 | /** |
| 451 | * Returns true if the proximity sensor screen-off function is available. |
| 452 | */ |
| 453 | public boolean isProximitySensorAvailable() { |
| 454 | return mProximitySensor != null; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Requests a new power state. |
| 459 | * The controller makes a copy of the provided object and then |
| 460 | * begins adjusting the power state to match what was requested. |
| 461 | * |
| 462 | * @param request The requested power state. |
| 463 | * @param waitForNegativeProximity If true, issues a request to wait for |
| 464 | * negative proximity before turning the screen back on, assuming the screen |
| 465 | * was turned off by the proximity sensor. |
| 466 | * @return True if display is ready, false if there are important changes that must |
| 467 | * be made asynchronously (such as turning the screen on), in which case the caller |
| 468 | * should grab a wake lock, watch for {@link Callbacks#onStateChanged()} then try |
| 469 | * the request again later until the state converges. |
| 470 | */ |
| 471 | public boolean requestPowerState(DisplayPowerRequest request, |
| 472 | boolean waitForNegativeProximity) { |
| 473 | if (DEBUG) { |
| 474 | Slog.d(TAG, "requestPowerState: " |
| 475 | + request + ", waitForNegativeProximity=" + waitForNegativeProximity); |
| 476 | } |
| 477 | |
| 478 | synchronized (mLock) { |
| 479 | boolean changed = false; |
| 480 | |
| 481 | if (waitForNegativeProximity |
| 482 | && !mPendingWaitForNegativeProximityLocked) { |
| 483 | mPendingWaitForNegativeProximityLocked = true; |
| 484 | changed = true; |
| 485 | } |
| 486 | |
| 487 | if (mPendingRequestLocked == null) { |
| 488 | mPendingRequestLocked = new DisplayPowerRequest(request); |
| 489 | changed = true; |
| 490 | } else if (!mPendingRequestLocked.equals(request)) { |
| 491 | mPendingRequestLocked.copyFrom(request); |
| 492 | changed = true; |
| 493 | } |
| 494 | |
| 495 | if (changed) { |
| 496 | mDisplayReadyLocked = false; |
| 497 | } |
| 498 | |
| 499 | if (changed && !mPendingRequestChangedLocked) { |
| 500 | mPendingRequestChangedLocked = true; |
| 501 | sendUpdatePowerStateLocked(); |
| 502 | } |
| 503 | |
| 504 | return mDisplayReadyLocked; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | private void sendUpdatePowerState() { |
| 509 | synchronized (mLock) { |
| 510 | sendUpdatePowerStateLocked(); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private void sendUpdatePowerStateLocked() { |
| 515 | if (!mPendingUpdatePowerStateLocked) { |
| 516 | mPendingUpdatePowerStateLocked = true; |
| 517 | Message msg = mHandler.obtainMessage(MSG_UPDATE_POWER_STATE); |
| 518 | msg.setAsynchronous(true); |
| 519 | mHandler.sendMessage(msg); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | private void initialize() { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 524 | mPowerState = new DisplayPowerState(new ElectronBeam(), mDisplayBlanker, |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 525 | mLights.getLight(LightsManager.LIGHT_ID_BACKLIGHT)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 526 | |
| 527 | mElectronBeamOnAnimator = ObjectAnimator.ofFloat( |
| 528 | mPowerState, DisplayPowerState.ELECTRON_BEAM_LEVEL, 0.0f, 1.0f); |
| 529 | mElectronBeamOnAnimator.setDuration(ELECTRON_BEAM_ON_ANIMATION_DURATION_MILLIS); |
| 530 | mElectronBeamOnAnimator.addListener(mAnimatorListener); |
| 531 | |
| 532 | mElectronBeamOffAnimator = ObjectAnimator.ofFloat( |
| 533 | mPowerState, DisplayPowerState.ELECTRON_BEAM_LEVEL, 1.0f, 0.0f); |
| 534 | mElectronBeamOffAnimator.setDuration(ELECTRON_BEAM_OFF_ANIMATION_DURATION_MILLIS); |
| 535 | mElectronBeamOffAnimator.addListener(mAnimatorListener); |
| 536 | |
| 537 | mScreenBrightnessRampAnimator = new RampAnimator<DisplayPowerState>( |
| 538 | mPowerState, DisplayPowerState.SCREEN_BRIGHTNESS); |
Dianne Hackborn | 3d658bf | 2014-02-05 13:38:56 -0800 | [diff] [blame] | 539 | |
| 540 | // Initialize screen on state. |
| 541 | if (mPowerState.isScreenOn()) { |
| 542 | mNotifier.onScreenOn(); |
| 543 | } else { |
| 544 | mNotifier.onScreenOff(); |
| 545 | } |
| 546 | mNotifier.onScreenBrightness(mPowerState.getScreenBrightness()); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | private final Animator.AnimatorListener mAnimatorListener = new Animator.AnimatorListener() { |
| 550 | @Override |
| 551 | public void onAnimationStart(Animator animation) { |
| 552 | } |
| 553 | @Override |
| 554 | public void onAnimationEnd(Animator animation) { |
| 555 | sendUpdatePowerState(); |
| 556 | } |
| 557 | @Override |
| 558 | public void onAnimationRepeat(Animator animation) { |
| 559 | } |
| 560 | @Override |
| 561 | public void onAnimationCancel(Animator animation) { |
| 562 | } |
| 563 | }; |
| 564 | |
| 565 | private void updatePowerState() { |
| 566 | // Update the power state request. |
| 567 | final boolean mustNotify; |
| 568 | boolean mustInitialize = false; |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 569 | boolean updateAutoBrightness = mTwilightChanged; |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 570 | boolean wasDim = false; |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 571 | mTwilightChanged = false; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 572 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 573 | synchronized (mLock) { |
| 574 | mPendingUpdatePowerStateLocked = false; |
| 575 | if (mPendingRequestLocked == null) { |
| 576 | return; // wait until first actual power request |
| 577 | } |
| 578 | |
| 579 | if (mPowerRequest == null) { |
| 580 | mPowerRequest = new DisplayPowerRequest(mPendingRequestLocked); |
| 581 | mWaitingForNegativeProximity = mPendingWaitForNegativeProximityLocked; |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 582 | mPendingWaitForNegativeProximityLocked = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 583 | mPendingRequestChangedLocked = false; |
| 584 | mustInitialize = true; |
| 585 | } else if (mPendingRequestChangedLocked) { |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 586 | if (mPowerRequest.screenAutoBrightnessAdjustment |
| 587 | != mPendingRequestLocked.screenAutoBrightnessAdjustment) { |
| 588 | updateAutoBrightness = true; |
| 589 | } |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 590 | wasDim = (mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 591 | mPowerRequest.copyFrom(mPendingRequestLocked); |
| 592 | mWaitingForNegativeProximity |= mPendingWaitForNegativeProximityLocked; |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 593 | mPendingWaitForNegativeProximityLocked = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 594 | mPendingRequestChangedLocked = false; |
| 595 | mDisplayReadyLocked = false; |
| 596 | } |
| 597 | |
| 598 | mustNotify = !mDisplayReadyLocked; |
| 599 | } |
| 600 | |
| 601 | // Initialize things the first time the power state is changed. |
| 602 | if (mustInitialize) { |
| 603 | initialize(); |
| 604 | } |
| 605 | |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 606 | // Apply the proximity sensor. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 607 | if (mProximitySensor != null) { |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 608 | if (mPowerRequest.useProximitySensor |
| 609 | && mPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF) { |
| 610 | setProximitySensorEnabled(true); |
| 611 | if (!mScreenOffBecauseOfProximity |
| 612 | && mProximity == PROXIMITY_POSITIVE) { |
| 613 | mScreenOffBecauseOfProximity = true; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 614 | sendOnProximityPositiveWithWakelock(); |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 615 | setScreenOn(false); |
| 616 | } |
| 617 | } else if (mWaitingForNegativeProximity |
| 618 | && mScreenOffBecauseOfProximity |
| 619 | && mProximity == PROXIMITY_POSITIVE |
| 620 | && mPowerRequest.screenState != DisplayPowerRequest.SCREEN_STATE_OFF) { |
| 621 | setProximitySensorEnabled(true); |
| 622 | } else { |
| 623 | setProximitySensorEnabled(false); |
| 624 | mWaitingForNegativeProximity = false; |
| 625 | } |
| 626 | if (mScreenOffBecauseOfProximity |
| 627 | && mProximity != PROXIMITY_POSITIVE) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 628 | mScreenOffBecauseOfProximity = false; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 629 | sendOnProximityNegativeWithWakelock(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 630 | } |
Jeff Brown | 6307a15 | 2012-08-20 13:24:23 -0700 | [diff] [blame] | 631 | } else { |
| 632 | mWaitingForNegativeProximity = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | // Turn on the light sensor if needed. |
| 636 | if (mLightSensor != null) { |
| 637 | setLightSensorEnabled(mPowerRequest.useAutoBrightness |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 638 | && wantScreenOn(mPowerRequest.screenState), updateAutoBrightness); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // Set the screen brightness. |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 642 | if (wantScreenOn(mPowerRequest.screenState)) { |
| 643 | int target; |
| 644 | boolean slow; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 645 | if (mScreenAutoBrightness >= 0 && mLightSensorEnabled) { |
| 646 | // Use current auto-brightness value. |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 647 | target = mScreenAutoBrightness; |
| 648 | slow = mUsingScreenAutoBrightness; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 649 | mUsingScreenAutoBrightness = true; |
| 650 | } else { |
| 651 | // Light sensor is disabled or not ready yet. |
| 652 | // Use the current brightness setting from the request, which is expected |
| 653 | // provide a nominal default value for the case where auto-brightness |
| 654 | // is not ready yet. |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 655 | target = mPowerRequest.screenBrightness; |
| 656 | slow = false; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 657 | mUsingScreenAutoBrightness = false; |
| 658 | } |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 659 | if (mPowerRequest.screenState == DisplayPowerRequest.SCREEN_STATE_DIM) { |
Jeff Brown | 5244c93 | 2012-10-24 14:46:26 -0700 | [diff] [blame] | 660 | // Dim quickly by at least some minimum amount. |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 661 | target = Math.min(target - SCREEN_DIM_MINIMUM_REDUCTION, |
| 662 | mScreenBrightnessDimConfig); |
Jeff Brown | 5244c93 | 2012-10-24 14:46:26 -0700 | [diff] [blame] | 663 | slow = false; |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 664 | } else if (wasDim) { |
| 665 | // Brighten quickly. |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 666 | slow = false; |
| 667 | } |
| 668 | animateScreenBrightness(clampScreenBrightness(target), |
| 669 | slow ? BRIGHTNESS_RAMP_RATE_SLOW : BRIGHTNESS_RAMP_RATE_FAST); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 670 | } else { |
| 671 | // Screen is off. Don't bother changing the brightness. |
| 672 | mUsingScreenAutoBrightness = false; |
| 673 | } |
| 674 | |
| 675 | // Animate the screen on or off. |
| 676 | if (!mScreenOffBecauseOfProximity) { |
| 677 | if (wantScreenOn(mPowerRequest.screenState)) { |
| 678 | // Want screen on. |
| 679 | // Wait for previous off animation to complete beforehand. |
| 680 | // It is relatively short but if we cancel it and switch to the |
| 681 | // on animation immediately then the results are pretty ugly. |
| 682 | if (!mElectronBeamOffAnimator.isStarted()) { |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 683 | // Turn the screen on. The contents of the screen may not yet |
| 684 | // be visible if the electron beam has not been dismissed because |
| 685 | // its last frame of animation is solid black. |
| 686 | setScreenOn(true); |
| 687 | |
| 688 | if (mPowerRequest.blockScreenOn |
| 689 | && mPowerState.getElectronBeamLevel() == 0.0f) { |
| 690 | blockScreenOn(); |
Jeff Brown | 13c589b | 2012-08-16 16:20:54 -0700 | [diff] [blame] | 691 | } else { |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 692 | unblockScreenOn(); |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 693 | if (USE_ELECTRON_BEAM_ON_ANIMATION) { |
| 694 | if (!mElectronBeamOnAnimator.isStarted()) { |
| 695 | if (mPowerState.getElectronBeamLevel() == 1.0f) { |
| 696 | mPowerState.dismissElectronBeam(); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 697 | } else if (mPowerState.prepareElectronBeam( |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 698 | mElectronBeamFadesConfig ? |
| 699 | ElectronBeam.MODE_FADE : |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 700 | ElectronBeam.MODE_WARM_UP)) { |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 701 | mElectronBeamOnAnimator.start(); |
| 702 | } else { |
| 703 | mElectronBeamOnAnimator.end(); |
| 704 | } |
| 705 | } |
| 706 | } else { |
| 707 | mPowerState.setElectronBeamLevel(1.0f); |
| 708 | mPowerState.dismissElectronBeam(); |
| 709 | } |
| 710 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 711 | } |
| 712 | } else { |
| 713 | // Want screen off. |
| 714 | // Wait for previous on animation to complete beforehand. |
| 715 | if (!mElectronBeamOnAnimator.isStarted()) { |
| 716 | if (!mElectronBeamOffAnimator.isStarted()) { |
| 717 | if (mPowerState.getElectronBeamLevel() == 0.0f) { |
| 718 | setScreenOn(false); |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 719 | } else if (mPowerState.prepareElectronBeam( |
Jeff Brown | 252c206 | 2012-10-08 16:21:01 -0700 | [diff] [blame] | 720 | mElectronBeamFadesConfig ? |
| 721 | ElectronBeam.MODE_FADE : |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 722 | ElectronBeam.MODE_COOL_DOWN) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 723 | && mPowerState.isScreenOn()) { |
| 724 | mElectronBeamOffAnimator.start(); |
| 725 | } else { |
| 726 | mElectronBeamOffAnimator.end(); |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | // Report whether the display is ready for use. |
| 734 | // We mostly care about the screen state here, ignoring brightness changes |
| 735 | // which will be handled asynchronously. |
| 736 | if (mustNotify |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 737 | && !mScreenOnWasBlocked |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 738 | && !mElectronBeamOnAnimator.isStarted() |
| 739 | && !mElectronBeamOffAnimator.isStarted() |
| 740 | && mPowerState.waitUntilClean(mCleanListener)) { |
| 741 | synchronized (mLock) { |
| 742 | if (!mPendingRequestChangedLocked) { |
| 743 | mDisplayReadyLocked = true; |
Jeff Brown | c38c9be | 2012-10-04 13:16:19 -0700 | [diff] [blame] | 744 | |
| 745 | if (DEBUG) { |
| 746 | Slog.d(TAG, "Display ready!"); |
| 747 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 750 | sendOnStateChangedWithWakelock(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Jeff Brown | 8b9cf1c | 2012-10-07 14:54:17 -0700 | [diff] [blame] | 754 | private void blockScreenOn() { |
| 755 | if (!mScreenOnWasBlocked) { |
| 756 | mScreenOnWasBlocked = true; |
| 757 | if (DEBUG) { |
| 758 | Slog.d(TAG, "Blocked screen on."); |
| 759 | mScreenOnBlockStartRealTime = SystemClock.elapsedRealtime(); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | private void unblockScreenOn() { |
| 765 | if (mScreenOnWasBlocked) { |
| 766 | mScreenOnWasBlocked = false; |
| 767 | if (DEBUG) { |
| 768 | Slog.d(TAG, "Unblocked screen on after " + |
| 769 | (SystemClock.elapsedRealtime() - mScreenOnBlockStartRealTime) + " ms"); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 774 | private void setScreenOn(boolean on) { |
| 775 | if (!mPowerState.isScreenOn() == on) { |
| 776 | mPowerState.setScreenOn(on); |
| 777 | if (on) { |
| 778 | mNotifier.onScreenOn(); |
| 779 | } else { |
| 780 | mNotifier.onScreenOff(); |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 785 | private int clampScreenBrightness(int value) { |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 786 | return clamp(value, mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum); |
| 787 | } |
| 788 | |
| 789 | private static int clampAbsoluteBrightness(int value) { |
| 790 | return clamp(value, PowerManager.BRIGHTNESS_OFF, PowerManager.BRIGHTNESS_ON); |
| 791 | } |
| 792 | |
| 793 | private static int clamp(int value, int min, int max) { |
| 794 | if (value <= min) { |
| 795 | return min; |
| 796 | } |
| 797 | if (value >= max) { |
| 798 | return max; |
| 799 | } |
| 800 | return value; |
| 801 | } |
| 802 | |
| 803 | private static float normalizeAbsoluteBrightness(int value) { |
| 804 | return (float)clampAbsoluteBrightness(value) / PowerManager.BRIGHTNESS_ON; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 807 | private void animateScreenBrightness(int target, int rate) { |
| 808 | if (mScreenBrightnessRampAnimator.animateTo(target, rate)) { |
| 809 | mNotifier.onScreenBrightness(target); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | private final Runnable mCleanListener = new Runnable() { |
| 814 | @Override |
| 815 | public void run() { |
| 816 | sendUpdatePowerState(); |
| 817 | } |
| 818 | }; |
| 819 | |
| 820 | private void setProximitySensorEnabled(boolean enable) { |
| 821 | if (enable) { |
| 822 | if (!mProximitySensorEnabled) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 823 | // Register the listener. |
| 824 | // Proximity sensor state already cleared initially. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 825 | mProximitySensorEnabled = true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 826 | mSensorManager.registerListener(mProximitySensorListener, mProximitySensor, |
| 827 | SensorManager.SENSOR_DELAY_NORMAL, mHandler); |
| 828 | } |
| 829 | } else { |
| 830 | if (mProximitySensorEnabled) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 831 | // Unregister the listener. |
| 832 | // Clear the proximity sensor state for next time. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 833 | mProximitySensorEnabled = false; |
| 834 | mProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 835 | mPendingProximity = PROXIMITY_UNKNOWN; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 836 | mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
| 837 | mSensorManager.unregisterListener(mProximitySensorListener); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 838 | clearPendingProximityDebounceTime(); // release wake lock (must be last) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | private void handleProximitySensorEvent(long time, boolean positive) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 844 | if (mProximitySensorEnabled) { |
| 845 | if (mPendingProximity == PROXIMITY_NEGATIVE && !positive) { |
| 846 | return; // no change |
| 847 | } |
| 848 | if (mPendingProximity == PROXIMITY_POSITIVE && positive) { |
| 849 | return; // no change |
| 850 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 851 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 852 | // Only accept a proximity sensor reading if it remains |
| 853 | // stable for the entire debounce delay. We hold a wake lock while |
| 854 | // debouncing the sensor. |
| 855 | mHandler.removeMessages(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
| 856 | if (positive) { |
| 857 | mPendingProximity = PROXIMITY_POSITIVE; |
| 858 | setPendingProximityDebounceTime( |
| 859 | time + PROXIMITY_SENSOR_POSITIVE_DEBOUNCE_DELAY); // acquire wake lock |
| 860 | } else { |
| 861 | mPendingProximity = PROXIMITY_NEGATIVE; |
| 862 | setPendingProximityDebounceTime( |
| 863 | time + PROXIMITY_SENSOR_NEGATIVE_DEBOUNCE_DELAY); // acquire wake lock |
| 864 | } |
| 865 | |
| 866 | // Debounce the new sensor reading. |
| 867 | debounceProximitySensor(); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 868 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | private void debounceProximitySensor() { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 872 | if (mProximitySensorEnabled |
| 873 | && mPendingProximity != PROXIMITY_UNKNOWN |
| 874 | && mPendingProximityDebounceTime >= 0) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 875 | final long now = SystemClock.uptimeMillis(); |
| 876 | if (mPendingProximityDebounceTime <= now) { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 877 | // Sensor reading accepted. Apply the change then release the wake lock. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 878 | mProximity = mPendingProximity; |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 879 | updatePowerState(); |
| 880 | clearPendingProximityDebounceTime(); // release wake lock (must be last) |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 881 | } else { |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 882 | // Need to wait a little longer. |
| 883 | // Debounce again later. We continue holding a wake lock while waiting. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 884 | Message msg = mHandler.obtainMessage(MSG_PROXIMITY_SENSOR_DEBOUNCED); |
| 885 | msg.setAsynchronous(true); |
| 886 | mHandler.sendMessageAtTime(msg, mPendingProximityDebounceTime); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 891 | private void clearPendingProximityDebounceTime() { |
| 892 | if (mPendingProximityDebounceTime >= 0) { |
| 893 | mPendingProximityDebounceTime = -1; |
| 894 | mDisplaySuspendBlocker.release(); // release wake lock |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | private void setPendingProximityDebounceTime(long debounceTime) { |
| 899 | if (mPendingProximityDebounceTime < 0) { |
| 900 | mDisplaySuspendBlocker.acquire(); // acquire wake lock |
| 901 | } |
| 902 | mPendingProximityDebounceTime = debounceTime; |
| 903 | } |
| 904 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 905 | private void setLightSensorEnabled(boolean enable, boolean updateAutoBrightness) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 906 | if (enable) { |
| 907 | if (!mLightSensorEnabled) { |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 908 | updateAutoBrightness = true; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 909 | mLightSensorEnabled = true; |
| 910 | mLightSensorEnableTime = SystemClock.uptimeMillis(); |
| 911 | mSensorManager.registerListener(mLightSensorListener, mLightSensor, |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 912 | LIGHT_SENSOR_RATE_MILLIS * 1000, mHandler); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 913 | } |
| 914 | } else { |
| 915 | if (mLightSensorEnabled) { |
| 916 | mLightSensorEnabled = false; |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 917 | mAmbientLuxValid = false; |
| 918 | mRecentLightSamples = 0; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 919 | mHandler.removeMessages(MSG_LIGHT_SENSOR_DEBOUNCED); |
| 920 | mSensorManager.unregisterListener(mLightSensorListener); |
| 921 | } |
| 922 | } |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 923 | if (updateAutoBrightness) { |
| 924 | updateAutoBrightness(false); |
| 925 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | private void handleLightSensorEvent(long time, float lux) { |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 929 | mHandler.removeMessages(MSG_LIGHT_SENSOR_DEBOUNCED); |
| 930 | |
| 931 | applyLightSensorMeasurement(time, lux); |
| 932 | updateAmbientLux(time); |
| 933 | } |
| 934 | |
| 935 | private void applyLightSensorMeasurement(long time, float lux) { |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 936 | // Update our filters. |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 937 | mRecentLightSamples += 1; |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 938 | if (mRecentLightSamples == 1) { |
| 939 | mRecentShortTermAverageLux = lux; |
| 940 | mRecentLongTermAverageLux = lux; |
| 941 | } else { |
| 942 | final long timeDelta = time - mLastObservedLuxTime; |
| 943 | mRecentShortTermAverageLux += (lux - mRecentShortTermAverageLux) |
| 944 | * timeDelta / (SHORT_TERM_AVERAGE_LIGHT_TIME_CONSTANT + timeDelta); |
| 945 | mRecentLongTermAverageLux += (lux - mRecentLongTermAverageLux) |
| 946 | * timeDelta / (LONG_TERM_AVERAGE_LIGHT_TIME_CONSTANT + timeDelta); |
| 947 | } |
| 948 | |
| 949 | // Remember this sample value. |
| 950 | mLastObservedLux = lux; |
| 951 | mLastObservedLuxTime = time; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 954 | private void setAmbientLux(float lux) { |
| 955 | mAmbientLux = lux; |
| 956 | mBrighteningLuxThreshold = mAmbientLux * (1.0f + BRIGHTENING_LIGHT_HYSTERESIS); |
| 957 | mDarkeningLuxThreshold = mAmbientLux * (1.0f - DARKENING_LIGHT_HYSTERESIS); |
| 958 | } |
| 959 | |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 960 | private void updateAmbientLux(long time) { |
| 961 | // If the light sensor was just turned on then immediately update our initial |
| 962 | // estimate of the current ambient light level. |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 963 | if (!mAmbientLuxValid) { |
| 964 | final long timeWhenSensorWarmedUp = |
| 965 | mLightSensorWarmUpTimeConfig + mLightSensorEnableTime; |
| 966 | if (time < timeWhenSensorWarmedUp) { |
| 967 | mHandler.sendEmptyMessageAtTime(MSG_LIGHT_SENSOR_DEBOUNCED, |
| 968 | timeWhenSensorWarmedUp); |
| 969 | return; |
| 970 | } |
| 971 | setAmbientLux(mRecentShortTermAverageLux); |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 972 | mAmbientLuxValid = true; |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 973 | mDebounceLuxDirection = 0; |
| 974 | mDebounceLuxTime = time; |
| 975 | if (DEBUG) { |
| 976 | Slog.d(TAG, "updateAmbientLux: Initializing: " |
| 977 | + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux |
| 978 | + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux |
| 979 | + ", mAmbientLux=" + mAmbientLux); |
| 980 | } |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 981 | updateAutoBrightness(true); |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 982 | } else if (mRecentShortTermAverageLux > mBrighteningLuxThreshold |
| 983 | && mRecentLongTermAverageLux > mBrighteningLuxThreshold) { |
| 984 | // The ambient environment appears to be brightening. |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 985 | if (mDebounceLuxDirection <= 0) { |
| 986 | mDebounceLuxDirection = 1; |
| 987 | mDebounceLuxTime = time; |
| 988 | if (DEBUG) { |
| 989 | Slog.d(TAG, "updateAmbientLux: Possibly brightened, waiting for " |
| 990 | + BRIGHTENING_LIGHT_DEBOUNCE + " ms: " |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 991 | + "mBrighteningLuxThreshold=" + mBrighteningLuxThreshold |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 992 | + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux |
| 993 | + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux |
| 994 | + ", mAmbientLux=" + mAmbientLux); |
| 995 | } |
| 996 | } |
| 997 | long debounceTime = mDebounceLuxTime + BRIGHTENING_LIGHT_DEBOUNCE; |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 998 | if (time < debounceTime) { |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 999 | mHandler.sendEmptyMessageAtTime(MSG_LIGHT_SENSOR_DEBOUNCED, debounceTime); |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 1000 | return; |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1001 | } |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 1002 | setAmbientLux(mRecentShortTermAverageLux); |
| 1003 | if (DEBUG) { |
| 1004 | Slog.d(TAG, "updateAmbientLux: Brightened: " |
| 1005 | + "mBrighteningLuxThreshold=" + mBrighteningLuxThreshold |
| 1006 | + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux |
| 1007 | + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux |
| 1008 | + ", mAmbientLux=" + mAmbientLux); |
| 1009 | } |
| 1010 | updateAutoBrightness(true); |
| 1011 | } else if (mRecentShortTermAverageLux < mDarkeningLuxThreshold |
| 1012 | && mRecentLongTermAverageLux < mDarkeningLuxThreshold) { |
| 1013 | // The ambient environment appears to be darkening. |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 1014 | if (mDebounceLuxDirection >= 0) { |
| 1015 | mDebounceLuxDirection = -1; |
| 1016 | mDebounceLuxTime = time; |
| 1017 | if (DEBUG) { |
| 1018 | Slog.d(TAG, "updateAmbientLux: Possibly darkened, waiting for " |
| 1019 | + DARKENING_LIGHT_DEBOUNCE + " ms: " |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 1020 | + "mDarkeningLuxThreshold=" + mDarkeningLuxThreshold |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 1021 | + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux |
| 1022 | + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux |
| 1023 | + ", mAmbientLux=" + mAmbientLux); |
| 1024 | } |
| 1025 | } |
| 1026 | long debounceTime = mDebounceLuxTime + DARKENING_LIGHT_DEBOUNCE; |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 1027 | if (time < debounceTime) { |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1028 | mHandler.sendEmptyMessageAtTime(MSG_LIGHT_SENSOR_DEBOUNCED, debounceTime); |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 1029 | return; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1030 | } |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 1031 | // Be conservative about reducing the brightness, only reduce it a little bit |
| 1032 | // at a time to avoid having to bump it up again soon. |
| 1033 | setAmbientLux(Math.max(mRecentShortTermAverageLux, mRecentLongTermAverageLux)); |
| 1034 | if (DEBUG) { |
| 1035 | Slog.d(TAG, "updateAmbientLux: Darkened: " |
| 1036 | + "mDarkeningLuxThreshold=" + mDarkeningLuxThreshold |
| 1037 | + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux |
| 1038 | + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux |
| 1039 | + ", mAmbientLux=" + mAmbientLux); |
| 1040 | } |
| 1041 | updateAutoBrightness(true); |
| 1042 | } else if (mDebounceLuxDirection != 0) { |
| 1043 | // No change or change is within the hysteresis thresholds. |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 1044 | mDebounceLuxDirection = 0; |
| 1045 | mDebounceLuxTime = time; |
| 1046 | if (DEBUG) { |
| 1047 | Slog.d(TAG, "updateAmbientLux: Canceled debounce: " |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 1048 | + "mBrighteningLuxThreshold=" + mBrighteningLuxThreshold |
| 1049 | + ", mDarkeningLuxThreshold=" + mDarkeningLuxThreshold |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 1050 | + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux |
| 1051 | + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux |
| 1052 | + ", mAmbientLux=" + mAmbientLux); |
| 1053 | } |
| 1054 | } |
| 1055 | |
Michael Wright | 65031a2 | 2013-11-04 19:07:49 -0800 | [diff] [blame] | 1056 | // Now that we've done all of that, we haven't yet posted a debounce |
| 1057 | // message. So consider the case where current lux is beyond the |
| 1058 | // threshold. It's possible that the light sensor may not report values |
| 1059 | // if the light level does not change, so we need to occasionally |
| 1060 | // synthesize sensor readings in order to make sure the brightness is |
| 1061 | // adjusted accordingly. Note these thresholds may have changed since |
| 1062 | // we entered the function because we called setAmbientLux and |
| 1063 | // updateAutoBrightness along the way. |
| 1064 | if (mLastObservedLux > mBrighteningLuxThreshold |
| 1065 | || mLastObservedLux < mDarkeningLuxThreshold) { |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 1066 | mHandler.sendEmptyMessageAtTime(MSG_LIGHT_SENSOR_DEBOUNCED, |
| 1067 | time + SYNTHETIC_LIGHT_SENSOR_RATE_MILLIS); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1071 | private void debounceLightSensor() { |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 1072 | if (mLightSensorEnabled) { |
| 1073 | long time = SystemClock.uptimeMillis(); |
| 1074 | if (time >= mLastObservedLuxTime + SYNTHETIC_LIGHT_SENSOR_RATE_MILLIS) { |
| 1075 | if (DEBUG) { |
| 1076 | Slog.d(TAG, "debounceLightSensor: Synthesizing light sensor measurement " |
| 1077 | + "after " + (time - mLastObservedLuxTime) + " ms."); |
| 1078 | } |
| 1079 | applyLightSensorMeasurement(time, mLastObservedLux); |
| 1080 | } |
| 1081 | updateAmbientLux(time); |
| 1082 | } |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1085 | private void updateAutoBrightness(boolean sendUpdate) { |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1086 | if (!mAmbientLuxValid) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1087 | return; |
| 1088 | } |
| 1089 | |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1090 | float value = mScreenAutoBrightnessSpline.interpolate(mAmbientLux); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1091 | float gamma = 1.0f; |
| 1092 | |
| 1093 | if (USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT |
| 1094 | && mPowerRequest.screenAutoBrightnessAdjustment != 0.0f) { |
| 1095 | final float adjGamma = FloatMath.pow(SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT_MAX_GAMMA, |
| 1096 | Math.min(1.0f, Math.max(-1.0f, |
| 1097 | -mPowerRequest.screenAutoBrightnessAdjustment))); |
| 1098 | gamma *= adjGamma; |
| 1099 | if (DEBUG) { |
| 1100 | Slog.d(TAG, "updateAutoBrightness: adjGamma=" + adjGamma); |
| 1101 | } |
| 1102 | } |
| 1103 | |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 1104 | if (USE_TWILIGHT_ADJUSTMENT) { |
| 1105 | TwilightState state = mTwilight.getCurrentState(); |
| 1106 | if (state != null && state.isNight()) { |
| 1107 | final long now = System.currentTimeMillis(); |
| 1108 | final float earlyGamma = |
| 1109 | getTwilightGamma(now, state.getYesterdaySunset(), state.getTodaySunrise()); |
| 1110 | final float lateGamma = |
| 1111 | getTwilightGamma(now, state.getTodaySunset(), state.getTomorrowSunrise()); |
| 1112 | gamma *= earlyGamma * lateGamma; |
| 1113 | if (DEBUG) { |
| 1114 | Slog.d(TAG, "updateAutoBrightness: earlyGamma=" + earlyGamma |
| 1115 | + ", lateGamma=" + lateGamma); |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1120 | if (gamma != 1.0f) { |
| 1121 | final float in = value; |
| 1122 | value = FloatMath.pow(value, gamma); |
| 1123 | if (DEBUG) { |
| 1124 | Slog.d(TAG, "updateAutoBrightness: gamma=" + gamma |
| 1125 | + ", in=" + in + ", out=" + value); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | int newScreenAutoBrightness = clampScreenBrightness( |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1130 | Math.round(value * PowerManager.BRIGHTNESS_ON)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1131 | if (mScreenAutoBrightness != newScreenAutoBrightness) { |
| 1132 | if (DEBUG) { |
| 1133 | Slog.d(TAG, "updateAutoBrightness: mScreenAutoBrightness=" |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1134 | + mScreenAutoBrightness + ", newScreenAutoBrightness=" |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 1135 | + newScreenAutoBrightness); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | mScreenAutoBrightness = newScreenAutoBrightness; |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1139 | mLastScreenAutoBrightnessGamma = gamma; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1140 | if (sendUpdate) { |
| 1141 | sendUpdatePowerState(); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 1146 | private static float getTwilightGamma(long now, long lastSunset, long nextSunrise) { |
| 1147 | if (lastSunset < 0 || nextSunrise < 0 |
| 1148 | || now < lastSunset || now > nextSunrise) { |
| 1149 | return 1.0f; |
| 1150 | } |
| 1151 | |
| 1152 | if (now < lastSunset + TWILIGHT_ADJUSTMENT_TIME) { |
| 1153 | return lerp(1.0f, TWILIGHT_ADJUSTMENT_MAX_GAMMA, |
| 1154 | (float)(now - lastSunset) / TWILIGHT_ADJUSTMENT_TIME); |
| 1155 | } |
| 1156 | |
| 1157 | if (now > nextSunrise - TWILIGHT_ADJUSTMENT_TIME) { |
| 1158 | return lerp(1.0f, TWILIGHT_ADJUSTMENT_MAX_GAMMA, |
| 1159 | (float)(nextSunrise - now) / TWILIGHT_ADJUSTMENT_TIME); |
| 1160 | } |
| 1161 | |
| 1162 | return TWILIGHT_ADJUSTMENT_MAX_GAMMA; |
| 1163 | } |
| 1164 | |
| 1165 | private static float lerp(float x, float y, float alpha) { |
| 1166 | return x + (y - x) * alpha; |
| 1167 | } |
| 1168 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1169 | private void sendOnStateChangedWithWakelock() { |
| 1170 | mDisplaySuspendBlocker.acquire(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1171 | mCallbackHandler.post(mOnStateChangedRunnable); |
| 1172 | } |
| 1173 | |
| 1174 | private final Runnable mOnStateChangedRunnable = new Runnable() { |
| 1175 | @Override |
| 1176 | public void run() { |
| 1177 | mCallbacks.onStateChanged(); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1178 | mDisplaySuspendBlocker.release(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1179 | } |
| 1180 | }; |
| 1181 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1182 | private void sendOnProximityPositiveWithWakelock() { |
| 1183 | mDisplaySuspendBlocker.acquire(); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1184 | mCallbackHandler.post(mOnProximityPositiveRunnable); |
| 1185 | } |
| 1186 | |
| 1187 | private final Runnable mOnProximityPositiveRunnable = new Runnable() { |
| 1188 | @Override |
| 1189 | public void run() { |
| 1190 | mCallbacks.onProximityPositive(); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1191 | mDisplaySuspendBlocker.release(); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | }; |
| 1194 | |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1195 | private void sendOnProximityNegativeWithWakelock() { |
| 1196 | mDisplaySuspendBlocker.acquire(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1197 | mCallbackHandler.post(mOnProximityNegativeRunnable); |
| 1198 | } |
| 1199 | |
| 1200 | private final Runnable mOnProximityNegativeRunnable = new Runnable() { |
| 1201 | @Override |
| 1202 | public void run() { |
| 1203 | mCallbacks.onProximityNegative(); |
Jeff Brown | ec08321 | 2013-09-11 20:45:25 -0700 | [diff] [blame] | 1204 | mDisplaySuspendBlocker.release(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1205 | } |
| 1206 | }; |
| 1207 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1208 | public void dump(final PrintWriter pw) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1209 | synchronized (mLock) { |
| 1210 | pw.println(); |
| 1211 | pw.println("Display Controller Locked State:"); |
| 1212 | pw.println(" mDisplayReadyLocked=" + mDisplayReadyLocked); |
| 1213 | pw.println(" mPendingRequestLocked=" + mPendingRequestLocked); |
| 1214 | pw.println(" mPendingRequestChangedLocked=" + mPendingRequestChangedLocked); |
| 1215 | pw.println(" mPendingWaitForNegativeProximityLocked=" |
| 1216 | + mPendingWaitForNegativeProximityLocked); |
| 1217 | pw.println(" mPendingUpdatePowerStateLocked=" + mPendingUpdatePowerStateLocked); |
| 1218 | } |
| 1219 | |
| 1220 | pw.println(); |
| 1221 | pw.println("Display Controller Configuration:"); |
| 1222 | pw.println(" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig); |
Jeff Brown | b76eebff | 2012-10-05 22:26:44 -0700 | [diff] [blame] | 1223 | pw.println(" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum); |
| 1224 | pw.println(" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1225 | pw.println(" mUseSoftwareAutoBrightnessConfig=" |
| 1226 | + mUseSoftwareAutoBrightnessConfig); |
Jeff Brown | 1a30b55 | 2012-08-16 01:31:11 -0700 | [diff] [blame] | 1227 | pw.println(" mScreenAutoBrightnessSpline=" + mScreenAutoBrightnessSpline); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1228 | pw.println(" mLightSensorWarmUpTimeConfig=" + mLightSensorWarmUpTimeConfig); |
| 1229 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 1230 | mHandler.runWithScissors(new Runnable() { |
| 1231 | @Override |
| 1232 | public void run() { |
| 1233 | dumpLocal(pw); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1234 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 1235 | }, 1000); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | private void dumpLocal(PrintWriter pw) { |
| 1239 | pw.println(); |
| 1240 | pw.println("Display Controller Thread State:"); |
| 1241 | pw.println(" mPowerRequest=" + mPowerRequest); |
| 1242 | pw.println(" mWaitingForNegativeProximity=" + mWaitingForNegativeProximity); |
| 1243 | |
| 1244 | pw.println(" mProximitySensor=" + mProximitySensor); |
| 1245 | pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled); |
| 1246 | pw.println(" mProximityThreshold=" + mProximityThreshold); |
| 1247 | pw.println(" mProximity=" + proximityToString(mProximity)); |
| 1248 | pw.println(" mPendingProximity=" + proximityToString(mPendingProximity)); |
| 1249 | pw.println(" mPendingProximityDebounceTime=" |
| 1250 | + TimeUtils.formatUptime(mPendingProximityDebounceTime)); |
| 1251 | pw.println(" mScreenOffBecauseOfProximity=" + mScreenOffBecauseOfProximity); |
| 1252 | |
| 1253 | pw.println(" mLightSensor=" + mLightSensor); |
| 1254 | pw.println(" mLightSensorEnabled=" + mLightSensorEnabled); |
| 1255 | pw.println(" mLightSensorEnableTime=" |
| 1256 | + TimeUtils.formatUptime(mLightSensorEnableTime)); |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1257 | pw.println(" mAmbientLux=" + mAmbientLux); |
| 1258 | pw.println(" mAmbientLuxValid=" + mAmbientLuxValid); |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1259 | pw.println(" mLastObservedLux=" + mLastObservedLux); |
| 1260 | pw.println(" mLastObservedLuxTime=" |
| 1261 | + TimeUtils.formatUptime(mLastObservedLuxTime)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1262 | pw.println(" mRecentLightSamples=" + mRecentLightSamples); |
Jeff Brown | 4f0e969 | 2012-10-18 16:14:16 -0700 | [diff] [blame] | 1263 | pw.println(" mRecentShortTermAverageLux=" + mRecentShortTermAverageLux); |
| 1264 | pw.println(" mRecentLongTermAverageLux=" + mRecentLongTermAverageLux); |
Jeff Brown | e941b1e | 2012-10-22 16:50:25 -0700 | [diff] [blame] | 1265 | pw.println(" mDebounceLuxDirection=" + mDebounceLuxDirection); |
| 1266 | pw.println(" mDebounceLuxTime=" + TimeUtils.formatUptime(mDebounceLuxTime)); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1267 | pw.println(" mScreenAutoBrightness=" + mScreenAutoBrightness); |
| 1268 | pw.println(" mUsingScreenAutoBrightness=" + mUsingScreenAutoBrightness); |
Jeff Brown | 330560f | 2012-08-21 22:10:57 -0700 | [diff] [blame] | 1269 | pw.println(" mLastScreenAutoBrightnessGamma=" + mLastScreenAutoBrightnessGamma); |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 1270 | pw.println(" mTwilight.getCurrentState()=" + mTwilight.getCurrentState()); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1271 | |
| 1272 | if (mElectronBeamOnAnimator != null) { |
| 1273 | pw.println(" mElectronBeamOnAnimator.isStarted()=" + |
| 1274 | mElectronBeamOnAnimator.isStarted()); |
| 1275 | } |
| 1276 | if (mElectronBeamOffAnimator != null) { |
| 1277 | pw.println(" mElectronBeamOffAnimator.isStarted()=" + |
| 1278 | mElectronBeamOffAnimator.isStarted()); |
| 1279 | } |
| 1280 | |
| 1281 | if (mPowerState != null) { |
| 1282 | mPowerState.dump(pw); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | private static String proximityToString(int state) { |
| 1287 | switch (state) { |
| 1288 | case PROXIMITY_UNKNOWN: |
| 1289 | return "Unknown"; |
| 1290 | case PROXIMITY_NEGATIVE: |
| 1291 | return "Negative"; |
| 1292 | case PROXIMITY_POSITIVE: |
| 1293 | return "Positive"; |
| 1294 | default: |
| 1295 | return Integer.toString(state); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | private static boolean wantScreenOn(int state) { |
| 1300 | switch (state) { |
| 1301 | case DisplayPowerRequest.SCREEN_STATE_BRIGHT: |
| 1302 | case DisplayPowerRequest.SCREEN_STATE_DIM: |
| 1303 | return true; |
| 1304 | } |
| 1305 | return false; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * Asynchronous callbacks from the power controller to the power manager service. |
| 1310 | */ |
| 1311 | public interface Callbacks { |
| 1312 | void onStateChanged(); |
Jeff Brown | 93cbbb2 | 2012-10-04 13:18:36 -0700 | [diff] [blame] | 1313 | void onProximityPositive(); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1314 | void onProximityNegative(); |
| 1315 | } |
| 1316 | |
| 1317 | private final class DisplayControllerHandler extends Handler { |
| 1318 | public DisplayControllerHandler(Looper looper) { |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 1319 | super(looper, null, true /*async*/); |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | @Override |
| 1323 | public void handleMessage(Message msg) { |
| 1324 | switch (msg.what) { |
| 1325 | case MSG_UPDATE_POWER_STATE: |
| 1326 | updatePowerState(); |
| 1327 | break; |
| 1328 | |
| 1329 | case MSG_PROXIMITY_SENSOR_DEBOUNCED: |
| 1330 | debounceProximitySensor(); |
| 1331 | break; |
| 1332 | |
| 1333 | case MSG_LIGHT_SENSOR_DEBOUNCED: |
| 1334 | debounceLightSensor(); |
| 1335 | break; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | private final SensorEventListener mProximitySensorListener = new SensorEventListener() { |
| 1341 | @Override |
| 1342 | public void onSensorChanged(SensorEvent event) { |
| 1343 | if (mProximitySensorEnabled) { |
| 1344 | final long time = SystemClock.uptimeMillis(); |
| 1345 | final float distance = event.values[0]; |
| 1346 | boolean positive = distance >= 0.0f && distance < mProximityThreshold; |
| 1347 | handleProximitySensorEvent(time, positive); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | @Override |
| 1352 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 1353 | // Not used. |
| 1354 | } |
| 1355 | }; |
| 1356 | |
| 1357 | private final SensorEventListener mLightSensorListener = new SensorEventListener() { |
| 1358 | @Override |
| 1359 | public void onSensorChanged(SensorEvent event) { |
| 1360 | if (mLightSensorEnabled) { |
| 1361 | final long time = SystemClock.uptimeMillis(); |
| 1362 | final float lux = event.values[0]; |
| 1363 | handleLightSensorEvent(time, lux); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | @Override |
| 1368 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 1369 | // Not used. |
| 1370 | } |
| 1371 | }; |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 1372 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1373 | private final TwilightListener mTwilightListener = new TwilightListener() { |
Jeff Brown | aa202a6 | 2012-08-21 22:14:26 -0700 | [diff] [blame] | 1374 | @Override |
| 1375 | public void onTwilightStateChanged() { |
| 1376 | mTwilightChanged = true; |
| 1377 | updatePowerState(); |
| 1378 | } |
| 1379 | }; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1380 | } |