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