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