Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 18 | |
Justin Klaassen | 908b86c | 2016-08-08 09:18:42 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 20 | import android.hardware.Sensor; |
| 21 | import android.hardware.SensorEvent; |
| 22 | import android.hardware.SensorEventListener; |
| 23 | import android.hardware.SensorManager; |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 24 | import android.hardware.display.BrightnessConfiguration; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 25 | import android.hardware.display.DisplayManagerInternal.DisplayPowerRequest; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.Looper; |
| 28 | import android.os.Message; |
| 29 | import android.os.PowerManager; |
| 30 | import android.os.SystemClock; |
Michael Wright | b0a1d3d | 2017-09-22 15:05:02 +0100 | [diff] [blame] | 31 | import android.os.Trace; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 32 | import android.util.EventLog; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 33 | import android.util.MathUtils; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 34 | import android.util.Slog; |
| 35 | import android.util.TimeUtils; |
| 36 | |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 37 | import com.android.server.EventLogTags; |
| 38 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 39 | import java.io.PrintWriter; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 40 | |
| 41 | class AutomaticBrightnessController { |
| 42 | private static final String TAG = "AutomaticBrightnessController"; |
| 43 | |
| 44 | private static final boolean DEBUG = false; |
| 45 | private static final boolean DEBUG_PRETEND_LIGHT_SENSOR_ABSENT = false; |
| 46 | |
| 47 | // If true, enables the use of the screen auto-brightness adjustment setting. |
Adrian Roos | daf7d41 | 2014-05-13 14:55:09 +0200 | [diff] [blame] | 48 | private static final boolean USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT = true; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 49 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 50 | // How long the current sensor reading is assumed to be valid beyond the current time. |
| 51 | // This provides a bit of prediction, as well as ensures that the weight for the last sample is |
| 52 | // non-zero, which in turn ensures that the total weight is non-zero. |
| 53 | private static final long AMBIENT_LIGHT_PREDICTION_TIME_MILLIS = 100; |
| 54 | |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 55 | // Debounce for sampling user-initiated changes in display brightness to ensure |
| 56 | // the user is satisfied with the result before storing the sample. |
| 57 | private static final int BRIGHTNESS_ADJUSTMENT_SAMPLE_DEBOUNCE_MILLIS = 10000; |
| 58 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 59 | // Timeout after which we remove the effects any user interactions might've had on the |
| 60 | // brightness mapping. This timeout doesn't start until we transition to a non-interactive |
| 61 | // display policy so that we don't reset while users are using their devices, but also so that |
| 62 | // we don't erroneously keep the short-term model if the device is dozing but the display is |
| 63 | // fully on. |
| 64 | private static final int SHORT_TERM_MODEL_TIMEOUT_MILLIS = 30000; |
| 65 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 66 | private static final int MSG_UPDATE_AMBIENT_LUX = 1; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 67 | private static final int MSG_BRIGHTNESS_ADJUSTMENT_SAMPLE = 2; |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 68 | private static final int MSG_INVALIDATE_SHORT_TERM_MODEL = 3; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 69 | |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 70 | // Length of the ambient light horizon used to calculate the long term estimate of ambient |
| 71 | // light. |
| 72 | private static final int AMBIENT_LIGHT_LONG_HORIZON_MILLIS = 10000; |
| 73 | |
| 74 | // Length of the ambient light horizon used to calculate short-term estimate of ambient light. |
| 75 | private static final int AMBIENT_LIGHT_SHORT_HORIZON_MILLIS = 2000; |
| 76 | |
Julius D'souza | 428aed0 | 2016-08-07 19:08:30 -0700 | [diff] [blame] | 77 | // Callbacks for requesting updates to the display's power state |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 78 | private final Callbacks mCallbacks; |
| 79 | |
| 80 | // The sensor manager. |
| 81 | private final SensorManager mSensorManager; |
| 82 | |
| 83 | // The light sensor, or null if not available or needed. |
| 84 | private final Sensor mLightSensor; |
| 85 | |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 86 | // The mapper to translate ambient lux to screen brightness in the range [0, 1.0]. |
| 87 | private final BrightnessMappingStrategy mBrightnessMapper; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 88 | |
| 89 | // The minimum and maximum screen brightnesses. |
| 90 | private final int mScreenBrightnessRangeMinimum; |
| 91 | private final int mScreenBrightnessRangeMaximum; |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 92 | |
| 93 | // How much to scale doze brightness by (should be (0, 1.0]). |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 94 | private final float mDozeScaleFactor; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 95 | |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 96 | // Initial light sensor event rate in milliseconds. |
| 97 | private final int mInitialLightSensorRate; |
| 98 | |
| 99 | // Steady-state light sensor event rate in milliseconds. |
| 100 | private final int mNormalLightSensorRate; |
| 101 | |
| 102 | // The current light sensor event rate in milliseconds. |
| 103 | private int mCurrentLightSensorRate; |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 104 | |
| 105 | // Stability requirements in milliseconds for accepting a new brightness level. This is used |
| 106 | // for debouncing the light sensor. Different constants are used to debounce the light sensor |
| 107 | // when adapting to brighter or darker environments. This parameter controls how quickly |
| 108 | // brightness changes occur in response to an observed change in light level that exceeds the |
| 109 | // hysteresis threshold. |
| 110 | private final long mBrighteningLightDebounceConfig; |
| 111 | private final long mDarkeningLightDebounceConfig; |
| 112 | |
| 113 | // If true immediately after the screen is turned on the controller will try to adjust the |
| 114 | // brightness based on the current sensor reads. If false, the controller will collect more data |
| 115 | // and only then decide whether to change brightness. |
| 116 | private final boolean mResetAmbientLuxAfterWarmUpConfig; |
| 117 | |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 118 | // Period of time in which to consider light samples in milliseconds. |
| 119 | private final int mAmbientLightHorizon; |
| 120 | |
| 121 | // The intercept used for the weighting calculation. This is used in order to keep all possible |
| 122 | // weighting values positive. |
| 123 | private final int mWeightingIntercept; |
| 124 | |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 125 | // Configuration object for determining thresholds to change brightness dynamically |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 126 | private final HysteresisLevels mAmbientBrightnessThresholds; |
| 127 | private final HysteresisLevels mScreenBrightnessThresholds; |
Julius D'souza | 428aed0 | 2016-08-07 19:08:30 -0700 | [diff] [blame] | 128 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 129 | // Amount of time to delay auto-brightness after screen on while waiting for |
| 130 | // the light sensor to warm-up in milliseconds. |
| 131 | // May be 0 if no warm-up is required. |
| 132 | private int mLightSensorWarmUpTimeConfig; |
| 133 | |
| 134 | // Set to true if the light sensor is enabled. |
| 135 | private boolean mLightSensorEnabled; |
| 136 | |
| 137 | // The time when the light sensor was enabled. |
| 138 | private long mLightSensorEnableTime; |
| 139 | |
| 140 | // The currently accepted nominal ambient light level. |
| 141 | private float mAmbientLux; |
| 142 | |
| 143 | // True if mAmbientLux holds a valid value. |
| 144 | private boolean mAmbientLuxValid; |
| 145 | |
| 146 | // The ambient light level threshold at which to brighten or darken the screen. |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 147 | private float mAmbientBrighteningThreshold; |
| 148 | private float mAmbientDarkeningThreshold; |
| 149 | |
| 150 | // The screen light level threshold at which to brighten or darken the screen. |
| 151 | private float mScreenBrighteningThreshold; |
| 152 | private float mScreenDarkeningThreshold; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 153 | |
| 154 | // The most recent light sample. |
| 155 | private float mLastObservedLux; |
| 156 | |
| 157 | // The time of the most light recent sample. |
| 158 | private long mLastObservedLuxTime; |
| 159 | |
| 160 | // The number of light samples collected since the light sensor was enabled. |
| 161 | private int mRecentLightSamples; |
| 162 | |
| 163 | // A ring buffer containing all of the recent ambient light sensor readings. |
| 164 | private AmbientLightRingBuffer mAmbientLightRingBuffer; |
| 165 | |
| 166 | // The handler |
| 167 | private AutomaticBrightnessHandler mHandler; |
| 168 | |
| 169 | // The screen brightness level that has been chosen by the auto-brightness |
| 170 | // algorithm. The actual brightness should ramp towards this value. |
| 171 | // We preserve this value even when we stop using the light sensor so |
| 172 | // that we can quickly revert to the previous auto-brightness level |
| 173 | // while the light sensor warms up. |
| 174 | // Use -1 if there is no current auto-brightness value available. |
| 175 | private int mScreenAutoBrightness = -1; |
| 176 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 177 | // The current display policy. This is useful, for example, for knowing when we're dozing, |
| 178 | // where the light sensor may not be available. |
| 179 | private int mDisplayPolicy = DisplayPowerRequest.POLICY_OFF; |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 180 | |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 181 | // True if we are collecting a brightness adjustment sample, along with some data |
| 182 | // for the initial state of the sample. |
| 183 | private boolean mBrightnessAdjustmentSamplePending; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 184 | private float mBrightnessAdjustmentSampleOldLux; |
| 185 | private int mBrightnessAdjustmentSampleOldBrightness; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 186 | |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 187 | // When the short term model is invalidated, we don't necessarily reset it (i.e. clear the |
| 188 | // user's adjustment) immediately, but wait for a drastic enough change in the ambient light. |
| 189 | // The anchor determines what were the light levels when the user has set her preference, and |
| 190 | // we use a relative threshold to determine when to revert to the OEM curve. |
| 191 | private boolean mShortTermModelValid; |
| 192 | private float mShortTermModelAnchor; |
| 193 | private float SHORT_TERM_MODEL_THRESHOLD_RATIO = 0.6f; |
| 194 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 195 | public AutomaticBrightnessController(Callbacks callbacks, Looper looper, |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 196 | SensorManager sensorManager, BrightnessMappingStrategy mapper, |
| 197 | int lightSensorWarmUpTime, int brightnessMin, int brightnessMax, float dozeScaleFactor, |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 198 | int lightSensorRate, int initialLightSensorRate, long brighteningLightDebounceConfig, |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 199 | long darkeningLightDebounceConfig, boolean resetAmbientLuxAfterWarmUpConfig, |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 200 | HysteresisLevels ambientBrightnessThresholds, |
| 201 | HysteresisLevels screenBrightnessThresholds) { |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 202 | mCallbacks = callbacks; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 203 | mSensorManager = sensorManager; |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 204 | mBrightnessMapper = mapper; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 205 | mScreenBrightnessRangeMinimum = brightnessMin; |
| 206 | mScreenBrightnessRangeMaximum = brightnessMax; |
| 207 | mLightSensorWarmUpTimeConfig = lightSensorWarmUpTime; |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 208 | mDozeScaleFactor = dozeScaleFactor; |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 209 | mNormalLightSensorRate = lightSensorRate; |
| 210 | mInitialLightSensorRate = initialLightSensorRate; |
| 211 | mCurrentLightSensorRate = -1; |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 212 | mBrighteningLightDebounceConfig = brighteningLightDebounceConfig; |
| 213 | mDarkeningLightDebounceConfig = darkeningLightDebounceConfig; |
| 214 | mResetAmbientLuxAfterWarmUpConfig = resetAmbientLuxAfterWarmUpConfig; |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 215 | mAmbientLightHorizon = AMBIENT_LIGHT_LONG_HORIZON_MILLIS; |
| 216 | mWeightingIntercept = AMBIENT_LIGHT_LONG_HORIZON_MILLIS; |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 217 | mAmbientBrightnessThresholds = ambientBrightnessThresholds; |
| 218 | mScreenBrightnessThresholds = screenBrightnessThresholds; |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 219 | mShortTermModelValid = true; |
| 220 | mShortTermModelAnchor = -1; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 221 | |
| 222 | mHandler = new AutomaticBrightnessHandler(looper); |
Michael Wright | 103fb78 | 2016-04-22 01:03:09 -0400 | [diff] [blame] | 223 | mAmbientLightRingBuffer = |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 224 | new AmbientLightRingBuffer(mNormalLightSensorRate, mAmbientLightHorizon); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 225 | |
| 226 | if (!DEBUG_PRETEND_LIGHT_SENSOR_ABSENT) { |
| 227 | mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); |
| 228 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | public int getAutomaticScreenBrightness() { |
Dan Gittik | 5878442 | 2018-04-05 12:30:38 +0100 | [diff] [blame] | 232 | if (!mAmbientLuxValid) { |
| 233 | return -1; |
| 234 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 235 | if (mDisplayPolicy == DisplayPowerRequest.POLICY_DOZE) { |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 236 | return (int) (mScreenAutoBrightness * mDozeScaleFactor); |
| 237 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 238 | return mScreenAutoBrightness; |
| 239 | } |
| 240 | |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 241 | public float getAutomaticScreenBrightnessAdjustment() { |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 242 | return mBrightnessMapper.getAutoBrightnessAdjustment(); |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 245 | public void configure(boolean enable, @Nullable BrightnessConfiguration configuration, |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 246 | float brightness, boolean userChangedBrightness, float adjustment, |
| 247 | boolean userChangedAutoBrightnessAdjustment, int displayPolicy) { |
Filip Gruszczynski | a15aa7d | 2014-10-28 14:12:40 -0700 | [diff] [blame] | 248 | // While dozing, the application processor may be suspended which will prevent us from |
| 249 | // receiving new information from the light sensor. On some devices, we may be able to |
| 250 | // switch to a wake-up light sensor instead but for now we will simply disable the sensor |
| 251 | // and hold onto the last computed screen auto brightness. We save the dozing flag for |
| 252 | // debugging purposes. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 253 | boolean dozing = (displayPolicy == DisplayPowerRequest.POLICY_DOZE); |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 254 | boolean changed = setBrightnessConfiguration(configuration); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 255 | changed |= setDisplayPolicy(displayPolicy); |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 256 | if (userChangedAutoBrightnessAdjustment) { |
| 257 | changed |= setAutoBrightnessAdjustment(adjustment); |
| 258 | } |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 259 | if (userChangedBrightness && enable) { |
| 260 | // Update the brightness curve with the new user control point. It's critical this |
| 261 | // happens after we update the autobrightness adjustment since it may reset it. |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 262 | changed |= setScreenBrightnessByUser(brightness); |
Michael Wright | 617564f | 2018-01-25 22:20:54 +0000 | [diff] [blame] | 263 | } |
| 264 | final boolean userInitiatedChange = |
| 265 | userChangedBrightness || userChangedAutoBrightnessAdjustment; |
| 266 | if (userInitiatedChange && enable && !dozing) { |
Soroosh Mariooryad | a8edffd | 2017-03-22 17:08:26 -0700 | [diff] [blame] | 267 | prepareBrightnessAdjustmentSample(); |
| 268 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 269 | changed |= setLightSensorEnabled(enable && !dozing); |
Jeff Brown | 970d413 | 2014-07-19 11:33:47 -0700 | [diff] [blame] | 270 | if (changed) { |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 271 | updateAutoBrightness(false /*sendUpdate*/); |
| 272 | } |
| 273 | } |
| 274 | |
Kenny Guy | 53d0661 | 2018-01-30 14:19:13 +0000 | [diff] [blame] | 275 | public boolean hasUserDataPoints() { |
| 276 | return mBrightnessMapper.hasUserDataPoints(); |
| 277 | } |
| 278 | |
| 279 | public boolean isDefaultConfig() { |
| 280 | return mBrightnessMapper.isDefaultConfig(); |
| 281 | } |
| 282 | |
Kenny Guy | 6d1009f | 2018-03-14 14:28:23 +0000 | [diff] [blame] | 283 | public BrightnessConfiguration getDefaultConfig() { |
| 284 | return mBrightnessMapper.getDefaultConfig(); |
| 285 | } |
| 286 | |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 287 | private boolean setDisplayPolicy(int policy) { |
| 288 | if (mDisplayPolicy == policy) { |
| 289 | return false; |
| 290 | } |
| 291 | final int oldPolicy = mDisplayPolicy; |
| 292 | mDisplayPolicy = policy; |
| 293 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 294 | Slog.d(TAG, "Display policy transitioning from " + oldPolicy + " to " + policy); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 295 | } |
| 296 | if (!isInteractivePolicy(policy) && isInteractivePolicy(oldPolicy)) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 297 | mHandler.sendEmptyMessageDelayed(MSG_INVALIDATE_SHORT_TERM_MODEL, |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 298 | SHORT_TERM_MODEL_TIMEOUT_MILLIS); |
| 299 | } else if (isInteractivePolicy(policy) && !isInteractivePolicy(oldPolicy)) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 300 | mHandler.removeMessages(MSG_INVALIDATE_SHORT_TERM_MODEL); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 301 | } |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | private static boolean isInteractivePolicy(int policy) { |
| 306 | return policy == DisplayPowerRequest.POLICY_BRIGHT |
| 307 | || policy == DisplayPowerRequest.POLICY_DIM |
| 308 | || policy == DisplayPowerRequest.POLICY_VR; |
| 309 | } |
| 310 | |
| 311 | private boolean setScreenBrightnessByUser(float brightness) { |
| 312 | if (!mAmbientLuxValid) { |
| 313 | // If we don't have a valid ambient lux then we don't have a valid brightness anyways, |
| 314 | // and we can't use this data to add a new control point to the short-term model. |
| 315 | return false; |
| 316 | } |
| 317 | mBrightnessMapper.addUserDataPoint(mAmbientLux, brightness); |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 318 | mShortTermModelValid = true; |
| 319 | mShortTermModelAnchor = mAmbientLux; |
| 320 | if (DEBUG) { |
| 321 | Slog.d(TAG, "ShortTermModel: anchor=" + mShortTermModelAnchor); |
| 322 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 323 | return true; |
| 324 | } |
| 325 | |
Dan Gittik | c135225 | 2018-04-27 17:48:31 +0100 | [diff] [blame] | 326 | public void resetShortTermModel() { |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 327 | mBrightnessMapper.clearUserDataPoints(); |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 328 | mShortTermModelValid = true; |
| 329 | mShortTermModelAnchor = -1; |
| 330 | } |
| 331 | |
| 332 | private void invalidateShortTermModel() { |
| 333 | if (DEBUG) { |
| 334 | Slog.d(TAG, "ShortTermModel: invalidate user data"); |
| 335 | } |
| 336 | mShortTermModelValid = false; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 339 | public boolean setBrightnessConfiguration(BrightnessConfiguration configuration) { |
Michael Wright | b8f4f77 | 2018-05-01 18:28:58 +0100 | [diff] [blame] | 340 | if (mBrightnessMapper.setBrightnessConfiguration(configuration)) { |
| 341 | resetShortTermModel(); |
| 342 | return true; |
| 343 | } |
| 344 | return false; |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 347 | public void dump(PrintWriter pw) { |
| 348 | pw.println(); |
| 349 | pw.println("Automatic Brightness Controller Configuration:"); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 350 | pw.println(" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum); |
| 351 | pw.println(" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum); |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 352 | pw.println(" mDozeScaleFactor=" + mDozeScaleFactor); |
| 353 | pw.println(" mInitialLightSensorRate=" + mInitialLightSensorRate); |
| 354 | pw.println(" mNormalLightSensorRate=" + mNormalLightSensorRate); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 355 | pw.println(" mLightSensorWarmUpTimeConfig=" + mLightSensorWarmUpTimeConfig); |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 356 | pw.println(" mBrighteningLightDebounceConfig=" + mBrighteningLightDebounceConfig); |
| 357 | pw.println(" mDarkeningLightDebounceConfig=" + mDarkeningLightDebounceConfig); |
| 358 | pw.println(" mResetAmbientLuxAfterWarmUpConfig=" + mResetAmbientLuxAfterWarmUpConfig); |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 359 | pw.println(" mAmbientLightHorizon=" + mAmbientLightHorizon); |
| 360 | pw.println(" mWeightingIntercept=" + mWeightingIntercept); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 361 | |
| 362 | pw.println(); |
| 363 | pw.println("Automatic Brightness Controller State:"); |
| 364 | pw.println(" mLightSensor=" + mLightSensor); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 365 | pw.println(" mLightSensorEnabled=" + mLightSensorEnabled); |
| 366 | pw.println(" mLightSensorEnableTime=" + TimeUtils.formatUptime(mLightSensorEnableTime)); |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 367 | pw.println(" mCurrentLightSensorRate=" + mCurrentLightSensorRate); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 368 | pw.println(" mAmbientLux=" + mAmbientLux); |
Dan Gittik | 5878442 | 2018-04-05 12:30:38 +0100 | [diff] [blame] | 369 | pw.println(" mAmbientLuxValid=" + mAmbientLuxValid); |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 370 | pw.println(" mAmbientBrighteningThreshold=" + mAmbientBrighteningThreshold); |
| 371 | pw.println(" mAmbientDarkeningThreshold=" + mAmbientDarkeningThreshold); |
| 372 | pw.println(" mScreenBrighteningThreshold=" + mScreenBrighteningThreshold); |
| 373 | pw.println(" mScreenDarkeningThreshold=" + mScreenDarkeningThreshold); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 374 | pw.println(" mLastObservedLux=" + mLastObservedLux); |
| 375 | pw.println(" mLastObservedLuxTime=" + TimeUtils.formatUptime(mLastObservedLuxTime)); |
| 376 | pw.println(" mRecentLightSamples=" + mRecentLightSamples); |
| 377 | pw.println(" mAmbientLightRingBuffer=" + mAmbientLightRingBuffer); |
| 378 | pw.println(" mScreenAutoBrightness=" + mScreenAutoBrightness); |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 379 | pw.println(" mDisplayPolicy=" + DisplayPowerRequest.policyToString(mDisplayPolicy)); |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 380 | pw.println(" mShortTermModelAnchor=" + mShortTermModelAnchor); |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 381 | pw.println(" mShortTermModelValid=" + mShortTermModelValid); |
| 382 | pw.println(" mBrightnessAdjustmentSamplePending=" + mBrightnessAdjustmentSamplePending); |
| 383 | pw.println(" mBrightnessAdjustmentSampleOldLux=" + mBrightnessAdjustmentSampleOldLux); |
| 384 | pw.println(" mBrightnessAdjustmentSampleOldBrightness=" |
| 385 | + mBrightnessAdjustmentSampleOldBrightness); |
| 386 | pw.println(" mShortTermModelValid=" + mShortTermModelValid); |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 387 | |
| 388 | pw.println(); |
| 389 | mBrightnessMapper.dump(pw); |
Michael Wright | 2155bc2 | 2018-05-01 00:38:32 +0100 | [diff] [blame] | 390 | |
| 391 | pw.println(); |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 392 | mAmbientBrightnessThresholds.dump(pw); |
| 393 | mScreenBrightnessThresholds.dump(pw); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | private boolean setLightSensorEnabled(boolean enable) { |
| 397 | if (enable) { |
| 398 | if (!mLightSensorEnabled) { |
| 399 | mLightSensorEnabled = true; |
| 400 | mLightSensorEnableTime = SystemClock.uptimeMillis(); |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 401 | mCurrentLightSensorRate = mInitialLightSensorRate; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 402 | mSensorManager.registerListener(mLightSensorListener, mLightSensor, |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 403 | mCurrentLightSensorRate * 1000, mHandler); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 404 | return true; |
| 405 | } |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 406 | } else if (mLightSensorEnabled) { |
| 407 | mLightSensorEnabled = false; |
| 408 | mAmbientLuxValid = !mResetAmbientLuxAfterWarmUpConfig; |
| 409 | mRecentLightSamples = 0; |
| 410 | mAmbientLightRingBuffer.clear(); |
| 411 | mCurrentLightSensorRate = -1; |
| 412 | mHandler.removeMessages(MSG_UPDATE_AMBIENT_LUX); |
| 413 | mSensorManager.unregisterListener(mLightSensorListener); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | private void handleLightSensorEvent(long time, float lux) { |
Michael Wright | b0a1d3d | 2017-09-22 15:05:02 +0100 | [diff] [blame] | 419 | Trace.traceCounter(Trace.TRACE_TAG_POWER, "ALS", (int) lux); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 420 | mHandler.removeMessages(MSG_UPDATE_AMBIENT_LUX); |
| 421 | |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 422 | if (mAmbientLightRingBuffer.size() == 0) { |
| 423 | // switch to using the steady-state sample rate after grabbing the initial light sample |
| 424 | adjustLightSensorRate(mNormalLightSensorRate); |
| 425 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 426 | applyLightSensorMeasurement(time, lux); |
| 427 | updateAmbientLux(time); |
| 428 | } |
| 429 | |
| 430 | private void applyLightSensorMeasurement(long time, float lux) { |
| 431 | mRecentLightSamples++; |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 432 | mAmbientLightRingBuffer.prune(time - mAmbientLightHorizon); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 433 | mAmbientLightRingBuffer.push(time, lux); |
| 434 | |
| 435 | // Remember this sample value. |
| 436 | mLastObservedLux = lux; |
| 437 | mLastObservedLuxTime = time; |
| 438 | } |
| 439 | |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 440 | private void adjustLightSensorRate(int lightSensorRate) { |
| 441 | // if the light sensor rate changed, update the sensor listener |
| 442 | if (lightSensorRate != mCurrentLightSensorRate) { |
| 443 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 444 | Slog.d(TAG, "adjustLightSensorRate: " + |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 445 | "previousRate=" + mCurrentLightSensorRate + ", " + |
| 446 | "currentRate=" + lightSensorRate); |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 447 | } |
| 448 | mCurrentLightSensorRate = lightSensorRate; |
| 449 | mSensorManager.unregisterListener(mLightSensorListener); |
| 450 | mSensorManager.registerListener(mLightSensorListener, mLightSensor, |
| 451 | lightSensorRate * 1000, mHandler); |
| 452 | } |
| 453 | } |
| 454 | |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 455 | private boolean setAutoBrightnessAdjustment(float adjustment) { |
| 456 | return mBrightnessMapper.setAutoBrightnessAdjustment(adjustment); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | private void setAmbientLux(float lux) { |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 460 | if (DEBUG) { |
| 461 | Slog.d(TAG, "setAmbientLux(" + lux + ")"); |
| 462 | } |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 463 | if (lux < 0) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 464 | Slog.w(TAG, "Ambient lux was negative, ignoring and setting to 0"); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 465 | lux = 0; |
| 466 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 467 | mAmbientLux = lux; |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 468 | mAmbientBrighteningThreshold = mAmbientBrightnessThresholds.getBrighteningThreshold(lux); |
| 469 | mAmbientDarkeningThreshold = mAmbientBrightnessThresholds.getDarkeningThreshold(lux); |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 470 | |
| 471 | // If the short term model was invalidated and the change is drastic enough, reset it. |
| 472 | if (!mShortTermModelValid && mShortTermModelAnchor != -1) { |
| 473 | final float minAmbientLux = |
| 474 | mShortTermModelAnchor - mShortTermModelAnchor * SHORT_TERM_MODEL_THRESHOLD_RATIO; |
| 475 | final float maxAmbientLux = |
| 476 | mShortTermModelAnchor + mShortTermModelAnchor * SHORT_TERM_MODEL_THRESHOLD_RATIO; |
| 477 | if (minAmbientLux < mAmbientLux && mAmbientLux < maxAmbientLux) { |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 478 | if (DEBUG) { |
| 479 | Slog.d(TAG, "ShortTermModel: re-validate user data, ambient lux is " + |
| 480 | minAmbientLux + " < " + mAmbientLux + " < " + maxAmbientLux); |
| 481 | } |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 482 | mShortTermModelValid = true; |
| 483 | } else { |
| 484 | Slog.d(TAG, "ShortTermModel: reset data, ambient lux is " + mAmbientLux + |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 485 | "(" + minAmbientLux + ", " + maxAmbientLux + ")"); |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 486 | resetShortTermModel(); |
| 487 | } |
| 488 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 491 | private float calculateAmbientLux(long now, long horizon) { |
| 492 | if (DEBUG) { |
| 493 | Slog.d(TAG, "calculateAmbientLux(" + now + ", " + horizon + ")"); |
| 494 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 495 | final int N = mAmbientLightRingBuffer.size(); |
| 496 | if (N == 0) { |
| 497 | Slog.e(TAG, "calculateAmbientLux: No ambient light readings available"); |
| 498 | return -1; |
| 499 | } |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 500 | |
| 501 | // Find the first measurement that is just outside of the horizon. |
| 502 | int endIndex = 0; |
| 503 | final long horizonStartTime = now - horizon; |
| 504 | for (int i = 0; i < N-1; i++) { |
| 505 | if (mAmbientLightRingBuffer.getTime(i + 1) <= horizonStartTime) { |
| 506 | endIndex++; |
| 507 | } else { |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | if (DEBUG) { |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 512 | Slog.d(TAG, "calculateAmbientLux: selected endIndex=" + endIndex + ", point=(" + |
| 513 | mAmbientLightRingBuffer.getTime(endIndex) + ", " + |
| 514 | mAmbientLightRingBuffer.getLux(endIndex) + ")"); |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 515 | } |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 516 | float sum = 0; |
| 517 | float totalWeight = 0; |
| 518 | long endTime = AMBIENT_LIGHT_PREDICTION_TIME_MILLIS; |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 519 | for (int i = N - 1; i >= endIndex; i--) { |
| 520 | long eventTime = mAmbientLightRingBuffer.getTime(i); |
| 521 | if (i == endIndex && eventTime < horizonStartTime) { |
| 522 | // If we're at the final value, make sure we only consider the part of the sample |
| 523 | // within our desired horizon. |
| 524 | eventTime = horizonStartTime; |
| 525 | } |
| 526 | final long startTime = eventTime - now; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 527 | float weight = calculateWeight(startTime, endTime); |
| 528 | float lux = mAmbientLightRingBuffer.getLux(i); |
| 529 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 530 | Slog.d(TAG, "calculateAmbientLux: [" + startTime + ", " + endTime + "]: " + |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 531 | "lux=" + lux + ", " + |
| 532 | "weight=" + weight); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 533 | } |
| 534 | totalWeight += weight; |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 535 | sum += lux * weight; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 536 | endTime = startTime; |
| 537 | } |
| 538 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 539 | Slog.d(TAG, "calculateAmbientLux: " + |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 540 | "totalWeight=" + totalWeight + ", " + |
| 541 | "newAmbientLux=" + (sum / totalWeight)); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 542 | } |
| 543 | return sum / totalWeight; |
| 544 | } |
| 545 | |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 546 | private float calculateWeight(long startDelta, long endDelta) { |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 547 | return weightIntegral(endDelta) - weightIntegral(startDelta); |
| 548 | } |
| 549 | |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 550 | // Evaluates the integral of y = x + mWeightingIntercept. This is always positive for the |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 551 | // horizon we're looking at and provides a non-linear weighting for light samples. |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 552 | private float weightIntegral(long x) { |
| 553 | return x * (x * 0.5f + mWeightingIntercept); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | private long nextAmbientLightBrighteningTransition(long time) { |
| 557 | final int N = mAmbientLightRingBuffer.size(); |
| 558 | long earliestValidTime = time; |
| 559 | for (int i = N - 1; i >= 0; i--) { |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 560 | if (mAmbientLightRingBuffer.getLux(i) <= mAmbientBrighteningThreshold) { |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 561 | break; |
| 562 | } |
| 563 | earliestValidTime = mAmbientLightRingBuffer.getTime(i); |
| 564 | } |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 565 | return earliestValidTime + mBrighteningLightDebounceConfig; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | private long nextAmbientLightDarkeningTransition(long time) { |
| 569 | final int N = mAmbientLightRingBuffer.size(); |
| 570 | long earliestValidTime = time; |
| 571 | for (int i = N - 1; i >= 0; i--) { |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 572 | if (mAmbientLightRingBuffer.getLux(i) >= mAmbientDarkeningThreshold) { |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 573 | break; |
| 574 | } |
| 575 | earliestValidTime = mAmbientLightRingBuffer.getTime(i); |
| 576 | } |
Filip Gruszczynski | d81ecd1 | 2015-02-06 12:38:47 -0800 | [diff] [blame] | 577 | return earliestValidTime + mDarkeningLightDebounceConfig; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | private void updateAmbientLux() { |
| 581 | long time = SystemClock.uptimeMillis(); |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 582 | mAmbientLightRingBuffer.prune(time - mAmbientLightHorizon); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 583 | updateAmbientLux(time); |
| 584 | } |
| 585 | |
| 586 | private void updateAmbientLux(long time) { |
| 587 | // If the light sensor was just turned on then immediately update our initial |
| 588 | // estimate of the current ambient light level. |
| 589 | if (!mAmbientLuxValid) { |
| 590 | final long timeWhenSensorWarmedUp = |
| 591 | mLightSensorWarmUpTimeConfig + mLightSensorEnableTime; |
| 592 | if (time < timeWhenSensorWarmedUp) { |
| 593 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 594 | Slog.d(TAG, "updateAmbientLux: Sensor not ready yet: " + |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 595 | "time=" + time + ", " + |
| 596 | "timeWhenSensorWarmedUp=" + timeWhenSensorWarmedUp); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 597 | } |
| 598 | mHandler.sendEmptyMessageAtTime(MSG_UPDATE_AMBIENT_LUX, |
| 599 | timeWhenSensorWarmedUp); |
| 600 | return; |
| 601 | } |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 602 | setAmbientLux(calculateAmbientLux(time, AMBIENT_LIGHT_SHORT_HORIZON_MILLIS)); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 603 | mAmbientLuxValid = true; |
| 604 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 605 | Slog.d(TAG, "updateAmbientLux: Initializing: " + |
| 606 | "mAmbientLightRingBuffer=" + mAmbientLightRingBuffer + ", " + |
| 607 | "mAmbientLux=" + mAmbientLux); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 608 | } |
| 609 | updateAutoBrightness(true); |
| 610 | } |
| 611 | |
| 612 | long nextBrightenTransition = nextAmbientLightBrighteningTransition(time); |
| 613 | long nextDarkenTransition = nextAmbientLightDarkeningTransition(time); |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 614 | // Essentially, we calculate both a slow ambient lux, to ensure there's a true long-term |
| 615 | // change in lighting conditions, and a fast ambient lux to determine what the new |
| 616 | // brightness situation is since the slow lux can be quite slow to converge. |
| 617 | // |
| 618 | // Note that both values need to be checked for sufficient change before updating the |
| 619 | // proposed ambient light value since the slow value might be sufficiently far enough away |
| 620 | // from the fast value to cause a recalculation while its actually just converging on |
| 621 | // the fast value still. |
| 622 | float slowAmbientLux = calculateAmbientLux(time, AMBIENT_LIGHT_LONG_HORIZON_MILLIS); |
| 623 | float fastAmbientLux = calculateAmbientLux(time, AMBIENT_LIGHT_SHORT_HORIZON_MILLIS); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 624 | |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 625 | if ((slowAmbientLux >= mAmbientBrighteningThreshold |
| 626 | && fastAmbientLux >= mAmbientBrighteningThreshold |
| 627 | && nextBrightenTransition <= time) |
| 628 | || (slowAmbientLux <= mAmbientDarkeningThreshold |
| 629 | && fastAmbientLux <= mAmbientDarkeningThreshold |
| 630 | && nextDarkenTransition <= time)) { |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 631 | setAmbientLux(fastAmbientLux); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 632 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 633 | Slog.d(TAG, "updateAmbientLux: " + |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 634 | ((fastAmbientLux > mAmbientLux) ? "Brightened" : "Darkened") + ": " |
| 635 | + "mAmbientBrighteningThreshold=" + mAmbientBrighteningThreshold + ", " |
| 636 | + "mAmbientLightRingBuffer=" + mAmbientLightRingBuffer + ", " |
| 637 | + "mAmbientLux=" + mAmbientLux); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 638 | } |
| 639 | updateAutoBrightness(true); |
| 640 | nextBrightenTransition = nextAmbientLightBrighteningTransition(time); |
| 641 | nextDarkenTransition = nextAmbientLightDarkeningTransition(time); |
| 642 | } |
| 643 | long nextTransitionTime = Math.min(nextDarkenTransition, nextBrightenTransition); |
| 644 | // If one of the transitions is ready to occur, but the total weighted ambient lux doesn't |
| 645 | // exceed the necessary threshold, then it's possible we'll get a transition time prior to |
| 646 | // now. Rather than continually checking to see whether the weighted lux exceeds the |
| 647 | // threshold, schedule an update for when we'd normally expect another light sample, which |
| 648 | // should be enough time to decide whether we should actually transition to the new |
| 649 | // weighted ambient lux or not. |
| 650 | nextTransitionTime = |
Julius D'souza | 5d71709 | 2016-10-24 19:26:45 -0700 | [diff] [blame] | 651 | nextTransitionTime > time ? nextTransitionTime : time + mNormalLightSensorRate; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 652 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 653 | Slog.d(TAG, "updateAmbientLux: Scheduling ambient lux update for " + |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 654 | nextTransitionTime + TimeUtils.formatUptime(nextTransitionTime)); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 655 | } |
| 656 | mHandler.sendEmptyMessageAtTime(MSG_UPDATE_AMBIENT_LUX, nextTransitionTime); |
| 657 | } |
| 658 | |
| 659 | private void updateAutoBrightness(boolean sendUpdate) { |
| 660 | if (!mAmbientLuxValid) { |
| 661 | return; |
| 662 | } |
| 663 | |
Michael Wright | eef0e13 | 2017-11-21 17:57:52 +0000 | [diff] [blame] | 664 | float value = mBrightnessMapper.getBrightness(mAmbientLux); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 665 | |
| 666 | int newScreenAutoBrightness = |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 667 | clampScreenBrightness(Math.round(value * PowerManager.BRIGHTNESS_ON)); |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 668 | |
| 669 | // If mScreenAutoBrightness is set, we should have screen{Brightening,Darkening}Threshold, |
| 670 | // in which case we ignore the new screen brightness if it doesn't differ enough from the |
| 671 | // previous one. |
| 672 | if (mScreenAutoBrightness != -1 |
| 673 | && newScreenAutoBrightness > mScreenDarkeningThreshold |
| 674 | && newScreenAutoBrightness < mScreenBrighteningThreshold) { |
| 675 | if (DEBUG) { |
| 676 | Slog.d(TAG, "ignoring newScreenAutoBrightness: " + mScreenDarkeningThreshold |
| 677 | + " < " + newScreenAutoBrightness + " < " + mScreenBrighteningThreshold); |
| 678 | } |
| 679 | return; |
| 680 | } |
| 681 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 682 | if (mScreenAutoBrightness != newScreenAutoBrightness) { |
| 683 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 684 | Slog.d(TAG, "updateAutoBrightness: " + |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 685 | "mScreenAutoBrightness=" + mScreenAutoBrightness + ", " + |
| 686 | "newScreenAutoBrightness=" + newScreenAutoBrightness); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | mScreenAutoBrightness = newScreenAutoBrightness; |
Dan Gittik | fe87cf3 | 2019-01-30 17:31:34 +0000 | [diff] [blame] | 690 | mScreenBrighteningThreshold = |
| 691 | mScreenBrightnessThresholds.getBrighteningThreshold(newScreenAutoBrightness); |
| 692 | mScreenDarkeningThreshold = |
| 693 | mScreenBrightnessThresholds.getDarkeningThreshold(newScreenAutoBrightness); |
| 694 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 695 | if (sendUpdate) { |
| 696 | mCallbacks.updateBrightness(); |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | private int clampScreenBrightness(int value) { |
| 702 | return MathUtils.constrain(value, |
| 703 | mScreenBrightnessRangeMinimum, mScreenBrightnessRangeMaximum); |
| 704 | } |
| 705 | |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 706 | private void prepareBrightnessAdjustmentSample() { |
| 707 | if (!mBrightnessAdjustmentSamplePending) { |
| 708 | mBrightnessAdjustmentSamplePending = true; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 709 | mBrightnessAdjustmentSampleOldLux = mAmbientLuxValid ? mAmbientLux : -1; |
| 710 | mBrightnessAdjustmentSampleOldBrightness = mScreenAutoBrightness; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 711 | } else { |
| 712 | mHandler.removeMessages(MSG_BRIGHTNESS_ADJUSTMENT_SAMPLE); |
| 713 | } |
| 714 | |
| 715 | mHandler.sendEmptyMessageDelayed(MSG_BRIGHTNESS_ADJUSTMENT_SAMPLE, |
| 716 | BRIGHTNESS_ADJUSTMENT_SAMPLE_DEBOUNCE_MILLIS); |
| 717 | } |
| 718 | |
| 719 | private void cancelBrightnessAdjustmentSample() { |
| 720 | if (mBrightnessAdjustmentSamplePending) { |
| 721 | mBrightnessAdjustmentSamplePending = false; |
| 722 | mHandler.removeMessages(MSG_BRIGHTNESS_ADJUSTMENT_SAMPLE); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | private void collectBrightnessAdjustmentSample() { |
| 727 | if (mBrightnessAdjustmentSamplePending) { |
| 728 | mBrightnessAdjustmentSamplePending = false; |
| 729 | if (mAmbientLuxValid && mScreenAutoBrightness >= 0) { |
| 730 | if (DEBUG) { |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 731 | Slog.d(TAG, "Auto-brightness adjustment changed by user: " + |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 732 | "lux=" + mAmbientLux + ", " + |
| 733 | "brightness=" + mScreenAutoBrightness + ", " + |
| 734 | "ring=" + mAmbientLightRingBuffer); |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | EventLog.writeEvent(EventLogTags.AUTO_BRIGHTNESS_ADJ, |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 738 | mBrightnessAdjustmentSampleOldLux, |
| 739 | mBrightnessAdjustmentSampleOldBrightness, |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 740 | mAmbientLux, |
Dan Gittik | 57d6f11 | 2018-03-27 18:14:22 +0100 | [diff] [blame] | 741 | mScreenAutoBrightness); |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 746 | private final class AutomaticBrightnessHandler extends Handler { |
| 747 | public AutomaticBrightnessHandler(Looper looper) { |
| 748 | super(looper, null, true /*async*/); |
| 749 | } |
| 750 | |
| 751 | @Override |
| 752 | public void handleMessage(Message msg) { |
| 753 | switch (msg.what) { |
| 754 | case MSG_UPDATE_AMBIENT_LUX: |
| 755 | updateAmbientLux(); |
| 756 | break; |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 757 | |
| 758 | case MSG_BRIGHTNESS_ADJUSTMENT_SAMPLE: |
| 759 | collectBrightnessAdjustmentSample(); |
| 760 | break; |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 761 | |
Dan Gittik | c5d3291 | 2018-02-22 13:53:47 +0000 | [diff] [blame] | 762 | case MSG_INVALIDATE_SHORT_TERM_MODEL: |
| 763 | invalidateShortTermModel(); |
Michael Wright | d846023 | 2018-01-16 18:04:59 +0000 | [diff] [blame] | 764 | break; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | private final SensorEventListener mLightSensorListener = new SensorEventListener() { |
| 770 | @Override |
| 771 | public void onSensorChanged(SensorEvent event) { |
| 772 | if (mLightSensorEnabled) { |
| 773 | final long time = SystemClock.uptimeMillis(); |
| 774 | final float lux = event.values[0]; |
| 775 | handleLightSensorEvent(time, lux); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 781 | // Not used. |
| 782 | } |
| 783 | }; |
| 784 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 785 | /** Callbacks to request updates to the display's power state. */ |
| 786 | interface Callbacks { |
| 787 | void updateBrightness(); |
| 788 | } |
| 789 | |
Michael Wright | 0a93314 | 2017-08-16 20:38:21 +0100 | [diff] [blame] | 790 | /** |
| 791 | * A ring buffer of ambient light measurements sorted by time. |
| 792 | * |
| 793 | * Each entry consists of a timestamp and a lux measurement, and the overall buffer is sorted |
| 794 | * from oldest to newest. |
| 795 | */ |
Michael Wright | 103fb78 | 2016-04-22 01:03:09 -0400 | [diff] [blame] | 796 | private static final class AmbientLightRingBuffer { |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 797 | // Proportional extra capacity of the buffer beyond the expected number of light samples |
| 798 | // in the horizon |
| 799 | private static final float BUFFER_SLACK = 1.5f; |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 800 | private float[] mRingLux; |
| 801 | private long[] mRingTime; |
| 802 | private int mCapacity; |
| 803 | |
| 804 | // The first valid element and the next open slot. |
| 805 | // Note that if mCount is zero then there are no valid elements. |
| 806 | private int mStart; |
| 807 | private int mEnd; |
| 808 | private int mCount; |
| 809 | |
Zoran Jovanovic | 6fc42f5 | 2015-12-10 17:01:16 +0100 | [diff] [blame] | 810 | public AmbientLightRingBuffer(long lightSensorRate, int ambientLightHorizon) { |
| 811 | mCapacity = (int) Math.ceil(ambientLightHorizon * BUFFER_SLACK / lightSensorRate); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 812 | mRingLux = new float[mCapacity]; |
| 813 | mRingTime = new long[mCapacity]; |
| 814 | } |
| 815 | |
| 816 | public float getLux(int index) { |
| 817 | return mRingLux[offsetOf(index)]; |
| 818 | } |
| 819 | |
| 820 | public long getTime(int index) { |
| 821 | return mRingTime[offsetOf(index)]; |
| 822 | } |
| 823 | |
| 824 | public void push(long time, float lux) { |
| 825 | int next = mEnd; |
| 826 | if (mCount == mCapacity) { |
| 827 | int newSize = mCapacity * 2; |
| 828 | |
| 829 | float[] newRingLux = new float[newSize]; |
| 830 | long[] newRingTime = new long[newSize]; |
| 831 | int length = mCapacity - mStart; |
| 832 | System.arraycopy(mRingLux, mStart, newRingLux, 0, length); |
| 833 | System.arraycopy(mRingTime, mStart, newRingTime, 0, length); |
| 834 | if (mStart != 0) { |
| 835 | System.arraycopy(mRingLux, 0, newRingLux, length, mStart); |
| 836 | System.arraycopy(mRingTime, 0, newRingTime, length, mStart); |
| 837 | } |
| 838 | mRingLux = newRingLux; |
| 839 | mRingTime = newRingTime; |
| 840 | |
| 841 | next = mCapacity; |
| 842 | mCapacity = newSize; |
| 843 | mStart = 0; |
| 844 | } |
| 845 | mRingTime[next] = time; |
| 846 | mRingLux[next] = lux; |
| 847 | mEnd = next + 1; |
| 848 | if (mEnd == mCapacity) { |
| 849 | mEnd = 0; |
| 850 | } |
| 851 | mCount++; |
| 852 | } |
| 853 | |
| 854 | public void prune(long horizon) { |
| 855 | if (mCount == 0) { |
| 856 | return; |
| 857 | } |
| 858 | |
| 859 | while (mCount > 1) { |
| 860 | int next = mStart + 1; |
| 861 | if (next >= mCapacity) { |
| 862 | next -= mCapacity; |
| 863 | } |
| 864 | if (mRingTime[next] > horizon) { |
| 865 | // Some light sensors only produce data upon a change in the ambient light |
| 866 | // levels, so we need to consider the previous measurement as the ambient light |
| 867 | // level for all points in time up until we receive a new measurement. Thus, we |
| 868 | // always want to keep the youngest element that would be removed from the |
| 869 | // buffer and just set its measurement time to the horizon time since at that |
| 870 | // point it is the ambient light level, and to remove it would be to drop a |
| 871 | // valid data point within our horizon. |
| 872 | break; |
| 873 | } |
| 874 | mStart = next; |
| 875 | mCount -= 1; |
| 876 | } |
| 877 | |
| 878 | if (mRingTime[mStart] < horizon) { |
| 879 | mRingTime[mStart] = horizon; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | public int size() { |
| 884 | return mCount; |
| 885 | } |
| 886 | |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 887 | public void clear() { |
| 888 | mStart = 0; |
| 889 | mEnd = 0; |
| 890 | mCount = 0; |
| 891 | } |
| 892 | |
| 893 | @Override |
| 894 | public String toString() { |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 895 | StringBuffer buf = new StringBuffer(); |
| 896 | buf.append('['); |
| 897 | for (int i = 0; i < mCount; i++) { |
| 898 | final long next = i + 1 < mCount ? getTime(i + 1) : SystemClock.uptimeMillis(); |
| 899 | if (i != 0) { |
| 900 | buf.append(", "); |
| 901 | } |
| 902 | buf.append(getLux(i)); |
| 903 | buf.append(" / "); |
| 904 | buf.append(next - getTime(i)); |
| 905 | buf.append("ms"); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 906 | } |
Jeff Brown | a576b4d | 2015-04-23 19:58:06 -0700 | [diff] [blame] | 907 | buf.append(']'); |
| 908 | return buf.toString(); |
Michael Wright | 639c8be | 2014-01-17 18:29:12 -0800 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | private int offsetOf(int index) { |
| 912 | if (index >= mCount || index < 0) { |
| 913 | throw new ArrayIndexOutOfBoundsException(index); |
| 914 | } |
| 915 | index += mStart; |
| 916 | if (index >= mCapacity) { |
| 917 | index -= mCapacity; |
| 918 | } |
| 919 | return index; |
| 920 | } |
| 921 | } |
| 922 | } |