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