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