| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import com.android.internal.app.IBatteryStats; |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 20 | import com.android.internal.app.ShutdownThread; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import com.android.server.am.BatteryStatsService; |
| 22 | |
| 23 | import android.app.ActivityManagerNative; |
| 24 | import android.app.IActivityManager; |
| 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.ContentQueryMap; |
| 27 | import android.content.ContentResolver; |
| Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 28 | import android.content.ContentValues; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
| 32 | import android.content.pm.PackageManager; |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 33 | import android.content.res.Resources; |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 34 | import android.database.ContentObserver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.database.Cursor; |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 36 | import android.hardware.Sensor; |
| 37 | import android.hardware.SensorEvent; |
| 38 | import android.hardware.SensorEventListener; |
| 39 | import android.hardware.SensorManager; |
| Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 40 | import android.os.BatteryManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.BatteryStats; |
| 42 | import android.os.Binder; |
| 43 | import android.os.Handler; |
| 44 | import android.os.HandlerThread; |
| 45 | import android.os.IBinder; |
| 46 | import android.os.IPowerManager; |
| 47 | import android.os.LocalPowerManager; |
| 48 | import android.os.Power; |
| 49 | import android.os.PowerManager; |
| 50 | import android.os.Process; |
| 51 | import android.os.RemoteException; |
| 52 | import android.os.SystemClock; |
| Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 53 | import android.os.SystemProperties; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 54 | import android.os.WorkSource; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.provider.Settings; |
| 56 | import android.util.EventLog; |
| 57 | import android.util.Log; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 58 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.view.WindowManagerPolicy; |
| 60 | import static android.provider.Settings.System.DIM_SCREEN; |
| 61 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS; |
| Dan Murphy | 951764b | 2009-08-27 14:59:03 -0500 | [diff] [blame] | 62 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 63 | import static android.provider.Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ; |
| Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 64 | import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; |
| 66 | import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN; |
| Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 67 | import static android.provider.Settings.System.WINDOW_ANIMATION_SCALE; |
| 68 | import static android.provider.Settings.System.TRANSITION_ANIMATION_SCALE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| 70 | import java.io.FileDescriptor; |
| 71 | import java.io.PrintWriter; |
| 72 | import java.util.ArrayList; |
| 73 | import java.util.HashMap; |
| 74 | import java.util.Observable; |
| 75 | import java.util.Observer; |
| 76 | |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 77 | public class PowerManagerService extends IPowerManager.Stub |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 78 | implements LocalPowerManager, Watchdog.Monitor { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
| 80 | private static final String TAG = "PowerManagerService"; |
| 81 | static final String PARTIAL_NAME = "PowerManagerService"; |
| 82 | |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 83 | static final boolean DEBUG_SCREEN_ON = false; |
| 84 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | private static final boolean LOG_PARTIAL_WL = false; |
| 86 | |
| 87 | // Indicates whether touch-down cycles should be logged as part of the |
| 88 | // LOG_POWER_SCREEN_STATE log events |
| 89 | private static final boolean LOG_TOUCH_DOWNS = true; |
| 90 | |
| 91 | private static final int LOCK_MASK = PowerManager.PARTIAL_WAKE_LOCK |
| 92 | | PowerManager.SCREEN_DIM_WAKE_LOCK |
| 93 | | PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 94 | | PowerManager.FULL_WAKE_LOCK |
| 95 | | PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
| 97 | // time since last state: time since last event: |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 98 | // The short keylight delay comes from secure settings; this is the default. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec |
| 100 | private static final int MEDIUM_KEYLIGHT_DELAY = 15000; // t+15 sec |
| 101 | private static final int LONG_KEYLIGHT_DELAY = 6000; // t+6 sec |
| 102 | private static final int LONG_DIM_TIME = 7000; // t+N-5 sec |
| 103 | |
| Mathias Agopian | 47f1fe5 | 2011-11-08 17:18:41 -0800 | [diff] [blame] | 104 | // How long to wait to debounce light sensor changes in milliseconds |
| Mike Lockwood | 9b813692 | 2009-11-06 15:53:59 -0500 | [diff] [blame] | 105 | private static final int LIGHT_SENSOR_DELAY = 2000; |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 106 | |
| Mathias Agopian | 47f1fe5 | 2011-11-08 17:18:41 -0800 | [diff] [blame] | 107 | // light sensor events rate in microseconds |
| 108 | private static final int LIGHT_SENSOR_RATE = 1000000; |
| 109 | |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 110 | // Expansion of range of light values when applying scale from light |
| 111 | // sensor brightness setting, in the [0..255] brightness range. |
| 112 | private static final int LIGHT_SENSOR_RANGE_EXPANSION = 20; |
| 113 | |
| 114 | // Scaling factor of the light sensor brightness setting when applying |
| 115 | // it to the final brightness. |
| 116 | private static final int LIGHT_SENSOR_OFFSET_SCALE = 8; |
| 117 | |
| Mathias Agopian | 47f1fe5 | 2011-11-08 17:18:41 -0800 | [diff] [blame] | 118 | // For debouncing the proximity sensor in milliseconds |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 119 | private static final int PROXIMITY_SENSOR_DELAY = 1000; |
| 120 | |
| Mike Lockwood | d20ea36 | 2009-09-15 00:13:38 -0400 | [diff] [blame] | 121 | // trigger proximity if distance is less than 5 cm |
| 122 | private static final float PROXIMITY_THRESHOLD = 5.0f; |
| 123 | |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 124 | // Cached secure settings; see updateSettingsValues() |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT; |
| 126 | |
| Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 127 | // Default timeout for screen off, if not found in settings database = 15 seconds. |
| 128 | private static final int DEFAULT_SCREEN_OFF_TIMEOUT = 15000; |
| 129 | |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 130 | // Screen brightness should always have a value, but just in case... |
| 131 | private static final int DEFAULT_SCREEN_BRIGHTNESS = 192; |
| 132 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | // flags for setPowerState |
| 134 | private static final int SCREEN_ON_BIT = 0x00000001; |
| 135 | private static final int SCREEN_BRIGHT_BIT = 0x00000002; |
| 136 | private static final int BUTTON_BRIGHT_BIT = 0x00000004; |
| 137 | private static final int KEYBOARD_BRIGHT_BIT = 0x00000008; |
| 138 | private static final int BATTERY_LOW_BIT = 0x00000010; |
| 139 | |
| 140 | // values for setPowerState |
| 141 | |
| 142 | // SCREEN_OFF == everything off |
| 143 | private static final int SCREEN_OFF = 0x00000000; |
| 144 | |
| 145 | // SCREEN_DIM == screen on, screen backlight dim |
| 146 | private static final int SCREEN_DIM = SCREEN_ON_BIT; |
| 147 | |
| 148 | // SCREEN_BRIGHT == screen on, screen backlight bright |
| 149 | private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT; |
| 150 | |
| 151 | // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright |
| 152 | private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT; |
| 153 | |
| 154 | // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright |
| 155 | private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT; |
| 156 | |
| 157 | // used for noChangeLights in setPowerState() |
| 158 | private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT; |
| 159 | |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 160 | boolean mAnimateScreenLights = true; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 161 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | static final int ANIM_STEPS = 60/4; |
| Mike Lockwood | dd9668e | 2009-10-27 15:47:02 -0400 | [diff] [blame] | 163 | // Slower animation for autobrightness changes |
| 164 | static final int AUTOBRIGHTNESS_ANIM_STEPS = 60; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 165 | // Number of steps when performing a more immediate brightness change. |
| 166 | static final int IMMEDIATE_ANIM_STEPS = 4; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
| 168 | // These magic numbers are the initial state of the LEDs at boot. Ideally |
| 169 | // we should read them from the driver, but our current hardware returns 0 |
| 170 | // for the initial value. Oops! |
| 171 | static final int INITIAL_SCREEN_BRIGHTNESS = 255; |
| 172 | static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF; |
| 173 | static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 174 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | private final int MY_UID; |
| Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 176 | private final int MY_PID; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
| 178 | private boolean mDoneBooting = false; |
| Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 179 | private boolean mBootCompleted = false; |
| Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 180 | private boolean mHeadless = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | private int mStayOnConditions = 0; |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 182 | private final int[] mBroadcastQueue = new int[] { -1, -1, -1 }; |
| 183 | private final int[] mBroadcastWhy = new int[3]; |
| Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 184 | private boolean mPreparingForScreenOn = false; |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 185 | private boolean mSkippedScreenOn = false; |
| 186 | private boolean mInitialized = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | private int mPartialCount = 0; |
| 188 | private int mPowerState; |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 189 | // mScreenOffReason can be WindowManagerPolicy.OFF_BECAUSE_OF_USER, |
| 190 | // WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT or WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR |
| 191 | private int mScreenOffReason; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | private int mUserState; |
| 193 | private boolean mKeyboardVisible = false; |
| 194 | private boolean mUserActivityAllowed = true; |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 195 | private int mProximityWakeLockCount = 0; |
| 196 | private boolean mProximitySensorEnabled = false; |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 197 | private boolean mProximitySensorActive = false; |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 198 | private int mProximityPendingValue = -1; // -1 == nothing, 0 == inactive, 1 == active |
| 199 | private long mLastProximityEventTime; |
| Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 200 | private int mScreenOffTimeoutSetting; |
| 201 | private int mMaximumScreenOffTimeout = Integer.MAX_VALUE; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | private int mKeylightDelay; |
| 203 | private int mDimDelay; |
| 204 | private int mScreenOffDelay; |
| 205 | private int mWakeLockState; |
| 206 | private long mLastEventTime = 0; |
| 207 | private long mScreenOffTime; |
| 208 | private volatile WindowManagerPolicy mPolicy; |
| 209 | private final LockList mLocks = new LockList(); |
| 210 | private Intent mScreenOffIntent; |
| 211 | private Intent mScreenOnIntent; |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 212 | private LightsService mLightsService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | private Context mContext; |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 214 | private LightsService.Light mLcdLight; |
| 215 | private LightsService.Light mButtonLight; |
| 216 | private LightsService.Light mKeyboardLight; |
| 217 | private LightsService.Light mAttentionLight; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | private UnsynchronizedWakeLock mBroadcastWakeLock; |
| 219 | private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock; |
| 220 | private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock; |
| 221 | private UnsynchronizedWakeLock mPreventScreenOnPartialLock; |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 222 | private UnsynchronizedWakeLock mProximityPartialLock; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | private HandlerThread mHandlerThread; |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 224 | private HandlerThread mScreenOffThread; |
| 225 | private Handler mScreenOffHandler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | private Handler mHandler; |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 227 | private final TimeoutTask mTimeoutTask = new TimeoutTask(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | private final BrightnessState mScreenBrightness |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 229 | = new BrightnessState(SCREEN_BRIGHT_BIT); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 230 | private boolean mStillNeedSleepNotification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | private boolean mIsPowered = false; |
| 232 | private IActivityManager mActivityService; |
| 233 | private IBatteryStats mBatteryStats; |
| 234 | private BatteryService mBatteryService; |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 235 | private SensorManager mSensorManager; |
| 236 | private Sensor mProximitySensor; |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 237 | private Sensor mLightSensor; |
| 238 | private boolean mLightSensorEnabled; |
| 239 | private float mLightSensorValue = -1; |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 240 | private boolean mProxIgnoredBecauseScreenTurnedOff = false; |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 241 | private int mHighestLightSensorValue = -1; |
| Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 242 | private boolean mLightSensorPendingDecrease = false; |
| 243 | private boolean mLightSensorPendingIncrease = false; |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 244 | private float mLightSensorPendingValue = -1; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 245 | private float mLightSensorAdjustSetting = 0; |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 246 | private int mLightSensorScreenBrightness = -1; |
| 247 | private int mLightSensorButtonBrightness = -1; |
| 248 | private int mLightSensorKeyboardBrightness = -1; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | private boolean mDimScreen = true; |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 250 | private boolean mIsDocked = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | private long mNextTimeout; |
| 252 | private volatile int mPokey = 0; |
| 253 | private volatile boolean mPokeAwakeOnSet = false; |
| 254 | private volatile boolean mInitComplete = false; |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 255 | private final HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>(); |
| Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 256 | // mLastScreenOnTime is the time the screen was last turned on |
| 257 | private long mLastScreenOnTime; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | private boolean mPreventScreenOn; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 259 | private int mScreenBrightnessSetting = DEFAULT_SCREEN_BRIGHTNESS; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | private int mScreenBrightnessOverride = -1; |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 261 | private int mButtonBrightnessOverride = -1; |
| Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 262 | private int mScreenBrightnessDim; |
| Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 263 | private boolean mUseSoftwareAutoBrightness; |
| Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 264 | private boolean mAutoBrightessEnabled; |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 265 | private int[] mAutoBrightnessLevels; |
| 266 | private int[] mLcdBacklightValues; |
| 267 | private int[] mButtonBacklightValues; |
| 268 | private int[] mKeyboardBacklightValues; |
| Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 269 | private int mLightSensorWarmupTime; |
| Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 270 | boolean mUnplugTurnsOnScreen; |
| Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 271 | private int mWarningSpewThrottleCount; |
| 272 | private long mWarningSpewThrottleTime; |
| Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 273 | private int mAnimationSetting = ANIM_SETTING_OFF; |
| 274 | |
| 275 | // Must match with the ISurfaceComposer constants in C++. |
| 276 | private static final int ANIM_SETTING_ON = 0x01; |
| 277 | private static final int ANIM_SETTING_OFF = 0x10; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | |
| 279 | // Used when logging number and duration of touch-down cycles |
| 280 | private long mTotalTouchDownTime; |
| 281 | private long mLastTouchDown; |
| 282 | private int mTouchCycles; |
| 283 | |
| 284 | // could be either static or controllable at runtime |
| 285 | private static final boolean mSpew = false; |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 286 | private static final boolean mDebugProximitySensor = (false || mSpew); |
| Mike Lockwood | ae92eb3 | 2011-10-25 10:11:46 -0400 | [diff] [blame] | 287 | private static final boolean mDebugLightSensor = (false || mSpew); |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 288 | |
| 289 | private native void nativeInit(); |
| 290 | private native void nativeSetPowerState(boolean screenOn, boolean screenBright); |
| Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 291 | private native void nativeStartSurfaceFlingerAnimation(int mode); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | static PrintStream mLog; |
| 295 | static { |
| 296 | try { |
| 297 | mLog = new PrintStream("/data/power.log"); |
| 298 | } |
| 299 | catch (FileNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 300 | android.util.Slog.e(TAG, "Life is hard", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | static class Log { |
| 304 | static void d(String tag, String s) { |
| 305 | mLog.println(s); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 306 | android.util.Slog.d(tag, s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | static void i(String tag, String s) { |
| 309 | mLog.println(s); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 310 | android.util.Slog.i(tag, s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | static void w(String tag, String s) { |
| 313 | mLog.println(s); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 314 | android.util.Slog.w(tag, s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
| 316 | static void e(String tag, String s) { |
| 317 | mLog.println(s); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 318 | android.util.Slog.e(tag, s); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | */ |
| 322 | |
| 323 | /** |
| 324 | * This class works around a deadlock between the lock in PowerManager.WakeLock |
| 325 | * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its |
| 326 | * mToken object so it can be accessed from any thread, but it calls into here |
| 327 | * with its lock held. This class is essentially a reimplementation of |
| 328 | * PowerManager.WakeLock, but without that extra synchronized block, because we'll |
| 329 | * only call it with our own locks held. |
| 330 | */ |
| 331 | private class UnsynchronizedWakeLock { |
| 332 | int mFlags; |
| 333 | String mTag; |
| 334 | IBinder mToken; |
| 335 | int mCount = 0; |
| 336 | boolean mRefCounted; |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 337 | boolean mHeld; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | |
| 339 | UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) { |
| 340 | mFlags = flags; |
| 341 | mTag = tag; |
| 342 | mToken = new Binder(); |
| 343 | mRefCounted = refCounted; |
| 344 | } |
| 345 | |
| 346 | public void acquire() { |
| 347 | if (!mRefCounted || mCount++ == 0) { |
| 348 | long ident = Binder.clearCallingIdentity(); |
| 349 | try { |
| 350 | PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken, |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 351 | MY_UID, MY_PID, mTag, null); |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 352 | mHeld = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } finally { |
| 354 | Binder.restoreCallingIdentity(ident); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | public void release() { |
| 360 | if (!mRefCounted || --mCount == 0) { |
| Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 361 | PowerManagerService.this.releaseWakeLockLocked(mToken, 0, false); |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 362 | mHeld = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |
| 364 | if (mCount < 0) { |
| 365 | throw new RuntimeException("WakeLock under-locked " + mTag); |
| 366 | } |
| 367 | } |
| 368 | |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 369 | public boolean isHeld() |
| 370 | { |
| 371 | return mHeld; |
| 372 | } |
| 373 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | public String toString() { |
| 375 | return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags) |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 376 | + " mCount=" + mCount + " mHeld=" + mHeld + ")"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | private final class BatteryReceiver extends BroadcastReceiver { |
| 381 | @Override |
| 382 | public void onReceive(Context context, Intent intent) { |
| 383 | synchronized (mLocks) { |
| 384 | boolean wasPowered = mIsPowered; |
| 385 | mIsPowered = mBatteryService.isPowered(); |
| 386 | |
| 387 | if (mIsPowered != wasPowered) { |
| 388 | // update mStayOnWhilePluggedIn wake lock |
| 389 | updateWakeLockLocked(); |
| 390 | |
| 391 | // treat plugging and unplugging the devices as a user activity. |
| 392 | // users find it disconcerting when they unplug the device |
| 393 | // and it shuts off right away. |
| Mike Lockwood | 84a8934 | 2010-03-01 21:28:58 -0500 | [diff] [blame] | 394 | // to avoid turning on the screen when unplugging, we only trigger |
| 395 | // user activity when screen was already on. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | // temporarily set mUserActivityAllowed to true so this will work |
| 397 | // even when the keyguard is on. |
| Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 398 | // However, you can also set config_unplugTurnsOnScreen to have it |
| 399 | // turn on. Some devices want this because they don't have a |
| 400 | // charging LED. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | synchronized (mLocks) { |
| Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 402 | if (!wasPowered || (mPowerState & SCREEN_ON_BIT) != 0 || |
| 403 | mUnplugTurnsOnScreen) { |
| Mike Lockwood | 84a8934 | 2010-03-01 21:28:58 -0500 | [diff] [blame] | 404 | forceUserActivityLocked(); |
| 405 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 412 | private final class BootCompletedReceiver extends BroadcastReceiver { |
| 413 | @Override |
| 414 | public void onReceive(Context context, Intent intent) { |
| 415 | bootCompleted(); |
| 416 | } |
| 417 | } |
| 418 | |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 419 | private final class DockReceiver extends BroadcastReceiver { |
| 420 | @Override |
| 421 | public void onReceive(Context context, Intent intent) { |
| 422 | int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 423 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 424 | dockStateChanged(state); |
| 425 | } |
| 426 | } |
| 427 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | /** |
| 429 | * Set the setting that determines whether the device stays on when plugged in. |
| 430 | * The argument is a bit string, with each bit specifying a power source that, |
| 431 | * when the device is connected to that source, causes the device to stay on. |
| 432 | * See {@link android.os.BatteryManager} for the list of power sources that |
| 433 | * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC} |
| 434 | * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB} |
| 435 | * @param val an {@code int} containing the bits that specify which power sources |
| 436 | * should cause the device to stay on. |
| 437 | */ |
| 438 | public void setStayOnSetting(int val) { |
| 439 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null); |
| 440 | Settings.System.putInt(mContext.getContentResolver(), |
| 441 | Settings.System.STAY_ON_WHILE_PLUGGED_IN, val); |
| 442 | } |
| 443 | |
| Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 444 | public void setMaximumScreenOffTimeount(int timeMs) { |
| 445 | mContext.enforceCallingOrSelfPermission( |
| 446 | android.Manifest.permission.WRITE_SECURE_SETTINGS, null); |
| 447 | synchronized (mLocks) { |
| 448 | mMaximumScreenOffTimeout = timeMs; |
| 449 | // recalculate everything |
| 450 | setScreenOffTimeoutsLocked(); |
| 451 | } |
| 452 | } |
| 453 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | private class SettingsObserver implements Observer { |
| Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 455 | private int getInt(String name, int defValue) { |
| 456 | ContentValues values = mSettings.getValues(name); |
| 457 | Integer iVal = values != null ? values.getAsInteger(Settings.System.VALUE) : null; |
| 458 | return iVal != null ? iVal : defValue; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 461 | private float getFloat(String name, float defValue) { |
| 462 | ContentValues values = mSettings.getValues(name); |
| 463 | Float fVal = values != null ? values.getAsFloat(Settings.System.VALUE) : null; |
| 464 | return fVal != null ? fVal : defValue; |
| 465 | } |
| 466 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | public void update(Observable o, Object arg) { |
| 468 | synchronized (mLocks) { |
| Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 469 | // STAY_ON_WHILE_PLUGGED_IN, default to when plugged into AC |
| 470 | mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN, |
| 471 | BatteryManager.BATTERY_PLUGGED_AC); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | updateWakeLockLocked(); |
| 473 | |
| Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 474 | // SCREEN_OFF_TIMEOUT, default to 15 seconds |
| 475 | mScreenOffTimeoutSetting = getInt(SCREEN_OFF_TIMEOUT, DEFAULT_SCREEN_OFF_TIMEOUT); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | |
| Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 477 | // DIM_SCREEN |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | //mDimScreen = getInt(DIM_SCREEN) != 0; |
| 479 | |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 480 | mScreenBrightnessSetting = getInt(SCREEN_BRIGHTNESS, DEFAULT_SCREEN_BRIGHTNESS); |
| 481 | mLightSensorAdjustSetting = getFloat(SCREEN_AUTO_BRIGHTNESS_ADJ, 0); |
| 482 | |
| Amith Yamasani | 8b61983 | 2010-09-22 16:11:59 -0700 | [diff] [blame] | 483 | // SCREEN_BRIGHTNESS_MODE, default to manual |
| 484 | setScreenBrightnessMode(getInt(SCREEN_BRIGHTNESS_MODE, |
| 485 | Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)); |
| Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 486 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | // recalculate everything |
| 488 | setScreenOffTimeoutsLocked(); |
| Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 489 | |
| 490 | final float windowScale = getFloat(WINDOW_ANIMATION_SCALE, 1.0f); |
| 491 | final float transitionScale = getFloat(TRANSITION_ANIMATION_SCALE, 1.0f); |
| 492 | mAnimationSetting = 0; |
| 493 | if (windowScale > 0.5f) { |
| 494 | mAnimationSetting |= ANIM_SETTING_OFF; |
| 495 | } |
| 496 | if (transitionScale > 0.5f) { |
| 497 | // Uncomment this if you want the screen-on animation. |
| 498 | // mAnimationSetting |= ANIM_SETTING_ON; |
| 499 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 504 | PowerManagerService() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | // Hack to get our uid... should have a func for this. |
| 506 | long token = Binder.clearCallingIdentity(); |
| Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 507 | MY_UID = Process.myUid(); |
| 508 | MY_PID = Process.myPid(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | Binder.restoreCallingIdentity(token); |
| 510 | |
| 511 | // XXX remove this when the kernel doesn't timeout wake locks |
| 512 | Power.setLastUserActivityTimeout(7*24*3600*1000); // one week |
| 513 | |
| 514 | // assume nothing is on yet |
| 515 | mUserState = mPowerState = 0; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 516 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | // Add ourself to the Watchdog monitors. |
| 518 | Watchdog.getInstance().addMonitor(this); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | private ContentQueryMap mSettings; |
| 522 | |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 523 | void init(Context context, LightsService lights, IActivityManager activity, |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 524 | BatteryService battery) { |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 525 | mLightsService = lights; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | mContext = context; |
| 527 | mActivityService = activity; |
| 528 | mBatteryStats = BatteryStatsService.getService(); |
| 529 | mBatteryService = battery; |
| 530 | |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 531 | mLcdLight = lights.getLight(LightsService.LIGHT_ID_BACKLIGHT); |
| 532 | mButtonLight = lights.getLight(LightsService.LIGHT_ID_BUTTONS); |
| 533 | mKeyboardLight = lights.getLight(LightsService.LIGHT_ID_KEYBOARD); |
| 534 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 535 | mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0")); |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 536 | |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 537 | nativeInit(); |
| 538 | synchronized (mLocks) { |
| 539 | updateNativePowerStateLocked(); |
| 540 | } |
| 541 | |
| 542 | mInitComplete = false; |
| 543 | mScreenOffThread = new HandlerThread("PowerManagerService.mScreenOffThread") { |
| 544 | @Override |
| 545 | protected void onLooperPrepared() { |
| 546 | mScreenOffHandler = new Handler(); |
| 547 | synchronized (mScreenOffThread) { |
| 548 | mInitComplete = true; |
| 549 | mScreenOffThread.notifyAll(); |
| 550 | } |
| 551 | } |
| 552 | }; |
| 553 | mScreenOffThread.start(); |
| 554 | |
| 555 | synchronized (mScreenOffThread) { |
| 556 | while (!mInitComplete) { |
| 557 | try { |
| 558 | mScreenOffThread.wait(); |
| 559 | } catch (InterruptedException e) { |
| 560 | // Ignore |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | mInitComplete = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | mHandlerThread = new HandlerThread("PowerManagerService") { |
| 567 | @Override |
| 568 | protected void onLooperPrepared() { |
| 569 | super.onLooperPrepared(); |
| 570 | initInThread(); |
| 571 | } |
| 572 | }; |
| 573 | mHandlerThread.start(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 574 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | synchronized (mHandlerThread) { |
| 576 | while (!mInitComplete) { |
| 577 | try { |
| 578 | mHandlerThread.wait(); |
| 579 | } catch (InterruptedException e) { |
| 580 | // Ignore |
| 581 | } |
| 582 | } |
| 583 | } |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 584 | |
| 585 | nativeInit(); |
| 586 | synchronized (mLocks) { |
| 587 | updateNativePowerStateLocked(); |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 588 | // We make sure to start out with the screen on due to user activity. |
| 589 | // (They did just boot their device, after all.) |
| 590 | forceUserActivityLocked(); |
| Dianne Hackborn | 4001109 | 2011-09-22 13:37:48 -0700 | [diff] [blame] | 591 | mInitialized = true; |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 592 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 594 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | void initInThread() { |
| 596 | mHandler = new Handler(); |
| 597 | |
| 598 | mBroadcastWakeLock = new UnsynchronizedWakeLock( |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 599 | PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock( |
| 601 | PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false); |
| 602 | mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock( |
| 603 | PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false); |
| 604 | mPreventScreenOnPartialLock = new UnsynchronizedWakeLock( |
| 605 | PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false); |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 606 | mProximityPartialLock = new UnsynchronizedWakeLock( |
| 607 | PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | |
| 609 | mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON); |
| 610 | mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 611 | mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF); |
| 612 | mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 613 | |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 614 | Resources resources = mContext.getResources(); |
| Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 615 | |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 616 | mAnimateScreenLights = resources.getBoolean( |
| 617 | com.android.internal.R.bool.config_animateScreenLights); |
| 618 | |
| Joe Onorato | 6d74765 | 2010-10-11 15:15:31 -0700 | [diff] [blame] | 619 | mUnplugTurnsOnScreen = resources.getBoolean( |
| 620 | com.android.internal.R.bool.config_unplugTurnsOnScreen); |
| 621 | |
| Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 622 | mScreenBrightnessDim = resources.getInteger( |
| 623 | com.android.internal.R.integer.config_screenBrightnessDim); |
| 624 | |
| Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 625 | // read settings for auto-brightness |
| 626 | mUseSoftwareAutoBrightness = resources.getBoolean( |
| 627 | com.android.internal.R.bool.config_automatic_brightness_available); |
| Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 628 | if (mUseSoftwareAutoBrightness) { |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 629 | mAutoBrightnessLevels = resources.getIntArray( |
| 630 | com.android.internal.R.array.config_autoBrightnessLevels); |
| 631 | mLcdBacklightValues = resources.getIntArray( |
| 632 | com.android.internal.R.array.config_autoBrightnessLcdBacklightValues); |
| 633 | mButtonBacklightValues = resources.getIntArray( |
| 634 | com.android.internal.R.array.config_autoBrightnessButtonBacklightValues); |
| 635 | mKeyboardBacklightValues = resources.getIntArray( |
| 636 | com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues); |
| Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 637 | mLightSensorWarmupTime = resources.getInteger( |
| 638 | com.android.internal.R.integer.config_lightSensorWarmupTime); |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 639 | } |
| Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 640 | |
| 641 | ContentResolver resolver = mContext.getContentResolver(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null, |
| 643 | "(" + Settings.System.NAME + "=?) or (" |
| 644 | + Settings.System.NAME + "=?) or (" |
| Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 645 | + Settings.System.NAME + "=?) or (" |
| Joe Onorato | 609695d | 2010-10-14 14:57:49 -0700 | [diff] [blame] | 646 | + Settings.System.NAME + "=?) or (" |
| 647 | + Settings.System.NAME + "=?) or (" |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 648 | + Settings.System.NAME + "=?) or (" |
| 649 | + Settings.System.NAME + "=?) or (" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | + Settings.System.NAME + "=?)", |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 651 | new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN, SCREEN_BRIGHTNESS, |
| 652 | SCREEN_BRIGHTNESS_MODE, SCREEN_AUTO_BRIGHTNESS_ADJ, |
| 653 | WINDOW_ANIMATION_SCALE, TRANSITION_ANIMATION_SCALE}, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | null); |
| 655 | mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler); |
| 656 | SettingsObserver settingsObserver = new SettingsObserver(); |
| 657 | mSettings.addObserver(settingsObserver); |
| 658 | |
| 659 | // pretend that the settings changed so we will get their initial state |
| 660 | settingsObserver.update(mSettings, null); |
| 661 | |
| 662 | // register for the battery changed notifications |
| 663 | IntentFilter filter = new IntentFilter(); |
| 664 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| 665 | mContext.registerReceiver(new BatteryReceiver(), filter); |
| Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 666 | filter = new IntentFilter(); |
| 667 | filter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 668 | mContext.registerReceiver(new BootCompletedReceiver(), filter); |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 669 | filter = new IntentFilter(); |
| 670 | filter.addAction(Intent.ACTION_DOCK_EVENT); |
| 671 | mContext.registerReceiver(new DockReceiver(), filter); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 673 | // Listen for secure settings changes |
| 674 | mContext.getContentResolver().registerContentObserver( |
| 675 | Settings.Secure.CONTENT_URI, true, |
| 676 | new ContentObserver(new Handler()) { |
| 677 | public void onChange(boolean selfChange) { |
| 678 | updateSettingsValues(); |
| 679 | } |
| 680 | }); |
| 681 | updateSettingsValues(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 683 | synchronized (mHandlerThread) { |
| 684 | mInitComplete = true; |
| 685 | mHandlerThread.notifyAll(); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | private class WakeLock implements IBinder.DeathRecipient |
| 690 | { |
| Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 691 | WakeLock(int f, IBinder b, String t, int u, int p) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | super(); |
| 693 | flags = f; |
| 694 | binder = b; |
| 695 | tag = t; |
| 696 | uid = u == MY_UID ? Process.SYSTEM_UID : u; |
| Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 697 | pid = p; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | if (u != MY_UID || ( |
| 699 | !"KEEP_SCREEN_ON_FLAG".equals(tag) |
| 700 | && !"KeyInputQueue".equals(tag))) { |
| 701 | monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK |
| 702 | ? BatteryStats.WAKE_TYPE_PARTIAL |
| 703 | : BatteryStats.WAKE_TYPE_FULL; |
| 704 | } else { |
| 705 | monitorType = -1; |
| 706 | } |
| 707 | try { |
| 708 | b.linkToDeath(this, 0); |
| 709 | } catch (RemoteException e) { |
| 710 | binderDied(); |
| 711 | } |
| 712 | } |
| 713 | public void binderDied() { |
| 714 | synchronized (mLocks) { |
| Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 715 | releaseWakeLockLocked(this.binder, 0, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | final int flags; |
| 719 | final IBinder binder; |
| 720 | final String tag; |
| 721 | final int uid; |
| Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 722 | final int pid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | final int monitorType; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 724 | WorkSource ws; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | boolean activated = true; |
| 726 | int minState; |
| 727 | } |
| 728 | |
| 729 | private void updateWakeLockLocked() { |
| 730 | if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) { |
| 731 | // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set. |
| 732 | mStayOnWhilePluggedInScreenDimLock.acquire(); |
| 733 | mStayOnWhilePluggedInPartialLock.acquire(); |
| 734 | } else { |
| 735 | mStayOnWhilePluggedInScreenDimLock.release(); |
| 736 | mStayOnWhilePluggedInPartialLock.release(); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | private boolean isScreenLock(int flags) |
| 741 | { |
| 742 | int n = flags & LOCK_MASK; |
| 743 | return n == PowerManager.FULL_WAKE_LOCK |
| 744 | || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 745 | || n == PowerManager.SCREEN_DIM_WAKE_LOCK |
| 746 | || n == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | } |
| 748 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 749 | void enforceWakeSourcePermission(int uid, int pid) { |
| 750 | if (uid == Process.myUid()) { |
| 751 | return; |
| 752 | } |
| 753 | mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS, |
| 754 | pid, uid, null); |
| 755 | } |
| 756 | |
| 757 | public void acquireWakeLock(int flags, IBinder lock, String tag, WorkSource ws) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 759 | int pid = Binder.getCallingPid(); |
| Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 760 | if (uid != Process.myUid()) { |
| 761 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 762 | } |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 763 | if (ws != null) { |
| 764 | enforceWakeSourcePermission(uid, pid); |
| 765 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | long ident = Binder.clearCallingIdentity(); |
| 767 | try { |
| 768 | synchronized (mLocks) { |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 769 | acquireWakeLockLocked(flags, lock, uid, pid, tag, ws); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | } |
| 771 | } finally { |
| 772 | Binder.restoreCallingIdentity(ident); |
| 773 | } |
| 774 | } |
| 775 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 776 | void noteStartWakeLocked(WakeLock wl, WorkSource ws) { |
| Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 777 | if (wl.monitorType >= 0) { |
| 778 | long origId = Binder.clearCallingIdentity(); |
| 779 | try { |
| 780 | if (ws != null) { |
| 781 | mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag, |
| 782 | wl.monitorType); |
| 783 | } else { |
| 784 | mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType); |
| 785 | } |
| 786 | } catch (RemoteException e) { |
| 787 | // Ignore |
| 788 | } finally { |
| 789 | Binder.restoreCallingIdentity(origId); |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 790 | } |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 794 | void noteStopWakeLocked(WakeLock wl, WorkSource ws) { |
| Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 795 | if (wl.monitorType >= 0) { |
| 796 | long origId = Binder.clearCallingIdentity(); |
| 797 | try { |
| 798 | if (ws != null) { |
| 799 | mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag, |
| 800 | wl.monitorType); |
| 801 | } else { |
| 802 | mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType); |
| 803 | } |
| 804 | } catch (RemoteException e) { |
| 805 | // Ignore |
| 806 | } finally { |
| 807 | Binder.restoreCallingIdentity(origId); |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 808 | } |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
| 812 | public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag, |
| 813 | WorkSource ws) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 815 | Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | } |
| 817 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 818 | if (ws != null && ws.size() == 0) { |
| 819 | ws = null; |
| 820 | } |
| 821 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | int index = mLocks.getIndex(lock); |
| 823 | WakeLock wl; |
| 824 | boolean newlock; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 825 | boolean diffsource; |
| 826 | WorkSource oldsource; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 827 | if (index < 0) { |
| Dianne Hackborn | 9adb9c3 | 2010-08-13 14:09:56 -0700 | [diff] [blame] | 828 | wl = new WakeLock(flags, lock, tag, uid, pid); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | switch (wl.flags & LOCK_MASK) |
| 830 | { |
| 831 | case PowerManager.FULL_WAKE_LOCK: |
| Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 832 | if (mUseSoftwareAutoBrightness) { |
| Mike Lockwood | 3333fa4 | 2009-10-26 14:50:42 -0400 | [diff] [blame] | 833 | wl.minState = SCREEN_BRIGHT; |
| 834 | } else { |
| 835 | wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT); |
| 836 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | break; |
| 838 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| 839 | wl.minState = SCREEN_BRIGHT; |
| 840 | break; |
| 841 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| 842 | wl.minState = SCREEN_DIM; |
| 843 | break; |
| 844 | case PowerManager.PARTIAL_WAKE_LOCK: |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 845 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | break; |
| 847 | default: |
| 848 | // just log and bail. we're in the server, so don't |
| 849 | // throw an exception. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 850 | Slog.e(TAG, "bad wakelock type for lock '" + tag + "' " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | + " flags=" + flags); |
| 852 | return; |
| 853 | } |
| 854 | mLocks.addLock(wl); |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 855 | if (ws != null) { |
| 856 | wl.ws = new WorkSource(ws); |
| 857 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | newlock = true; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 859 | diffsource = false; |
| 860 | oldsource = null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | } else { |
| 862 | wl = mLocks.get(index); |
| 863 | newlock = false; |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 864 | oldsource = wl.ws; |
| 865 | if (oldsource != null) { |
| 866 | if (ws == null) { |
| 867 | wl.ws = null; |
| 868 | diffsource = true; |
| 869 | } else { |
| 870 | diffsource = oldsource.diff(ws); |
| 871 | } |
| 872 | } else if (ws != null) { |
| 873 | diffsource = true; |
| 874 | } else { |
| 875 | diffsource = false; |
| 876 | } |
| 877 | if (diffsource) { |
| 878 | wl.ws = new WorkSource(ws); |
| 879 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | } |
| 881 | if (isScreenLock(flags)) { |
| 882 | // if this causes a wakeup, we reactivate all of the locks and |
| 883 | // set it to whatever they want. otherwise, we modulate that |
| 884 | // by the current state so we never turn it more on than |
| 885 | // it already is. |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 886 | if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) { |
| 887 | mProximityWakeLockCount++; |
| 888 | if (mProximityWakeLockCount == 1) { |
| 889 | enableProximityLockLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | } else { |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 892 | if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) { |
| 893 | int oldWakeLockState = mWakeLockState; |
| 894 | mWakeLockState = mLocks.reactivateScreenLocksLocked(); |
| Mike Lockwood | db97f60 | 2011-09-02 11:59:08 -0400 | [diff] [blame] | 895 | |
| 896 | // Disable proximity sensor if if user presses power key while we are in the |
| 897 | // "waiting for proximity sensor to go negative" state. |
| 898 | if ((mWakeLockState & SCREEN_ON_BIT) != 0 |
| 899 | && mProximitySensorActive && mProximityWakeLockCount == 0) { |
| 900 | mProximitySensorActive = false; |
| 901 | } |
| 902 | |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 903 | if (mSpew) { |
| 904 | Slog.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState) |
| 905 | + " mWakeLockState=0x" |
| 906 | + Integer.toHexString(mWakeLockState) |
| 907 | + " previous wakeLockState=0x" |
| 908 | + Integer.toHexString(oldWakeLockState)); |
| 909 | } |
| 910 | } else { |
| 911 | if (mSpew) { |
| 912 | Slog.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState) |
| 913 | + " mLocks.gatherState()=0x" |
| 914 | + Integer.toHexString(mLocks.gatherState()) |
| 915 | + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)); |
| 916 | } |
| 917 | mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | } |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 919 | setPowerState(mWakeLockState | mUserState); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | } |
| 922 | else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) { |
| 923 | if (newlock) { |
| 924 | mPartialCount++; |
| 925 | if (mPartialCount == 1) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 926 | if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME); |
| 930 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | |
| Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 932 | if (diffsource) { |
| 933 | // If the lock sources have changed, need to first release the |
| 934 | // old ones. |
| 935 | noteStopWakeLocked(wl, oldsource); |
| 936 | } |
| 937 | if (newlock || diffsource) { |
| 938 | noteStartWakeLocked(wl, ws); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) { |
| 943 | int uid = Binder.getCallingUid(); |
| 944 | int pid = Binder.getCallingPid(); |
| 945 | if (ws != null && ws.size() == 0) { |
| 946 | ws = null; |
| 947 | } |
| 948 | if (ws != null) { |
| 949 | enforceWakeSourcePermission(uid, pid); |
| 950 | } |
| Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 951 | synchronized (mLocks) { |
| 952 | int index = mLocks.getIndex(lock); |
| 953 | if (index < 0) { |
| 954 | throw new IllegalArgumentException("Wake lock not active"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | } |
| Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 956 | WakeLock wl = mLocks.get(index); |
| 957 | WorkSource oldsource = wl.ws; |
| 958 | wl.ws = ws != null ? new WorkSource(ws) : null; |
| 959 | noteStopWakeLocked(wl, oldsource); |
| 960 | noteStartWakeLocked(wl, ws); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
| Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 964 | public void releaseWakeLock(IBinder lock, int flags) { |
| Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 965 | int uid = Binder.getCallingUid(); |
| 966 | if (uid != Process.myUid()) { |
| 967 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null); |
| 968 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | |
| 970 | synchronized (mLocks) { |
| Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 971 | releaseWakeLockLocked(lock, flags, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
| Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 975 | private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | WakeLock wl = mLocks.removeLock(lock); |
| 977 | if (wl == null) { |
| 978 | return; |
| 979 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 980 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 982 | Slog.d(TAG, "releaseWakeLock flags=0x" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | + Integer.toHexString(wl.flags) + " tag=" + wl.tag); |
| 984 | } |
| 985 | |
| 986 | if (isScreenLock(wl.flags)) { |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 987 | if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) { |
| 988 | mProximityWakeLockCount--; |
| 989 | if (mProximityWakeLockCount == 0) { |
| 990 | if (mProximitySensorActive && |
| 991 | ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) { |
| 992 | // wait for proximity sensor to go negative before disabling sensor |
| 993 | if (mDebugProximitySensor) { |
| 994 | Slog.d(TAG, "waiting for proximity sensor to go negative"); |
| 995 | } |
| 996 | } else { |
| 997 | disableProximityLockLocked(); |
| 998 | } |
| 999 | } |
| 1000 | } else { |
| 1001 | mWakeLockState = mLocks.gatherState(); |
| 1002 | // goes in the middle to reduce flicker |
| 1003 | if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) { |
| 1004 | userActivity(SystemClock.uptimeMillis(), -1, false, OTHER_EVENT, false); |
| 1005 | } |
| 1006 | setPowerState(mWakeLockState | mUserState); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | } |
| 1009 | else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) { |
| 1010 | mPartialCount--; |
| 1011 | if (mPartialCount == 0) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1012 | if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | Power.releaseWakeLock(PARTIAL_NAME); |
| 1014 | } |
| 1015 | } |
| 1016 | // Unlink the lock from the binder. |
| 1017 | wl.binder.unlinkToDeath(wl, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | |
| Dianne Hackborn | 70be167 | 2010-09-14 11:13:03 -0700 | [diff] [blame] | 1019 | noteStopWakeLocked(wl, wl.ws); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | private class PokeLock implements IBinder.DeathRecipient |
| 1023 | { |
| 1024 | PokeLock(int p, IBinder b, String t) { |
| 1025 | super(); |
| 1026 | this.pokey = p; |
| 1027 | this.binder = b; |
| 1028 | this.tag = t; |
| 1029 | try { |
| 1030 | b.linkToDeath(this, 0); |
| 1031 | } catch (RemoteException e) { |
| 1032 | binderDied(); |
| 1033 | } |
| 1034 | } |
| 1035 | public void binderDied() { |
| 1036 | setPokeLock(0, this.binder, this.tag); |
| 1037 | } |
| 1038 | int pokey; |
| 1039 | IBinder binder; |
| 1040 | String tag; |
| 1041 | boolean awakeOnSet; |
| 1042 | } |
| 1043 | |
| 1044 | public void setPokeLock(int pokey, IBinder token, String tag) { |
| 1045 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1046 | if (token == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1047 | Slog.e(TAG, "setPokeLock got null token for tag='" + tag + "'"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | return; |
| 1049 | } |
| 1050 | |
| 1051 | if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) { |
| 1052 | throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT" |
| 1053 | + " and POKE_LOCK_MEDIUM_TIMEOUT"); |
| 1054 | } |
| 1055 | |
| 1056 | synchronized (mLocks) { |
| 1057 | if (pokey != 0) { |
| 1058 | PokeLock p = mPokeLocks.get(token); |
| 1059 | int oldPokey = 0; |
| 1060 | if (p != null) { |
| 1061 | oldPokey = p.pokey; |
| 1062 | p.pokey = pokey; |
| 1063 | } else { |
| 1064 | p = new PokeLock(pokey, token, tag); |
| 1065 | mPokeLocks.put(token, p); |
| 1066 | } |
| 1067 | int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK; |
| 1068 | int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK; |
| 1069 | if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) { |
| 1070 | p.awakeOnSet = true; |
| 1071 | } |
| 1072 | } else { |
| Suchi Amalapurapu | fff2fda | 2009-06-30 21:36:16 -0700 | [diff] [blame] | 1073 | PokeLock rLock = mPokeLocks.remove(token); |
| 1074 | if (rLock != null) { |
| 1075 | token.unlinkToDeath(rLock, 0); |
| 1076 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | int oldPokey = mPokey; |
| 1080 | int cumulative = 0; |
| 1081 | boolean oldAwakeOnSet = mPokeAwakeOnSet; |
| 1082 | boolean awakeOnSet = false; |
| 1083 | for (PokeLock p: mPokeLocks.values()) { |
| 1084 | cumulative |= p.pokey; |
| 1085 | if (p.awakeOnSet) { |
| 1086 | awakeOnSet = true; |
| 1087 | } |
| 1088 | } |
| 1089 | mPokey = cumulative; |
| 1090 | mPokeAwakeOnSet = awakeOnSet; |
| 1091 | |
| 1092 | int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK; |
| 1093 | int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1094 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | if (oldCumulativeTimeout != newCumulativeTimeout) { |
| 1096 | setScreenOffTimeoutsLocked(); |
| 1097 | // reset the countdown timer, but use the existing nextState so it doesn't |
| 1098 | // change anything |
| 1099 | setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState); |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | private static String lockType(int type) |
| 1105 | { |
| 1106 | switch (type) |
| 1107 | { |
| 1108 | case PowerManager.FULL_WAKE_LOCK: |
| David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1109 | return "FULL_WAKE_LOCK "; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | case PowerManager.SCREEN_BRIGHT_WAKE_LOCK: |
| David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1111 | return "SCREEN_BRIGHT_WAKE_LOCK "; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | case PowerManager.SCREEN_DIM_WAKE_LOCK: |
| David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1113 | return "SCREEN_DIM_WAKE_LOCK "; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | case PowerManager.PARTIAL_WAKE_LOCK: |
| David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1115 | return "PARTIAL_WAKE_LOCK "; |
| 1116 | case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK: |
| 1117 | return "PROXIMITY_SCREEN_OFF_WAKE_LOCK"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | default: |
| David Brown | 251faa6 | 2009-08-02 22:04:36 -0700 | [diff] [blame] | 1119 | return "??? "; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | private static String dumpPowerState(int state) { |
| 1124 | return (((state & KEYBOARD_BRIGHT_BIT) != 0) |
| 1125 | ? "KEYBOARD_BRIGHT_BIT " : "") |
| 1126 | + (((state & SCREEN_BRIGHT_BIT) != 0) |
| 1127 | ? "SCREEN_BRIGHT_BIT " : "") |
| 1128 | + (((state & SCREEN_ON_BIT) != 0) |
| 1129 | ? "SCREEN_ON_BIT " : "") |
| 1130 | + (((state & BATTERY_LOW_BIT) != 0) |
| 1131 | ? "BATTERY_LOW_BIT " : ""); |
| 1132 | } |
| 1133 | |
| 1134 | @Override |
| 1135 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1136 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1137 | != PackageManager.PERMISSION_GRANTED) { |
| 1138 | pw.println("Permission Denial: can't dump PowerManager from from pid=" |
| 1139 | + Binder.getCallingPid() |
| 1140 | + ", uid=" + Binder.getCallingUid()); |
| 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | long now = SystemClock.uptimeMillis(); |
| 1145 | |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1146 | synchronized (mLocks) { |
| 1147 | pw.println("Power Manager State:"); |
| 1148 | pw.println(" mIsPowered=" + mIsPowered |
| 1149 | + " mPowerState=" + mPowerState |
| 1150 | + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime) |
| 1151 | + " ms"); |
| 1152 | pw.println(" mPartialCount=" + mPartialCount); |
| 1153 | pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState)); |
| 1154 | pw.println(" mUserState=" + dumpPowerState(mUserState)); |
| 1155 | pw.println(" mPowerState=" + dumpPowerState(mPowerState)); |
| 1156 | pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState())); |
| 1157 | pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now |
| 1158 | + " " + ((mNextTimeout-now)/1000) + "s from now"); |
| 1159 | pw.println(" mDimScreen=" + mDimScreen |
| Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1160 | + " mStayOnConditions=" + mStayOnConditions |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1161 | + " mPreparingForScreenOn=" + mPreparingForScreenOn |
| 1162 | + " mSkippedScreenOn=" + mSkippedScreenOn); |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1163 | pw.println(" mScreenOffReason=" + mScreenOffReason |
| 1164 | + " mUserState=" + mUserState); |
| 1165 | pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1] |
| 1166 | + ',' + mBroadcastQueue[2] + "}"); |
| 1167 | pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1] |
| 1168 | + ',' + mBroadcastWhy[2] + "}"); |
| 1169 | pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet); |
| 1170 | pw.println(" mKeyboardVisible=" + mKeyboardVisible |
| 1171 | + " mUserActivityAllowed=" + mUserActivityAllowed); |
| 1172 | pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay |
| 1173 | + " mScreenOffDelay=" + mScreenOffDelay); |
| 1174 | pw.println(" mPreventScreenOn=" + mPreventScreenOn |
| 1175 | + " mScreenBrightnessOverride=" + mScreenBrightnessOverride |
| 1176 | + " mButtonBrightnessOverride=" + mButtonBrightnessOverride); |
| 1177 | pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting |
| 1178 | + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout); |
| 1179 | pw.println(" mLastScreenOnTime=" + mLastScreenOnTime); |
| 1180 | pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock); |
| 1181 | pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock); |
| 1182 | pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock); |
| 1183 | pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock); |
| 1184 | pw.println(" mProximityPartialLock=" + mProximityPartialLock); |
| 1185 | pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount); |
| 1186 | pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled); |
| 1187 | pw.println(" mProximitySensorActive=" + mProximitySensorActive); |
| 1188 | pw.println(" mProximityPendingValue=" + mProximityPendingValue); |
| 1189 | pw.println(" mLastProximityEventTime=" + mLastProximityEventTime); |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 1190 | pw.println(" mLightSensorEnabled=" + mLightSensorEnabled |
| 1191 | + " mLightSensorAdjustSetting=" + mLightSensorAdjustSetting); |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1192 | pw.println(" mLightSensorValue=" + mLightSensorValue |
| 1193 | + " mLightSensorPendingValue=" + mLightSensorPendingValue); |
| Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 1194 | pw.println(" mLightSensorPendingDecrease=" + mLightSensorPendingDecrease |
| 1195 | + " mLightSensorPendingIncrease=" + mLightSensorPendingIncrease); |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1196 | pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness |
| 1197 | + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness |
| 1198 | + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness); |
| 1199 | pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness); |
| 1200 | pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled); |
| 1201 | mScreenBrightness.dump(pw, " mScreenBrightness: "); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1202 | |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1203 | int N = mLocks.size(); |
| 1204 | pw.println(); |
| 1205 | pw.println("mLocks.size=" + N + ":"); |
| 1206 | for (int i=0; i<N; i++) { |
| 1207 | WakeLock wl = mLocks.get(i); |
| 1208 | String type = lockType(wl.flags & LOCK_MASK); |
| 1209 | String acquireCausesWakeup = ""; |
| 1210 | if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) { |
| 1211 | acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP "; |
| 1212 | } |
| 1213 | String activated = ""; |
| 1214 | if (wl.activated) { |
| 1215 | activated = " activated"; |
| 1216 | } |
| 1217 | pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup |
| Mike Lockwood | f5bd092 | 2010-03-22 17:10:15 -0400 | [diff] [blame] | 1218 | + activated + " (minState=" + wl.minState + ", uid=" + wl.uid |
| 1219 | + ", pid=" + wl.pid + ")"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1220 | } |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1221 | |
| 1222 | pw.println(); |
| 1223 | pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":"); |
| 1224 | for (PokeLock p: mPokeLocks.values()) { |
| 1225 | pw.println(" poke lock '" + p.tag + "':" |
| Joe Onorato | 1a542c7 | 2010-11-08 09:48:20 -0800 | [diff] [blame] | 1226 | + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0 |
| 1227 | ? " POKE_LOCK_IGNORE_TOUCH_EVENTS" : "") |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1228 | + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0 |
| 1229 | ? " POKE_LOCK_SHORT_TIMEOUT" : "") |
| 1230 | + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0 |
| 1231 | ? " POKE_LOCK_MEDIUM_TIMEOUT" : "")); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1233 | |
| Mike Lockwood | ca44df8 | 2010-02-25 13:48:49 -0500 | [diff] [blame] | 1234 | pw.println(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1238 | private void setTimeoutLocked(long now, int nextState) { |
| 1239 | setTimeoutLocked(now, -1, nextState); |
| 1240 | } |
| 1241 | |
| 1242 | // If they gave a timeoutOverride it is the number of seconds |
| 1243 | // to screen-off. Figure out where in the countdown cycle we |
| 1244 | // should jump to. |
| Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1245 | private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) { |
| 1246 | long timeoutOverride = originalTimeoutOverride; |
| Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 1247 | if (mBootCompleted) { |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1248 | synchronized (mLocks) { |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1249 | long when = 0; |
| 1250 | if (timeoutOverride <= 0) { |
| 1251 | switch (nextState) |
| 1252 | { |
| 1253 | case SCREEN_BRIGHT: |
| 1254 | when = now + mKeylightDelay; |
| 1255 | break; |
| 1256 | case SCREEN_DIM: |
| 1257 | if (mDimDelay >= 0) { |
| 1258 | when = now + mDimDelay; |
| Andreas Huber | 84047bc | 2010-07-27 16:49:10 -0700 | [diff] [blame] | 1259 | break; |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1260 | } else { |
| 1261 | Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim"); |
| 1262 | } |
| 1263 | case SCREEN_OFF: |
| 1264 | synchronized (mLocks) { |
| 1265 | when = now + mScreenOffDelay; |
| 1266 | } |
| 1267 | break; |
| Andreas Huber | 84047bc | 2010-07-27 16:49:10 -0700 | [diff] [blame] | 1268 | default: |
| 1269 | when = now; |
| 1270 | break; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1271 | } |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1272 | } else { |
| 1273 | override: { |
| 1274 | if (timeoutOverride <= mScreenOffDelay) { |
| 1275 | when = now + timeoutOverride; |
| 1276 | nextState = SCREEN_OFF; |
| 1277 | break override; |
| 1278 | } |
| 1279 | timeoutOverride -= mScreenOffDelay; |
| 1280 | |
| 1281 | if (mDimDelay >= 0) { |
| 1282 | if (timeoutOverride <= mDimDelay) { |
| 1283 | when = now + timeoutOverride; |
| 1284 | nextState = SCREEN_DIM; |
| 1285 | break override; |
| 1286 | } |
| 1287 | timeoutOverride -= mDimDelay; |
| 1288 | } |
| 1289 | |
| 1290 | when = now + timeoutOverride; |
| 1291 | nextState = SCREEN_BRIGHT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | } |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1293 | } |
| 1294 | if (mSpew) { |
| 1295 | Slog.d(TAG, "setTimeoutLocked now=" + now |
| 1296 | + " timeoutOverride=" + timeoutOverride |
| 1297 | + " nextState=" + nextState + " when=" + when); |
| 1298 | } |
| Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1299 | |
| 1300 | mHandler.removeCallbacks(mTimeoutTask); |
| 1301 | mTimeoutTask.nextState = nextState; |
| 1302 | mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0 |
| 1303 | ? (originalTimeoutOverride - timeoutOverride) |
| 1304 | : -1; |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 1305 | mHandler.postAtTime(mTimeoutTask, when); |
| 1306 | mNextTimeout = when; // for debugging |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | private void cancelTimerLocked() |
| 1312 | { |
| 1313 | mHandler.removeCallbacks(mTimeoutTask); |
| 1314 | mTimeoutTask.nextState = -1; |
| 1315 | } |
| 1316 | |
| 1317 | private class TimeoutTask implements Runnable |
| 1318 | { |
| 1319 | int nextState; // access should be synchronized on mLocks |
| Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1320 | long remainingTimeoutOverride; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | public void run() |
| 1322 | { |
| 1323 | synchronized (mLocks) { |
| 1324 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1325 | Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | if (nextState == -1) { |
| 1329 | return; |
| 1330 | } |
| 1331 | |
| 1332 | mUserState = this.nextState; |
| 1333 | setPowerState(this.nextState | mWakeLockState); |
| 1334 | |
| 1335 | long now = SystemClock.uptimeMillis(); |
| 1336 | |
| 1337 | switch (this.nextState) |
| 1338 | { |
| 1339 | case SCREEN_BRIGHT: |
| 1340 | if (mDimDelay >= 0) { |
| Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1341 | setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | break; |
| 1343 | } |
| 1344 | case SCREEN_DIM: |
| Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 1345 | setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | break; |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1352 | private void sendNotificationLocked(boolean on, int why) { |
| 1353 | if (!mInitialized) { |
| 1354 | // No notifications sent until first initialization is done. |
| 1355 | // This is so that when we are moving from our initial state |
| 1356 | // which looks like the screen was off to it being on, we do not |
| 1357 | // go through the process of waiting for the higher-level user |
| 1358 | // space to be ready before turning up the display brightness. |
| 1359 | // (And also do not send needless broadcasts about the screen.) |
| 1360 | return; |
| 1361 | } |
| Dianne Hackborn | 4001109 | 2011-09-22 13:37:48 -0700 | [diff] [blame] | 1362 | |
| 1363 | if (DEBUG_SCREEN_ON) { |
| 1364 | RuntimeException here = new RuntimeException("here"); |
| 1365 | here.fillInStackTrace(); |
| 1366 | Slog.i(TAG, "sendNotificationLocked: " + on, here); |
| 1367 | } |
| 1368 | |
| Joe Onorato | 64c62ba | 2009-03-24 20:13:57 -0700 | [diff] [blame] | 1369 | if (!on) { |
| 1370 | mStillNeedSleepNotification = false; |
| 1371 | } |
| 1372 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1373 | // Add to the queue. |
| 1374 | int index = 0; |
| 1375 | while (mBroadcastQueue[index] != -1) { |
| 1376 | index++; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | } |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1378 | mBroadcastQueue[index] = on ? 1 : 0; |
| 1379 | mBroadcastWhy[index] = why; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1381 | // If we added it position 2, then there is a pair that can be stripped. |
| 1382 | // If we added it position 1 and we're turning the screen off, we can strip |
| 1383 | // the pair and do nothing, because the screen is already off, and therefore |
| 1384 | // keyguard has already been enabled. |
| 1385 | // However, if we added it at position 1 and we're turning it on, then position |
| 1386 | // 0 was to turn it off, and we can't strip that, because keyguard needs to come |
| 1387 | // on, so have to run the queue then. |
| 1388 | if (index == 2) { |
| Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1389 | // While we're collapsing them, if it's going off, and the new reason |
| 1390 | // is more significant than the first, then use the new one. |
| 1391 | if (!on && mBroadcastWhy[0] > why) { |
| 1392 | mBroadcastWhy[0] = why; |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1393 | } |
| 1394 | mBroadcastQueue[0] = on ? 1 : 0; |
| 1395 | mBroadcastQueue[1] = -1; |
| 1396 | mBroadcastQueue[2] = -1; |
| Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1397 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount); |
| Mike Lockwood | 9c90a37 | 2010-04-13 15:40:27 -0400 | [diff] [blame] | 1398 | mBroadcastWakeLock.release(); |
| Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1399 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount); |
| Mike Lockwood | 9c90a37 | 2010-04-13 15:40:27 -0400 | [diff] [blame] | 1400 | mBroadcastWakeLock.release(); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1401 | index = 0; |
| 1402 | } |
| 1403 | if (index == 1 && !on) { |
| 1404 | mBroadcastQueue[0] = -1; |
| 1405 | mBroadcastQueue[1] = -1; |
| 1406 | index = -1; |
| 1407 | // The wake lock was being held, but we're not actually going to do any |
| 1408 | // broadcasts, so release the wake lock. |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1409 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | mBroadcastWakeLock.release(); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
| Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 1413 | // The broadcast queue has changed; make sure the screen is on if it |
| 1414 | // is now possible for it to be. |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1415 | if (mSkippedScreenOn) { |
| 1416 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
| 1417 | } |
| Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 1418 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1419 | // Now send the message. |
| 1420 | if (index >= 0) { |
| 1421 | // Acquire the broadcast wake lock before changing the power |
| 1422 | // state. It will be release after the broadcast is sent. |
| 1423 | // We always increment the ref count for each notification in the queue |
| 1424 | // and always decrement when that notification is handled. |
| 1425 | mBroadcastWakeLock.acquire(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1426 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1427 | mHandler.post(mNotificationTask); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
| Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1431 | private WindowManagerPolicy.ScreenOnListener mScreenOnListener = |
| 1432 | new WindowManagerPolicy.ScreenOnListener() { |
| 1433 | @Override public void onScreenOn() { |
| 1434 | synchronized (mLocks) { |
| 1435 | if (mPreparingForScreenOn) { |
| 1436 | mPreparingForScreenOn = false; |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1437 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
| Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1438 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, |
| 1439 | 4, mBroadcastWakeLock.mCount); |
| 1440 | mBroadcastWakeLock.release(); |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | }; |
| 1445 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | private Runnable mNotificationTask = new Runnable() |
| 1447 | { |
| 1448 | public void run() |
| 1449 | { |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1450 | while (true) { |
| 1451 | int value; |
| 1452 | int why; |
| 1453 | WindowManagerPolicy policy; |
| 1454 | synchronized (mLocks) { |
| 1455 | value = mBroadcastQueue[0]; |
| 1456 | why = mBroadcastWhy[0]; |
| 1457 | for (int i=0; i<2; i++) { |
| 1458 | mBroadcastQueue[i] = mBroadcastQueue[i+1]; |
| 1459 | mBroadcastWhy[i] = mBroadcastWhy[i+1]; |
| 1460 | } |
| 1461 | policy = getPolicyLocked(); |
| Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1462 | if (value == 1 && !mPreparingForScreenOn) { |
| 1463 | mPreparingForScreenOn = true; |
| 1464 | mBroadcastWakeLock.acquire(); |
| 1465 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, |
| 1466 | mBroadcastWakeLock.mCount); |
| Dianne Hackborn | 29aae6f | 2011-08-18 18:30:09 -0700 | [diff] [blame] | 1467 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | } |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1469 | if (value == 1) { |
| 1470 | mScreenOnStart = SystemClock.uptimeMillis(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1471 | |
| Dianne Hackborn | 38e29a6 | 2011-09-18 14:43:08 -0700 | [diff] [blame] | 1472 | policy.screenTurningOn(mScreenOnListener); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1473 | try { |
| 1474 | ActivityManagerNative.getDefault().wakingUp(); |
| 1475 | } catch (RemoteException e) { |
| 1476 | // ignore it |
| 1477 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1479 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1480 | Slog.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1481 | } |
| 1482 | if (mContext != null && ActivityManagerNative.isSystemReady()) { |
| 1483 | mContext.sendOrderedBroadcast(mScreenOnIntent, null, |
| 1484 | mScreenOnBroadcastDone, mHandler, 0, null, null); |
| 1485 | } else { |
| 1486 | synchronized (mLocks) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1487 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2, |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1488 | mBroadcastWakeLock.mCount); |
| 1489 | mBroadcastWakeLock.release(); |
| 1490 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | } |
| 1492 | } |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1493 | else if (value == 0) { |
| 1494 | mScreenOffStart = SystemClock.uptimeMillis(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1495 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1496 | policy.screenTurnedOff(why); |
| 1497 | try { |
| 1498 | ActivityManagerNative.getDefault().goingToSleep(); |
| 1499 | } catch (RemoteException e) { |
| 1500 | // ignore it. |
| 1501 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1503 | if (mContext != null && ActivityManagerNative.isSystemReady()) { |
| 1504 | mContext.sendOrderedBroadcast(mScreenOffIntent, null, |
| 1505 | mScreenOffBroadcastDone, mHandler, 0, null, null); |
| 1506 | } else { |
| 1507 | synchronized (mLocks) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1508 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3, |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1509 | mBroadcastWakeLock.mCount); |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1510 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1511 | mBroadcastWakeLock.release(); |
| 1512 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1513 | } |
| 1514 | } |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1515 | else { |
| 1516 | // If we're in this case, then this handler is running for a previous |
| 1517 | // paired transaction. mBroadcastWakeLock will already have been released. |
| 1518 | break; |
| 1519 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | }; |
| 1523 | |
| 1524 | long mScreenOnStart; |
| 1525 | private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() { |
| 1526 | public void onReceive(Context context, Intent intent) { |
| 1527 | synchronized (mLocks) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1528 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount); |
| 1530 | mBroadcastWakeLock.release(); |
| 1531 | } |
| 1532 | } |
| 1533 | }; |
| 1534 | |
| 1535 | long mScreenOffStart; |
| 1536 | private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() { |
| 1537 | public void onReceive(Context context, Intent intent) { |
| 1538 | synchronized (mLocks) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1539 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1540 | SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount); |
| 1541 | mBroadcastWakeLock.release(); |
| 1542 | } |
| 1543 | } |
| 1544 | }; |
| 1545 | |
| 1546 | void logPointerUpEvent() { |
| 1547 | if (LOG_TOUCH_DOWNS) { |
| 1548 | mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown; |
| 1549 | mLastTouchDown = 0; |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | void logPointerDownEvent() { |
| 1554 | if (LOG_TOUCH_DOWNS) { |
| 1555 | // If we are not already timing a down/up sequence |
| 1556 | if (mLastTouchDown == 0) { |
| 1557 | mLastTouchDown = SystemClock.elapsedRealtime(); |
| 1558 | mTouchCycles++; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * Prevents the screen from turning on even if it *should* turn on due |
| 1565 | * to a subsequent full wake lock being acquired. |
| 1566 | * <p> |
| 1567 | * This is a temporary hack that allows an activity to "cover up" any |
| 1568 | * display glitches that happen during the activity's startup |
| 1569 | * sequence. (Specifically, this API was added to work around a |
| 1570 | * cosmetic bug in the "incoming call" sequence, where the lock screen |
| 1571 | * would flicker briefly before the incoming call UI became visible.) |
| 1572 | * TODO: There ought to be a more elegant way of doing this, |
| 1573 | * probably by having the PowerManager and ActivityManager |
| 1574 | * work together to let apps specify that the screen on/off |
| 1575 | * state should be synchronized with the Activity lifecycle. |
| 1576 | * <p> |
| 1577 | * Note that calling preventScreenOn(true) will NOT turn the screen |
| 1578 | * off if it's currently on. (This API only affects *future* |
| 1579 | * acquisitions of full wake locks.) |
| 1580 | * But calling preventScreenOn(false) WILL turn the screen on if |
| 1581 | * it's currently off because of a prior preventScreenOn(true) call. |
| 1582 | * <p> |
| 1583 | * Any call to preventScreenOn(true) MUST be followed promptly by a call |
| 1584 | * to preventScreenOn(false). In fact, if the preventScreenOn(false) |
| 1585 | * call doesn't occur within 5 seconds, we'll turn the screen back on |
| 1586 | * ourselves (and log a warning about it); this prevents a buggy app |
| 1587 | * from disabling the screen forever.) |
| 1588 | * <p> |
| 1589 | * TODO: this feature should really be controlled by a new type of poke |
| 1590 | * lock (rather than an IPowerManager call). |
| 1591 | */ |
| 1592 | public void preventScreenOn(boolean prevent) { |
| 1593 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1594 | |
| 1595 | synchronized (mLocks) { |
| 1596 | if (prevent) { |
| 1597 | // First of all, grab a partial wake lock to |
| 1598 | // make sure the CPU stays on during the entire |
| 1599 | // preventScreenOn(true) -> preventScreenOn(false) sequence. |
| 1600 | mPreventScreenOnPartialLock.acquire(); |
| 1601 | |
| 1602 | // Post a forceReenableScreen() call (for 5 seconds in the |
| 1603 | // future) to make sure the matching preventScreenOn(false) call |
| 1604 | // has happened by then. |
| 1605 | mHandler.removeCallbacks(mForceReenableScreenTask); |
| 1606 | mHandler.postDelayed(mForceReenableScreenTask, 5000); |
| 1607 | |
| 1608 | // Finally, set the flag that prevents the screen from turning on. |
| 1609 | // (Below, in setPowerState(), we'll check mPreventScreenOn and |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1610 | // we *won't* call setScreenStateLocked(true) if it's set.) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | mPreventScreenOn = true; |
| 1612 | } else { |
| 1613 | // (Re)enable the screen. |
| 1614 | mPreventScreenOn = false; |
| 1615 | |
| 1616 | // We're "undoing" a the prior preventScreenOn(true) call, so we |
| 1617 | // no longer need the 5-second safeguard. |
| 1618 | mHandler.removeCallbacks(mForceReenableScreenTask); |
| 1619 | |
| 1620 | // Forcibly turn on the screen if it's supposed to be on. (This |
| 1621 | // handles the case where the screen is currently off because of |
| 1622 | // a prior preventScreenOn(true) call.) |
| Mike Lockwood | e09028142 | 2009-11-14 21:02:56 -0500 | [diff] [blame] | 1623 | if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1625 | Slog.d(TAG, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | "preventScreenOn: turning on after a prior preventScreenOn(true)!"); |
| 1627 | } |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1628 | int err = setScreenStateLocked(true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | if (err != 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1630 | Slog.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | // Release the partial wake lock that we held during the |
| 1635 | // preventScreenOn(true) -> preventScreenOn(false) sequence. |
| 1636 | mPreventScreenOnPartialLock.release(); |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | public void setScreenBrightnessOverride(int brightness) { |
| 1642 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1643 | |
| Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1644 | if (mSpew) Slog.d(TAG, "setScreenBrightnessOverride " + brightness); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | synchronized (mLocks) { |
| 1646 | if (mScreenBrightnessOverride != brightness) { |
| 1647 | mScreenBrightnessOverride = brightness; |
| Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1648 | if (isScreenOn()) { |
| 1649 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
| 1650 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | } |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1654 | |
| 1655 | public void setButtonBrightnessOverride(int brightness) { |
| 1656 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1657 | |
| Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1658 | if (mSpew) Slog.d(TAG, "setButtonBrightnessOverride " + brightness); |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1659 | synchronized (mLocks) { |
| 1660 | if (mButtonBrightnessOverride != brightness) { |
| 1661 | mButtonBrightnessOverride = brightness; |
| Mike Lockwood | f527c71 | 2010-06-10 14:12:33 -0400 | [diff] [blame] | 1662 | if (isScreenOn()) { |
| 1663 | updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT); |
| 1664 | } |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1669 | /** |
| 1670 | * Sanity-check that gets called 5 seconds after any call to |
| 1671 | * preventScreenOn(true). This ensures that the original call |
| 1672 | * is followed promptly by a call to preventScreenOn(false). |
| 1673 | */ |
| 1674 | private void forceReenableScreen() { |
| 1675 | // We shouldn't get here at all if mPreventScreenOn is false, since |
| 1676 | // we should have already removed any existing |
| 1677 | // mForceReenableScreenTask messages... |
| 1678 | if (!mPreventScreenOn) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1679 | Slog.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1680 | return; |
| 1681 | } |
| 1682 | |
| 1683 | // Uh oh. It's been 5 seconds since a call to |
| 1684 | // preventScreenOn(true) and we haven't re-enabled the screen yet. |
| 1685 | // This means the app that called preventScreenOn(true) is either |
| 1686 | // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)), |
| 1687 | // or buggy (i.e. it forgot to call preventScreenOn(false), or |
| 1688 | // crashed before doing so.) |
| 1689 | |
| 1690 | // Log a warning, and forcibly turn the screen back on. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1691 | Slog.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | + "Forcing the screen back on..."); |
| 1693 | preventScreenOn(false); |
| 1694 | } |
| 1695 | |
| 1696 | private Runnable mForceReenableScreenTask = new Runnable() { |
| 1697 | public void run() { |
| 1698 | forceReenableScreen(); |
| 1699 | } |
| 1700 | }; |
| 1701 | |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1702 | private int setScreenStateLocked(boolean on) { |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1703 | if (DEBUG_SCREEN_ON) { |
| 1704 | RuntimeException e = new RuntimeException("here"); |
| 1705 | e.fillInStackTrace(); |
| 1706 | Slog.i(TAG, "Set screen state: " + on, e); |
| 1707 | } |
| Dianne Hackborn | 474fd74 | 2011-10-10 18:40:22 -0700 | [diff] [blame] | 1708 | if (on) { |
| 1709 | if ((mPowerState & SCREEN_ON_BIT) == 0 || mSkippedScreenOn) { |
| 1710 | // If we are turning the screen state on, but the screen |
| 1711 | // light is currently off, then make sure that we set the |
| 1712 | // light at this point to 0. This is the case where we are |
| 1713 | // turning on the screen and waiting for the UI to be drawn |
| 1714 | // before showing it to the user. We want the light off |
| 1715 | // until it is ready to be shown to the user, not it using |
| 1716 | // whatever the last value it had. |
| Dianne Hackborn | 81de8b9 | 2011-11-28 16:54:31 -0800 | [diff] [blame] | 1717 | if (DEBUG_SCREEN_ON) { |
| 1718 | Slog.i(TAG, "Forcing brightness 0: mPowerState=0x" |
| 1719 | + Integer.toHexString(mPowerState) |
| 1720 | + " mSkippedScreenOn=" + mSkippedScreenOn); |
| 1721 | } |
| Dianne Hackborn | 474fd74 | 2011-10-10 18:40:22 -0700 | [diff] [blame] | 1722 | mScreenBrightness.forceValueLocked(Power.BRIGHTNESS_OFF); |
| 1723 | } |
| 1724 | } |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1725 | int err = Power.setScreenState(on); |
| Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1726 | if (err == 0) { |
| 1727 | mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0); |
| 1728 | if (mUseSoftwareAutoBrightness) { |
| Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 1729 | enableLightSensorLocked(on); |
| Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1730 | if (!on) { |
| 1731 | // make sure button and key backlights are off too |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1732 | mButtonLight.turnOff(); |
| 1733 | mKeyboardLight.turnOff(); |
| Mike Lockwood | 20ee6f2 | 2009-11-07 20:33:47 -0500 | [diff] [blame] | 1734 | } |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 1735 | } |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1736 | } |
| 1737 | return err; |
| 1738 | } |
| 1739 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | private void setPowerState(int state) |
| 1741 | { |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1742 | setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1745 | private void setPowerState(int newState, boolean noChangeLights, int reason) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | { |
| 1747 | synchronized (mLocks) { |
| 1748 | int err; |
| 1749 | |
| 1750 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1751 | Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1752 | + " newState=0x" + Integer.toHexString(newState) |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1753 | + " noChangeLights=" + noChangeLights |
| 1754 | + " reason=" + reason); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | if (noChangeLights) { |
| 1758 | newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK); |
| 1759 | } |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 1760 | if (mProximitySensorActive) { |
| 1761 | // don't turn on the screen when the proximity sensor lock is held |
| 1762 | newState = (newState & ~SCREEN_BRIGHT); |
| 1763 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1764 | |
| 1765 | if (batteryIsLow()) { |
| 1766 | newState |= BATTERY_LOW_BIT; |
| 1767 | } else { |
| 1768 | newState &= ~BATTERY_LOW_BIT; |
| 1769 | } |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1770 | if (newState == mPowerState && mInitialized) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | return; |
| 1772 | } |
| Mike Lockwood | 3333fa4 | 2009-10-26 14:50:42 -0400 | [diff] [blame] | 1773 | |
| Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 1774 | if (!mBootCompleted && !mUseSoftwareAutoBrightness) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1775 | newState |= ALL_BRIGHT; |
| 1776 | } |
| 1777 | |
| 1778 | boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0; |
| 1779 | boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0; |
| 1780 | |
| Mike Lockwood | 51b84496 | 2009-11-16 21:51:18 -0500 | [diff] [blame] | 1781 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1782 | Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | + " newState=" + newState + " noChangeLights=" + noChangeLights); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1784 | Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1785 | + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0)); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1786 | Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1787 | + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0)); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1788 | Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1789 | + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0)); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1790 | Slog.d(TAG, " oldScreenOn=" + oldScreenOn |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | + " newScreenOn=" + newScreenOn); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1792 | Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1793 | + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0)); |
| 1794 | } |
| 1795 | |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1796 | final boolean stateChanged = mPowerState != newState; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1797 | |
| 1798 | if (oldScreenOn != newScreenOn) { |
| 1799 | if (newScreenOn) { |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1800 | // When the user presses the power button, we need to always send out the |
| 1801 | // notification that it's going to sleep so the keyguard goes on. But |
| 1802 | // we can't do that until the screen fades out, so we don't show the keyguard |
| 1803 | // too early. |
| 1804 | if (mStillNeedSleepNotification) { |
| 1805 | sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
| 1806 | } |
| 1807 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1808 | // Turn on the screen UNLESS there was a prior |
| 1809 | // preventScreenOn(true) request. (Note that the lifetime |
| 1810 | // of a single preventScreenOn() request is limited to 5 |
| 1811 | // seconds to prevent a buggy app from disabling the |
| 1812 | // screen forever; see forceReenableScreen().) |
| 1813 | boolean reallyTurnScreenOn = true; |
| 1814 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1815 | Slog.d(TAG, "- turning screen on... mPreventScreenOn = " |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1816 | + mPreventScreenOn); |
| 1817 | } |
| 1818 | |
| 1819 | if (mPreventScreenOn) { |
| 1820 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1821 | Slog.d(TAG, "- PREVENTING screen from really turning on!"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | } |
| 1823 | reallyTurnScreenOn = false; |
| 1824 | } |
| 1825 | if (reallyTurnScreenOn) { |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1826 | err = setScreenStateLocked(true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | long identity = Binder.clearCallingIdentity(); |
| 1828 | try { |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 1829 | mBatteryStats.noteScreenBrightness(getPreferredBrightness()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1830 | mBatteryStats.noteScreenOn(); |
| 1831 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1832 | Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | } finally { |
| 1834 | Binder.restoreCallingIdentity(identity); |
| 1835 | } |
| 1836 | } else { |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1837 | setScreenStateLocked(false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1838 | // But continue as if we really did turn the screen on... |
| 1839 | err = 0; |
| 1840 | } |
| 1841 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1842 | mLastTouchDown = 0; |
| 1843 | mTotalTouchDownTime = 0; |
| 1844 | mTouchCycles = 0; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1845 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1846 | mTotalTouchDownTime, mTouchCycles); |
| 1847 | if (err == 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1848 | sendNotificationLocked(true, -1); |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1849 | // Update the lights *after* taking care of turning the |
| 1850 | // screen on, so we do this after our notifications are |
| 1851 | // enqueued and thus will delay turning on the screen light |
| 1852 | // until the windows are correctly displayed. |
| 1853 | if (stateChanged) { |
| 1854 | updateLightsLocked(newState, 0); |
| 1855 | } |
| 1856 | mPowerState |= SCREEN_ON_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | } |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1858 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | } else { |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1860 | // Update the lights *before* taking care of turning the |
| 1861 | // screen off, so we can initiate any animations that are desired. |
| 1862 | if (stateChanged) { |
| 1863 | updateLightsLocked(newState, 0); |
| 1864 | } |
| 1865 | |
| Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 1866 | // cancel light sensor task |
| 1867 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 1868 | mLightSensorPendingDecrease = false; |
| 1869 | mLightSensorPendingIncrease = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1870 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 1871 | long identity = Binder.clearCallingIdentity(); |
| 1872 | try { |
| 1873 | mBatteryStats.noteScreenOff(); |
| 1874 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1875 | Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | } finally { |
| 1877 | Binder.restoreCallingIdentity(identity); |
| 1878 | } |
| 1879 | mPowerState &= ~SCREEN_ON_BIT; |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1880 | mScreenOffReason = reason; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1881 | if (!mScreenBrightness.animating) { |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1882 | err = screenOffFinishedAnimatingLocked(reason); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | } else { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1884 | err = 0; |
| 1885 | mLastTouchDown = 0; |
| 1886 | } |
| 1887 | } |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1888 | } else if (stateChanged) { |
| 1889 | // Screen on/off didn't change, but lights may have. |
| 1890 | updateLightsLocked(newState, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | } |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1892 | |
| 1893 | mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK); |
| 1894 | |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1895 | updateNativePowerStateLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1896 | } |
| 1897 | } |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1898 | |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1899 | private void updateNativePowerStateLocked() { |
| Mike Lockwood | 3a74bd3 | 2011-08-12 13:55:22 -0700 | [diff] [blame] | 1900 | if (!mHeadless) { |
| 1901 | nativeSetPowerState( |
| 1902 | (mPowerState & SCREEN_ON_BIT) != 0, |
| 1903 | (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT); |
| 1904 | } |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1905 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1906 | |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1907 | private int screenOffFinishedAnimatingLocked(int reason) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1908 | // I don't think we need to check the current state here because all of these |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1909 | // Power.setScreenState and sendNotificationLocked can both handle being |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1910 | // called multiple times in the same state. -joeo |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1911 | EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime, |
| 1912 | mTouchCycles); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1913 | mLastTouchDown = 0; |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 1914 | int err = setScreenStateLocked(false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1915 | if (err == 0) { |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 1916 | mScreenOffReason = reason; |
| 1917 | sendNotificationLocked(false, reason); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1918 | } |
| 1919 | return err; |
| 1920 | } |
| 1921 | |
| 1922 | private boolean batteryIsLow() { |
| 1923 | return (!mIsPowered && |
| 1924 | mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD); |
| 1925 | } |
| 1926 | |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1927 | private boolean shouldDeferScreenOnLocked() { |
| 1928 | if (mPreparingForScreenOn) { |
| 1929 | // Currently waiting for confirmation from the policy that it |
| 1930 | // is okay to turn on the screen. Don't allow the screen to go |
| 1931 | // on until that is done. |
| 1932 | if (DEBUG_SCREEN_ON) Slog.i(TAG, |
| 1933 | "updateLights: delaying screen on due to mPreparingForScreenOn"); |
| 1934 | return true; |
| 1935 | } else { |
| 1936 | // If there is a screen-on command in the notification queue, we |
| 1937 | // can't turn the screen on until it has been processed (and we |
| 1938 | // have set mPreparingForScreenOn) or it has been dropped. |
| 1939 | for (int i=0; i<mBroadcastQueue.length; i++) { |
| 1940 | if (mBroadcastQueue[i] == 1) { |
| 1941 | if (DEBUG_SCREEN_ON) Slog.i(TAG, |
| 1942 | "updateLights: delaying screen on due to notification queue"); |
| 1943 | return true; |
| 1944 | } |
| 1945 | } |
| 1946 | } |
| 1947 | return false; |
| 1948 | } |
| 1949 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1950 | private void updateLightsLocked(int newState, int forceState) { |
| Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1951 | final int oldState = mPowerState; |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 1952 | |
| 1953 | // If the screen is not currently on, we will want to delay actually |
| 1954 | // turning the lights on if we are still getting the UI put up. |
| 1955 | if ((oldState&SCREEN_ON_BIT) == 0 || mSkippedScreenOn) { |
| 1956 | // Don't turn screen on until we know we are really ready to. |
| 1957 | // This is to avoid letting the screen go on before things like the |
| 1958 | // lock screen have been displayed. |
| 1959 | if ((mSkippedScreenOn=shouldDeferScreenOnLocked())) { |
| 1960 | newState &= ~(SCREEN_ON_BIT|SCREEN_BRIGHT_BIT); |
| 1961 | } |
| 1962 | } |
| 1963 | |
| Joe Onorato | 60607a90 | 2010-10-23 14:49:30 -0700 | [diff] [blame] | 1964 | if ((newState & SCREEN_ON_BIT) != 0) { |
| 1965 | // Only turn on the buttons or keyboard if the screen is also on. |
| 1966 | // We should never see the buttons on but not the screen. |
| 1967 | newState = applyButtonState(newState); |
| 1968 | newState = applyKeyboardState(newState); |
| 1969 | } |
| Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1970 | final int realDifference = (newState ^ oldState); |
| 1971 | final int difference = realDifference | forceState; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1972 | if (difference == 0) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1973 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1974 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1975 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | int offMask = 0; |
| 1977 | int dimMask = 0; |
| 1978 | int onMask = 0; |
| 1979 | |
| 1980 | int preferredBrightness = getPreferredBrightness(); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1981 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1982 | if ((difference & KEYBOARD_BRIGHT_BIT) != 0) { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1983 | if ((newState & KEYBOARD_BRIGHT_BIT) == 0) { |
| 1984 | offMask |= KEYBOARD_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1985 | } else { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1986 | onMask |= KEYBOARD_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | if ((difference & BUTTON_BRIGHT_BIT) != 0) { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1991 | if ((newState & BUTTON_BRIGHT_BIT) == 0) { |
| 1992 | offMask |= BUTTON_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1993 | } else { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1994 | onMask |= BUTTON_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 1999 | int nominalCurrentValue = -1; |
| 2000 | // If there was an actual difference in the light state, then |
| 2001 | // figure out the "ideal" current value based on the previous |
| 2002 | // state. Otherwise, this is a change due to the brightness |
| 2003 | // override, so we want to animate from whatever the current |
| 2004 | // value is. |
| 2005 | if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) { |
| 2006 | switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) { |
| 2007 | case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT: |
| 2008 | nominalCurrentValue = preferredBrightness; |
| 2009 | break; |
| 2010 | case SCREEN_ON_BIT: |
| Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2011 | nominalCurrentValue = mScreenBrightnessDim; |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2012 | break; |
| 2013 | case 0: |
| 2014 | nominalCurrentValue = Power.BRIGHTNESS_OFF; |
| 2015 | break; |
| 2016 | case SCREEN_BRIGHT_BIT: |
| 2017 | default: |
| 2018 | // not possible |
| 2019 | nominalCurrentValue = (int)mScreenBrightness.curValue; |
| 2020 | break; |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2021 | } |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2022 | } |
| 2023 | int brightness = preferredBrightness; |
| 2024 | int steps = ANIM_STEPS; |
| 2025 | if ((newState & SCREEN_BRIGHT_BIT) == 0) { |
| 2026 | // dim or turn off backlight, depending on if the screen is on |
| 2027 | // the scale is because the brightness ramp isn't linear and this biases |
| 2028 | // it so the later parts take longer. |
| 2029 | final float scale = 1.5f; |
| Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2030 | float ratio = (((float)mScreenBrightnessDim)/preferredBrightness); |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2031 | if (ratio > 1.0f) ratio = 1.0f; |
| 2032 | if ((newState & SCREEN_ON_BIT) == 0) { |
| 2033 | if ((oldState & SCREEN_BRIGHT_BIT) != 0) { |
| 2034 | // was bright |
| 2035 | steps = ANIM_STEPS; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2036 | } else { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2037 | // was dim |
| 2038 | steps = (int)(ANIM_STEPS*ratio*scale); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2039 | } |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2040 | brightness = Power.BRIGHTNESS_OFF; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2041 | } else { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2042 | if ((oldState & SCREEN_ON_BIT) != 0) { |
| 2043 | // was bright |
| 2044 | steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale); |
| 2045 | } else { |
| 2046 | // was dim |
| 2047 | steps = (int)(ANIM_STEPS*ratio); |
| 2048 | } |
| 2049 | if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) { |
| 2050 | // If the "stay on while plugged in" option is |
| 2051 | // turned on, then the screen will often not |
| 2052 | // automatically turn off while plugged in. To |
| 2053 | // still have a sense of when it is inactive, we |
| 2054 | // will then count going dim as turning off. |
| 2055 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 2056 | } |
| Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2057 | brightness = mScreenBrightnessDim; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2058 | } |
| 2059 | } |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2060 | long identity = Binder.clearCallingIdentity(); |
| 2061 | try { |
| 2062 | mBatteryStats.noteScreenBrightness(brightness); |
| 2063 | } catch (RemoteException e) { |
| 2064 | // Nothing interesting to do. |
| 2065 | } finally { |
| 2066 | Binder.restoreCallingIdentity(identity); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2067 | } |
| Dianne Hackborn | 81de8b9 | 2011-11-28 16:54:31 -0800 | [diff] [blame] | 2068 | if (!mSkippedScreenOn) { |
| 2069 | mScreenBrightness.setTargetLocked(brightness, steps, |
| 2070 | INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue); |
| 2071 | if (DEBUG_SCREEN_ON) { |
| 2072 | RuntimeException e = new RuntimeException("here"); |
| 2073 | e.fillInStackTrace(); |
| 2074 | Slog.i(TAG, "Setting screen brightness: " + brightness, e); |
| 2075 | } |
| Dianne Hackborn | beae3bd | 2011-09-21 10:55:12 -0700 | [diff] [blame] | 2076 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2077 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2078 | |
| Joe Onorato | 60607a90 | 2010-10-23 14:49:30 -0700 | [diff] [blame] | 2079 | if (mSpew) { |
| 2080 | Slog.d(TAG, "offMask=0x" + Integer.toHexString(offMask) |
| 2081 | + " dimMask=0x" + Integer.toHexString(dimMask) |
| 2082 | + " onMask=0x" + Integer.toHexString(onMask) |
| 2083 | + " difference=0x" + Integer.toHexString(difference) |
| 2084 | + " realDifference=0x" + Integer.toHexString(realDifference) |
| 2085 | + " forceState=0x" + Integer.toHexString(forceState) |
| 2086 | ); |
| 2087 | } |
| 2088 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2089 | if (offMask != 0) { |
| Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2090 | if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2091 | setLightBrightness(offMask, Power.BRIGHTNESS_OFF); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2092 | } |
| 2093 | if (dimMask != 0) { |
| Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2094 | int brightness = mScreenBrightnessDim; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2095 | if ((newState & BATTERY_LOW_BIT) != 0 && |
| 2096 | brightness > Power.BRIGHTNESS_LOW_BATTERY) { |
| 2097 | brightness = Power.BRIGHTNESS_LOW_BATTERY; |
| 2098 | } |
| Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2099 | if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2100 | setLightBrightness(dimMask, brightness); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2101 | } |
| 2102 | if (onMask != 0) { |
| 2103 | int brightness = getPreferredBrightness(); |
| 2104 | if ((newState & BATTERY_LOW_BIT) != 0 && |
| 2105 | brightness > Power.BRIGHTNESS_LOW_BATTERY) { |
| 2106 | brightness = Power.BRIGHTNESS_LOW_BATTERY; |
| 2107 | } |
| Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2108 | if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2109 | setLightBrightness(onMask, brightness); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2110 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2111 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2112 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2113 | private void setLightBrightness(int mask, int value) { |
| Mike Lockwood | cc9a63d | 2009-11-10 07:50:28 -0500 | [diff] [blame] | 2114 | int brightnessMode = (mAutoBrightessEnabled |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 2115 | ? LightsService.BRIGHTNESS_MODE_SENSOR |
| 2116 | : LightsService.BRIGHTNESS_MODE_USER); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2117 | if ((mask & SCREEN_BRIGHT_BIT) != 0) { |
| Dianne Hackborn | 81de8b9 | 2011-11-28 16:54:31 -0800 | [diff] [blame] | 2118 | if (DEBUG_SCREEN_ON) { |
| 2119 | RuntimeException e = new RuntimeException("here"); |
| 2120 | e.fillInStackTrace(); |
| 2121 | Slog.i(TAG, "Set LCD brightness: " + value, e); |
| 2122 | } |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2123 | mLcdLight.setBrightness(value, brightnessMode); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2124 | } |
| 2125 | if ((mask & BUTTON_BRIGHT_BIT) != 0) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2126 | mButtonLight.setBrightness(value); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2127 | } |
| 2128 | if ((mask & KEYBOARD_BRIGHT_BIT) != 0) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2129 | mKeyboardLight.setBrightness(value); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2130 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2131 | } |
| 2132 | |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2133 | class BrightnessState implements Runnable { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2134 | final int mask; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2135 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2136 | boolean initialized; |
| 2137 | int targetValue; |
| 2138 | float curValue; |
| 2139 | float delta; |
| 2140 | boolean animating; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2141 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2142 | BrightnessState(int m) { |
| 2143 | mask = m; |
| 2144 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2145 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2146 | public void dump(PrintWriter pw, String prefix) { |
| 2147 | pw.println(prefix + "animating=" + animating |
| 2148 | + " targetValue=" + targetValue |
| 2149 | + " curValue=" + curValue |
| 2150 | + " delta=" + delta); |
| 2151 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2152 | |
| Dianne Hackborn | 474fd74 | 2011-10-10 18:40:22 -0700 | [diff] [blame] | 2153 | void forceValueLocked(int value) { |
| 2154 | targetValue = -1; |
| 2155 | curValue = value; |
| 2156 | setLightBrightness(mask, value); |
| 2157 | if (animating) { |
| 2158 | finishAnimationLocked(false, value); |
| 2159 | } |
| 2160 | } |
| 2161 | |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2162 | void setTargetLocked(int target, int stepsToTarget, int initialValue, |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2163 | int nominalCurrentValue) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2164 | if (!initialized) { |
| 2165 | initialized = true; |
| 2166 | curValue = (float)initialValue; |
| Dianne Hackborn | aa80b60 | 2009-10-09 17:38:26 -0700 | [diff] [blame] | 2167 | } else if (targetValue == target) { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2168 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2169 | } |
| 2170 | targetValue = target; |
| Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 2171 | delta = (targetValue - |
| 2172 | (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue)) |
| 2173 | / stepsToTarget; |
| Dianne Hackborn | 81de8b9 | 2011-11-28 16:54:31 -0800 | [diff] [blame] | 2174 | if (mSpew || DEBUG_SCREEN_ON) { |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2175 | String noticeMe = nominalCurrentValue == curValue ? "" : " ******************"; |
| Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2176 | Slog.i(TAG, "setTargetLocked mask=" + mask + " curValue=" + curValue |
| 2177 | + " target=" + target + " targetValue=" + targetValue + " delta=" + delta |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2178 | + " nominalCurrentValue=" + nominalCurrentValue |
| 2179 | + noticeMe); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2180 | } |
| 2181 | animating = true; |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2182 | |
| 2183 | if (mSpew) { |
| 2184 | Slog.i(TAG, "scheduling light animator"); |
| 2185 | } |
| 2186 | mScreenOffHandler.removeCallbacks(this); |
| 2187 | mScreenOffHandler.post(this); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2188 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2189 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2190 | boolean stepLocked() { |
| 2191 | if (!animating) return false; |
| 2192 | if (false && mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2193 | Slog.i(TAG, "Step target " + mask + ": cur=" + curValue |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2194 | + " target=" + targetValue + " delta=" + delta); |
| 2195 | } |
| 2196 | curValue += delta; |
| 2197 | int curIntValue = (int)curValue; |
| 2198 | boolean more = true; |
| 2199 | if (delta == 0) { |
| Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 2200 | curValue = curIntValue = targetValue; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2201 | more = false; |
| 2202 | } else if (delta > 0) { |
| 2203 | if (curIntValue >= targetValue) { |
| 2204 | curValue = curIntValue = targetValue; |
| 2205 | more = false; |
| 2206 | } |
| 2207 | } else { |
| 2208 | if (curIntValue <= targetValue) { |
| 2209 | curValue = curIntValue = targetValue; |
| 2210 | more = false; |
| 2211 | } |
| 2212 | } |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2213 | if (mSpew) Slog.d(TAG, "Animating curIntValue=" + curIntValue + ": " + mask); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2214 | setLightBrightness(mask, curIntValue); |
| Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2215 | finishAnimationLocked(more, curIntValue); |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2216 | return more; |
| 2217 | } |
| 2218 | |
| Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2219 | void jumpToTargetLocked() { |
| 2220 | if (mSpew) Slog.d(TAG, "jumpToTargetLocked targetValue=" + targetValue + ": " + mask); |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2221 | setLightBrightness(mask, targetValue); |
| 2222 | final int tv = targetValue; |
| 2223 | curValue = tv; |
| 2224 | targetValue = -1; |
| Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2225 | finishAnimationLocked(false, tv); |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
| Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2228 | private void finishAnimationLocked(boolean more, int curIntValue) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2229 | animating = more; |
| 2230 | if (!more) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2231 | if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) { |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2232 | screenOffFinishedAnimatingLocked(mScreenOffReason); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2233 | } |
| 2234 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2235 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2236 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | public void run() { |
| Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 2238 | synchronized (mLocks) { |
| 2239 | // we're turning off |
| 2240 | final boolean turningOff = animating && targetValue == Power.BRIGHTNESS_OFF; |
| 2241 | if (mAnimateScreenLights || !turningOff) { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2242 | long now = SystemClock.uptimeMillis(); |
| 2243 | boolean more = mScreenBrightness.stepLocked(); |
| 2244 | if (more) { |
| 2245 | mScreenOffHandler.postAtTime(this, now+(1000/60)); |
| 2246 | } |
| Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 2247 | } else { |
| 2248 | // It's pretty scary to hold mLocks for this long, and we should |
| 2249 | // redesign this, but it works for now. |
| 2250 | nativeStartSurfaceFlingerAnimation( |
| 2251 | mScreenOffReason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR |
| 2252 | ? 0 : mAnimationSetting); |
| Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 2253 | mScreenBrightness.jumpToTargetLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2254 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2255 | } |
| 2256 | } |
| 2257 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2258 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2259 | private int getPreferredBrightness() { |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2260 | if (mScreenBrightnessOverride >= 0) { |
| 2261 | return mScreenBrightnessOverride; |
| 2262 | } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness |
| 2263 | && mAutoBrightessEnabled) { |
| 2264 | return mLightSensorScreenBrightness; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2265 | } |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2266 | final int brightness = mScreenBrightnessSetting; |
| 2267 | // Don't let applications turn the screen all the way off |
| 2268 | return Math.max(brightness, mScreenBrightnessDim); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2269 | } |
| 2270 | |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2271 | private int applyButtonState(int state) { |
| 2272 | int brightness = -1; |
| Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2273 | if ((state & BATTERY_LOW_BIT) != 0) { |
| 2274 | // do not override brightness if the battery is low |
| 2275 | return state; |
| 2276 | } |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2277 | if (mButtonBrightnessOverride >= 0) { |
| 2278 | brightness = mButtonBrightnessOverride; |
| 2279 | } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) { |
| 2280 | brightness = mLightSensorButtonBrightness; |
| 2281 | } |
| 2282 | if (brightness > 0) { |
| 2283 | return state | BUTTON_BRIGHT_BIT; |
| 2284 | } else if (brightness == 0) { |
| 2285 | return state & ~BUTTON_BRIGHT_BIT; |
| 2286 | } else { |
| 2287 | return state; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | private int applyKeyboardState(int state) { |
| 2292 | int brightness = -1; |
| Mike Lockwood | 48358bd | 2010-04-17 22:29:20 -0400 | [diff] [blame] | 2293 | if ((state & BATTERY_LOW_BIT) != 0) { |
| 2294 | // do not override brightness if the battery is low |
| 2295 | return state; |
| 2296 | } |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2297 | if (!mKeyboardVisible) { |
| 2298 | brightness = 0; |
| 2299 | } else if (mButtonBrightnessOverride >= 0) { |
| 2300 | brightness = mButtonBrightnessOverride; |
| 2301 | } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) { |
| 2302 | brightness = mLightSensorKeyboardBrightness; |
| 2303 | } |
| 2304 | if (brightness > 0) { |
| 2305 | return state | KEYBOARD_BRIGHT_BIT; |
| 2306 | } else if (brightness == 0) { |
| 2307 | return state & ~KEYBOARD_BRIGHT_BIT; |
| 2308 | } else { |
| 2309 | return state; |
| 2310 | } |
| 2311 | } |
| 2312 | |
| Charles Mendis | 322591c | 2009-10-29 11:06:59 -0700 | [diff] [blame] | 2313 | public boolean isScreenOn() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2314 | synchronized (mLocks) { |
| 2315 | return (mPowerState & SCREEN_ON_BIT) != 0; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| Charles Mendis | 322591c | 2009-10-29 11:06:59 -0700 | [diff] [blame] | 2319 | boolean isScreenBright() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2320 | synchronized (mLocks) { |
| 2321 | return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT; |
| 2322 | } |
| 2323 | } |
| 2324 | |
| Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 2325 | private boolean isScreenTurningOffLocked() { |
| 2326 | return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0); |
| 2327 | } |
| 2328 | |
| Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 2329 | private boolean shouldLog(long time) { |
| 2330 | synchronized (mLocks) { |
| 2331 | if (time > (mWarningSpewThrottleTime + (60*60*1000))) { |
| 2332 | mWarningSpewThrottleTime = time; |
| 2333 | mWarningSpewThrottleCount = 0; |
| 2334 | return true; |
| 2335 | } else if (mWarningSpewThrottleCount < 30) { |
| 2336 | mWarningSpewThrottleCount++; |
| 2337 | return true; |
| 2338 | } else { |
| 2339 | return false; |
| 2340 | } |
| 2341 | } |
| 2342 | } |
| 2343 | |
| Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2344 | private void forceUserActivityLocked() { |
| Mike Lockwood | e09028142 | 2009-11-14 21:02:56 -0500 | [diff] [blame] | 2345 | if (isScreenTurningOffLocked()) { |
| 2346 | // cancel animation so userActivity will succeed |
| 2347 | mScreenBrightness.animating = false; |
| 2348 | } |
| Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 2349 | boolean savedActivityAllowed = mUserActivityAllowed; |
| 2350 | mUserActivityAllowed = true; |
| 2351 | userActivity(SystemClock.uptimeMillis(), false); |
| 2352 | mUserActivityAllowed = savedActivityAllowed; |
| 2353 | } |
| 2354 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2355 | public void userActivityWithForce(long time, boolean noChangeLights, boolean force) { |
| 2356 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2357 | userActivity(time, -1, noChangeLights, OTHER_EVENT, force); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | public void userActivity(long time, boolean noChangeLights) { |
| Joe Onorato | 4b9f62d | 2010-10-11 13:41:35 -0700 | [diff] [blame] | 2361 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER) |
| 2362 | != PackageManager.PERMISSION_GRANTED) { |
| 2363 | if (shouldLog(time)) { |
| 2364 | Slog.w(TAG, "Caller does not have DEVICE_POWER permission. pid=" |
| 2365 | + Binder.getCallingPid() + " uid=" + Binder.getCallingUid()); |
| 2366 | } |
| 2367 | return; |
| 2368 | } |
| 2369 | |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2370 | userActivity(time, -1, noChangeLights, OTHER_EVENT, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | public void userActivity(long time, boolean noChangeLights, int eventType) { |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2374 | userActivity(time, -1, noChangeLights, eventType, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2375 | } |
| 2376 | |
| 2377 | public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) { |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2378 | userActivity(time, -1, noChangeLights, eventType, force); |
| 2379 | } |
| 2380 | |
| 2381 | /* |
| 2382 | * Reset the user activity timeout to now + timeout. This overrides whatever else is going |
| 2383 | * on with user activity. Don't use this function. |
| 2384 | */ |
| 2385 | public void clearUserActivityTimeout(long now, long timeout) { |
| 2386 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2387 | Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now"); |
| 2388 | userActivity(now, timeout, false, OTHER_EVENT, false); |
| 2389 | } |
| 2390 | |
| 2391 | private void userActivity(long time, long timeoutOverride, boolean noChangeLights, |
| 2392 | int eventType, boolean force) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2393 | |
| Joe Onorato | 1a542c7 | 2010-11-08 09:48:20 -0800 | [diff] [blame] | 2394 | if (((mPokey & POKE_LOCK_IGNORE_TOUCH_EVENTS) != 0) && (eventType == TOUCH_EVENT)) { |
| Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 2395 | if (false) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2396 | Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey)); |
| Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 2397 | } |
| 2398 | return; |
| 2399 | } |
| 2400 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2401 | synchronized (mLocks) { |
| 2402 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2403 | Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2404 | + " mUserActivityAllowed=" + mUserActivityAllowed |
| 2405 | + " mUserState=0x" + Integer.toHexString(mUserState) |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2406 | + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState) |
| 2407 | + " mProximitySensorActive=" + mProximitySensorActive |
| Joe Onorato | 797e688 | 2010-08-26 14:46:01 -0400 | [diff] [blame] | 2408 | + " timeoutOverride=" + timeoutOverride |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2409 | + " force=" + force); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2410 | } |
| Mike Lockwood | 0506712 | 2009-10-27 23:07:25 -0400 | [diff] [blame] | 2411 | // ignore user activity if we are in the process of turning off the screen |
| Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 2412 | if (isScreenTurningOffLocked()) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2413 | Slog.d(TAG, "ignoring user activity while turning off screen"); |
| Mike Lockwood | 0506712 | 2009-10-27 23:07:25 -0400 | [diff] [blame] | 2414 | return; |
| 2415 | } |
| Mike Lockwood | 0e39ea8 | 2009-11-18 15:37:10 -0500 | [diff] [blame] | 2416 | // Disable proximity sensor if if user presses power key while we are in the |
| 2417 | // "waiting for proximity sensor to go negative" state. |
| 2418 | if (mProximitySensorActive && mProximityWakeLockCount == 0) { |
| 2419 | mProximitySensorActive = false; |
| 2420 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2421 | if (mLastEventTime <= time || force) { |
| 2422 | mLastEventTime = time; |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2423 | if ((mUserActivityAllowed && !mProximitySensorActive) || force) { |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2424 | // Only turn on button backlights if a button was pressed |
| 2425 | // and auto brightness is disabled |
| Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2426 | if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2427 | mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT); |
| 2428 | } else { |
| 2429 | // don't clear button/keyboard backlights when the screen is touched. |
| 2430 | mUserState |= SCREEN_BRIGHT; |
| 2431 | } |
| 2432 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2433 | int uid = Binder.getCallingUid(); |
| 2434 | long ident = Binder.clearCallingIdentity(); |
| 2435 | try { |
| 2436 | mBatteryStats.noteUserActivity(uid, eventType); |
| 2437 | } catch (RemoteException e) { |
| 2438 | // Ignore |
| 2439 | } finally { |
| 2440 | Binder.restoreCallingIdentity(ident); |
| 2441 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2442 | |
| Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2443 | mWakeLockState = mLocks.reactivateScreenLocksLocked(); |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2444 | setPowerState(mUserState | mWakeLockState, noChangeLights, |
| 2445 | WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
| Joe Onorato | 7999bff | 2010-07-24 11:50:05 -0400 | [diff] [blame] | 2446 | setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | } |
| Mike Lockwood | ef73162 | 2010-01-27 17:51:34 -0500 | [diff] [blame] | 2450 | |
| 2451 | if (mPolicy != null) { |
| 2452 | mPolicy.userActivity(); |
| 2453 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2454 | } |
| 2455 | |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2456 | private int getAutoBrightnessValue(int sensorValue, int[] values) { |
| 2457 | try { |
| 2458 | int i; |
| 2459 | for (i = 0; i < mAutoBrightnessLevels.length; i++) { |
| 2460 | if (sensorValue < mAutoBrightnessLevels[i]) { |
| 2461 | break; |
| 2462 | } |
| 2463 | } |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2464 | // This is the range of brightness values that we can use. |
| 2465 | final int minval = values[0]; |
| 2466 | final int maxval = values[mAutoBrightnessLevels.length]; |
| 2467 | // This is the range we will be scaling. We put some padding |
| 2468 | // at the low and high end to give the adjustment a little better |
| 2469 | // impact on the actual observed value. |
| 2470 | final int range = (maxval-minval) + LIGHT_SENSOR_RANGE_EXPANSION; |
| 2471 | // This is the desired brightness value from 0.0 to 1.0. |
| 2472 | float valf = ((values[i]-minval+(LIGHT_SENSOR_RANGE_EXPANSION/2))/(float)range); |
| 2473 | // Apply a scaling to the value based on the adjustment. |
| 2474 | if (mLightSensorAdjustSetting > 0 && mLightSensorAdjustSetting <= 1) { |
| 2475 | float adj = (float)Math.sqrt(1.0f-mLightSensorAdjustSetting); |
| 2476 | if (adj <= .00001) { |
| 2477 | valf = 1; |
| 2478 | } else { |
| 2479 | valf /= adj; |
| 2480 | } |
| 2481 | } else if (mLightSensorAdjustSetting < 0 && mLightSensorAdjustSetting >= -1) { |
| 2482 | float adj = (float)Math.sqrt(1.0f+mLightSensorAdjustSetting); |
| 2483 | valf *= adj; |
| 2484 | } |
| 2485 | // Apply an additional offset to the value based on the adjustment. |
| 2486 | valf += mLightSensorAdjustSetting/LIGHT_SENSOR_OFFSET_SCALE; |
| 2487 | // Convert the 0.0-1.0 value back to a brightness integer. |
| 2488 | int val = (int)((valf*range)+minval) - (LIGHT_SENSOR_RANGE_EXPANSION/2); |
| 2489 | if (val < minval) val = minval; |
| 2490 | else if (val > maxval) val = maxval; |
| 2491 | return val; |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2492 | } catch (Exception e) { |
| 2493 | // guard against null pointer or index out of bounds errors |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2494 | Slog.e(TAG, "getAutoBrightnessValue", e); |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2495 | return 255; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2499 | private Runnable mProximityTask = new Runnable() { |
| 2500 | public void run() { |
| 2501 | synchronized (mLocks) { |
| 2502 | if (mProximityPendingValue != -1) { |
| 2503 | proximityChangedLocked(mProximityPendingValue == 1); |
| 2504 | mProximityPendingValue = -1; |
| 2505 | } |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 2506 | if (mProximityPartialLock.isHeld()) { |
| 2507 | mProximityPartialLock.release(); |
| 2508 | } |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 2509 | } |
| 2510 | } |
| 2511 | }; |
| 2512 | |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2513 | private Runnable mAutoBrightnessTask = new Runnable() { |
| 2514 | public void run() { |
| Mike Lockwood | fa68ab4 | 2009-10-20 11:08:49 -0400 | [diff] [blame] | 2515 | synchronized (mLocks) { |
| Jim Rodovich | d102fea | 2010-09-02 12:30:49 -0500 | [diff] [blame] | 2516 | if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) { |
| 2517 | int value = (int)mLightSensorPendingValue; |
| 2518 | mLightSensorPendingDecrease = false; |
| 2519 | mLightSensorPendingIncrease = false; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2520 | lightSensorChangedLocked(value, false); |
| Mike Lockwood | fa68ab4 | 2009-10-20 11:08:49 -0400 | [diff] [blame] | 2521 | } |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2522 | } |
| 2523 | } |
| 2524 | }; |
| 2525 | |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2526 | private void dockStateChanged(int state) { |
| 2527 | synchronized (mLocks) { |
| 2528 | mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 2529 | if (mIsDocked) { |
| Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame] | 2530 | // allow brightness to decrease when docked |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2531 | mHighestLightSensorValue = -1; |
| 2532 | } |
| 2533 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 2534 | // force lights recalculation |
| 2535 | int value = (int)mLightSensorValue; |
| 2536 | mLightSensorValue = -1; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2537 | lightSensorChangedLocked(value, false); |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2542 | private void lightSensorChangedLocked(int value, boolean immediate) { |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2543 | if (mDebugLightSensor) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2544 | Slog.d(TAG, "lightSensorChangedLocked " + value); |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2545 | } |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2546 | |
| Joe Onorato | 06eb33a | 2010-10-25 14:09:21 -0700 | [diff] [blame] | 2547 | // Don't do anything if the screen is off. |
| 2548 | if ((mPowerState & SCREEN_ON_BIT) == 0) { |
| 2549 | if (mDebugLightSensor) { |
| 2550 | Slog.d(TAG, "dropping lightSensorChangedLocked because screen is off"); |
| 2551 | } |
| 2552 | return; |
| 2553 | } |
| 2554 | |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2555 | // do not allow light sensor value to decrease |
| 2556 | if (mHighestLightSensorValue < value) { |
| 2557 | mHighestLightSensorValue = value; |
| 2558 | } |
| 2559 | |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2560 | if (mLightSensorValue != value) { |
| 2561 | mLightSensorValue = value; |
| 2562 | if ((mPowerState & BATTERY_LOW_BIT) == 0) { |
| Mike Lockwood | b286541 | 2010-02-02 22:40:33 -0500 | [diff] [blame] | 2563 | // use maximum light sensor value seen since screen went on for LCD to avoid flicker |
| 2564 | // we only do this if we are undocked, since lighting should be stable when |
| 2565 | // stationary in a dock. |
| 2566 | int lcdValue = getAutoBrightnessValue( |
| 2567 | (mIsDocked ? value : mHighestLightSensorValue), |
| 2568 | mLcdBacklightValues); |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2569 | int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues); |
| Mike Lockwood | df02492 | 2009-10-29 21:29:15 -0400 | [diff] [blame] | 2570 | int keyboardValue; |
| 2571 | if (mKeyboardVisible) { |
| 2572 | keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues); |
| 2573 | } else { |
| 2574 | keyboardValue = 0; |
| 2575 | } |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2576 | mLightSensorScreenBrightness = lcdValue; |
| 2577 | mLightSensorButtonBrightness = buttonValue; |
| 2578 | mLightSensorKeyboardBrightness = keyboardValue; |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2579 | |
| 2580 | if (mDebugLightSensor) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2581 | Slog.d(TAG, "lcdValue " + lcdValue); |
| 2582 | Slog.d(TAG, "buttonValue " + buttonValue); |
| 2583 | Slog.d(TAG, "keyboardValue " + keyboardValue); |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
| Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2586 | if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) { |
| Dianne Hackborn | 81de8b9 | 2011-11-28 16:54:31 -0800 | [diff] [blame] | 2587 | if (!mSkippedScreenOn) { |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2588 | mScreenBrightness.setTargetLocked(lcdValue, |
| 2589 | immediate ? IMMEDIATE_ANIM_STEPS : AUTOBRIGHTNESS_ANIM_STEPS, |
| Dianne Hackborn | 81de8b9 | 2011-11-28 16:54:31 -0800 | [diff] [blame] | 2590 | INITIAL_SCREEN_BRIGHTNESS, (int)mScreenBrightness.curValue); |
| 2591 | } |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2592 | } |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2593 | if (mButtonBrightnessOverride < 0) { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2594 | mButtonLight.setBrightness(buttonValue); |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2595 | } |
| Mike Lockwood | fb73f79 | 2009-11-20 11:31:18 -0500 | [diff] [blame] | 2596 | if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) { |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2597 | mKeyboardLight.setBrightness(keyboardValue); |
| Mike Lockwood | d7786b4 | 2009-10-15 17:09:16 -0700 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | } |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2601 | } |
| 2602 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2603 | /** |
| 2604 | * The user requested that we go to sleep (probably with the power button). |
| 2605 | * This overrides all wake locks that are held. |
| 2606 | */ |
| 2607 | public void goToSleep(long time) |
| 2608 | { |
| Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 2609 | goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
| 2610 | } |
| 2611 | |
| 2612 | /** |
| 2613 | * The user requested that we go to sleep (probably with the power button). |
| 2614 | * This overrides all wake locks that are held. |
| 2615 | */ |
| 2616 | public void goToSleepWithReason(long time, int reason) |
| 2617 | { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2618 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2619 | synchronized (mLocks) { |
| Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 2620 | goToSleepLocked(time, reason); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2621 | } |
| 2622 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2623 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2624 | /** |
| Doug Zongker | 50a21f4 | 2009-11-19 12:49:53 -0800 | [diff] [blame] | 2625 | * Reboot the device immediately, passing 'reason' (may be null) |
| 2626 | * to the underlying __reboot system call. Should not return. |
| 2627 | */ |
| 2628 | public void reboot(String reason) |
| 2629 | { |
| 2630 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| San Mehat | 14e69af | 2010-01-06 14:58:18 -0800 | [diff] [blame] | 2631 | |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2632 | if (mHandler == null || !ActivityManagerNative.isSystemReady()) { |
| 2633 | throw new IllegalStateException("Too early to call reboot()"); |
| 2634 | } |
| Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2635 | |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2636 | final String finalReason = reason; |
| 2637 | Runnable runnable = new Runnable() { |
| 2638 | public void run() { |
| 2639 | synchronized (this) { |
| 2640 | ShutdownThread.reboot(mContext, finalReason, false); |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2641 | } |
| 2642 | |
| San Mehat | 1e51279 | 2010-01-07 10:40:29 -0800 | [diff] [blame] | 2643 | } |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2644 | }; |
| Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2645 | // ShutdownThread must run on a looper capable of displaying the UI. |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2646 | mHandler.post(runnable); |
| 2647 | |
| Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2648 | // PowerManager.reboot() is documented not to return so just wait for the inevitable. |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2649 | synchronized (runnable) { |
| Mike Lockwood | b62f959 | 2010-03-12 07:55:23 -0500 | [diff] [blame] | 2650 | while (true) { |
| 2651 | try { |
| 2652 | runnable.wait(); |
| 2653 | } catch (InterruptedException e) { |
| 2654 | } |
| Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 2655 | } |
| Doug Zongker | 50a21f4 | 2009-11-19 12:49:53 -0800 | [diff] [blame] | 2656 | } |
| 2657 | } |
| 2658 | |
| Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 2659 | /** |
| 2660 | * Crash the runtime (causing a complete restart of the Android framework). |
| 2661 | * Requires REBOOT permission. Mostly for testing. Should not return. |
| 2662 | */ |
| 2663 | public void crash(final String message) |
| 2664 | { |
| 2665 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); |
| 2666 | Thread t = new Thread("PowerManagerService.crash()") { |
| 2667 | public void run() { throw new RuntimeException(message); } |
| 2668 | }; |
| 2669 | try { |
| 2670 | t.start(); |
| 2671 | t.join(); |
| 2672 | } catch (InterruptedException e) { |
| 2673 | Log.wtf(TAG, e); |
| 2674 | } |
| 2675 | } |
| 2676 | |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2677 | private void goToSleepLocked(long time, int reason) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2678 | |
| 2679 | if (mLastEventTime <= time) { |
| 2680 | mLastEventTime = time; |
| 2681 | // cancel all of the wake locks |
| 2682 | mWakeLockState = SCREEN_OFF; |
| 2683 | int N = mLocks.size(); |
| 2684 | int numCleared = 0; |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2685 | boolean proxLock = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2686 | for (int i=0; i<N; i++) { |
| 2687 | WakeLock wl = mLocks.get(i); |
| 2688 | if (isScreenLock(wl.flags)) { |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2689 | if (((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) |
| 2690 | && reason == WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR) { |
| 2691 | proxLock = true; |
| 2692 | } else { |
| 2693 | mLocks.get(i).activated = false; |
| 2694 | numCleared++; |
| 2695 | } |
| 2696 | } |
| 2697 | } |
| 2698 | if (!proxLock) { |
| 2699 | mProxIgnoredBecauseScreenTurnedOff = true; |
| 2700 | if (mDebugProximitySensor) { |
| 2701 | Slog.d(TAG, "setting mProxIgnoredBecauseScreenTurnedOff"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2702 | } |
| 2703 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2704 | EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 2705 | mStillNeedSleepNotification = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2706 | mUserState = SCREEN_OFF; |
| Mike Lockwood | 435eb64 | 2009-12-03 08:40:18 -0500 | [diff] [blame] | 2707 | setPowerState(SCREEN_OFF, false, reason); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2708 | cancelTimerLocked(); |
| 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | public long timeSinceScreenOn() { |
| 2713 | synchronized (mLocks) { |
| 2714 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 2715 | return 0; |
| 2716 | } |
| 2717 | return SystemClock.elapsedRealtime() - mScreenOffTime; |
| 2718 | } |
| 2719 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2720 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2721 | public void setKeyboardVisibility(boolean visible) { |
| Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2722 | synchronized (mLocks) { |
| 2723 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2724 | Slog.d(TAG, "setKeyboardVisibility: " + visible); |
| Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2725 | } |
| Mike Lockwood | 3c9435a | 2009-10-22 15:45:37 -0400 | [diff] [blame] | 2726 | if (mKeyboardVisible != visible) { |
| 2727 | mKeyboardVisible = visible; |
| 2728 | // don't signal user activity if the screen is off; other code |
| 2729 | // will take care of turning on due to a true change to the lid |
| 2730 | // switch and synchronized with the lock screen. |
| 2731 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| Mike Lockwood | 4984e73 | 2009-11-01 08:16:33 -0500 | [diff] [blame] | 2732 | if (mUseSoftwareAutoBrightness) { |
| Mike Lockwood | df02492 | 2009-10-29 21:29:15 -0400 | [diff] [blame] | 2733 | // force recompute of backlight values |
| 2734 | if (mLightSensorValue >= 0) { |
| 2735 | int value = (int)mLightSensorValue; |
| 2736 | mLightSensorValue = -1; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2737 | lightSensorChangedLocked(value, false); |
| Mike Lockwood | df02492 | 2009-10-29 21:29:15 -0400 | [diff] [blame] | 2738 | } |
| 2739 | } |
| Mike Lockwood | 3c9435a | 2009-10-22 15:45:37 -0400 | [diff] [blame] | 2740 | userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true); |
| 2741 | } |
| Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 2742 | } |
| 2743 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | /** |
| 2747 | * When the keyguard is up, it manages the power state, and userActivity doesn't do anything. |
| Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2748 | * When disabling user activity we also reset user power state so the keyguard can reset its |
| 2749 | * short screen timeout when keyguard is unhidden. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2750 | */ |
| 2751 | public void enableUserActivity(boolean enabled) { |
| Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2752 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2753 | Slog.d(TAG, "enableUserActivity " + enabled); |
| Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2754 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2755 | synchronized (mLocks) { |
| 2756 | mUserActivityAllowed = enabled; |
| Mike Lockwood | 50c548d | 2009-11-09 16:02:06 -0500 | [diff] [blame] | 2757 | if (!enabled) { |
| 2758 | // cancel timeout and clear mUserState so the keyguard can set a short timeout |
| 2759 | setTimeoutLocked(SystemClock.uptimeMillis(), 0); |
| 2760 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2761 | } |
| 2762 | } |
| 2763 | |
| Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 2764 | private void setScreenBrightnessMode(int mode) { |
| Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 2765 | synchronized (mLocks) { |
| 2766 | boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC); |
| 2767 | if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) { |
| 2768 | mAutoBrightessEnabled = enabled; |
| 2769 | // This will get us a new value |
| 2770 | enableLightSensorLocked(mAutoBrightessEnabled && isScreenOn()); |
| Mike Lockwood | 2d155d2 | 2009-10-27 09:32:30 -0400 | [diff] [blame] | 2771 | } |
| Mike Lockwood | dc3494e | 2009-10-14 21:17:09 -0700 | [diff] [blame] | 2772 | } |
| 2773 | } |
| 2774 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2775 | /** Sets the screen off timeouts: |
| 2776 | * mKeylightDelay |
| 2777 | * mDimDelay |
| 2778 | * mScreenOffDelay |
| 2779 | * */ |
| 2780 | private void setScreenOffTimeoutsLocked() { |
| 2781 | if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) { |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2782 | mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2783 | mDimDelay = -1; |
| 2784 | mScreenOffDelay = 0; |
| 2785 | } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) { |
| 2786 | mKeylightDelay = MEDIUM_KEYLIGHT_DELAY; |
| 2787 | mDimDelay = -1; |
| 2788 | mScreenOffDelay = 0; |
| 2789 | } else { |
| Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2790 | int totalDelay = mScreenOffTimeoutSetting; |
| 2791 | if (totalDelay > mMaximumScreenOffTimeout) { |
| 2792 | totalDelay = mMaximumScreenOffTimeout; |
| 2793 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2794 | mKeylightDelay = LONG_KEYLIGHT_DELAY; |
| 2795 | if (totalDelay < 0) { |
| Jim Miller | bc4603b | 2010-08-30 21:21:34 -0700 | [diff] [blame] | 2796 | // negative number means stay on as long as possible. |
| 2797 | mScreenOffDelay = mMaximumScreenOffTimeout; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2798 | } else if (mKeylightDelay < totalDelay) { |
| 2799 | // subtract the time that the keylight delay. This will give us the |
| 2800 | // remainder of the time that we need to sleep to get the accurate |
| 2801 | // screen off timeout. |
| 2802 | mScreenOffDelay = totalDelay - mKeylightDelay; |
| 2803 | } else { |
| 2804 | mScreenOffDelay = 0; |
| 2805 | } |
| 2806 | if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) { |
| 2807 | mDimDelay = mScreenOffDelay - LONG_DIM_TIME; |
| 2808 | mScreenOffDelay = LONG_DIM_TIME; |
| 2809 | } else { |
| 2810 | mDimDelay = -1; |
| 2811 | } |
| 2812 | } |
| 2813 | if (mSpew) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2814 | Slog.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2815 | + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay |
| 2816 | + " mDimScreen=" + mDimScreen); |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | /** |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2821 | * Refreshes cached secure settings. Called once on startup, and |
| 2822 | * on subsequent changes to secure settings. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2823 | */ |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2824 | private void updateSettingsValues() { |
| 2825 | mShortKeylightDelay = Settings.Secure.getInt( |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2826 | mContext.getContentResolver(), |
| Doug Zongker | 43866e0 | 2010-01-07 12:09:54 -0800 | [diff] [blame] | 2827 | Settings.Secure.SHORT_KEYLIGHT_DELAY_MS, |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2828 | SHORT_KEYLIGHT_DELAY_DEFAULT); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2829 | // Slog.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | private class LockList extends ArrayList<WakeLock> |
| 2833 | { |
| 2834 | void addLock(WakeLock wl) |
| 2835 | { |
| 2836 | int index = getIndex(wl.binder); |
| 2837 | if (index < 0) { |
| 2838 | this.add(wl); |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | WakeLock removeLock(IBinder binder) |
| 2843 | { |
| 2844 | int index = getIndex(binder); |
| 2845 | if (index >= 0) { |
| 2846 | return this.remove(index); |
| 2847 | } else { |
| 2848 | return null; |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | int getIndex(IBinder binder) |
| 2853 | { |
| 2854 | int N = this.size(); |
| 2855 | for (int i=0; i<N; i++) { |
| 2856 | if (this.get(i).binder == binder) { |
| 2857 | return i; |
| 2858 | } |
| 2859 | } |
| 2860 | return -1; |
| 2861 | } |
| 2862 | |
| 2863 | int gatherState() |
| 2864 | { |
| 2865 | int result = 0; |
| 2866 | int N = this.size(); |
| 2867 | for (int i=0; i<N; i++) { |
| 2868 | WakeLock wl = this.get(i); |
| 2869 | if (wl.activated) { |
| 2870 | if (isScreenLock(wl.flags)) { |
| 2871 | result |= wl.minState; |
| 2872 | } |
| 2873 | } |
| 2874 | } |
| 2875 | return result; |
| 2876 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2877 | |
| Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2878 | int reactivateScreenLocksLocked() |
| 2879 | { |
| 2880 | int result = 0; |
| 2881 | int N = this.size(); |
| 2882 | for (int i=0; i<N; i++) { |
| 2883 | WakeLock wl = this.get(i); |
| 2884 | if (isScreenLock(wl.flags)) { |
| 2885 | wl.activated = true; |
| 2886 | result |= wl.minState; |
| 2887 | } |
| 2888 | } |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 2889 | if (mDebugProximitySensor) { |
| 2890 | Slog.d(TAG, "reactivateScreenLocksLocked mProxIgnoredBecauseScreenTurnedOff=" |
| 2891 | + mProxIgnoredBecauseScreenTurnedOff); |
| 2892 | } |
| 2893 | mProxIgnoredBecauseScreenTurnedOff = false; |
| Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 2894 | return result; |
| 2895 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2896 | } |
| 2897 | |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 2898 | public void setPolicy(WindowManagerPolicy p) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2899 | synchronized (mLocks) { |
| 2900 | mPolicy = p; |
| 2901 | mLocks.notifyAll(); |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | WindowManagerPolicy getPolicyLocked() { |
| 2906 | while (mPolicy == null || !mDoneBooting) { |
| 2907 | try { |
| 2908 | mLocks.wait(); |
| 2909 | } catch (InterruptedException e) { |
| 2910 | // Ignore |
| 2911 | } |
| 2912 | } |
| 2913 | return mPolicy; |
| 2914 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2915 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2916 | void systemReady() { |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2917 | mSensorManager = new SensorManager(mHandlerThread.getLooper()); |
| 2918 | mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); |
| 2919 | // don't bother with the light sensor if auto brightness is handled in hardware |
| Mike Lockwood | aa66ea8 | 2009-10-31 16:31:27 -0400 | [diff] [blame] | 2920 | if (mUseSoftwareAutoBrightness) { |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2921 | mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 2922 | } |
| 2923 | |
| Mike Lockwood | b42ab0f | 2010-03-04 08:02:44 -0500 | [diff] [blame] | 2924 | // wait until sensors are enabled before turning on screen. |
| 2925 | // some devices will not activate the light sensor properly on boot |
| 2926 | // unless we do this. |
| 2927 | if (mUseSoftwareAutoBrightness) { |
| 2928 | // turn the screen on |
| 2929 | setPowerState(SCREEN_BRIGHT); |
| 2930 | } else { |
| 2931 | // turn everything on |
| 2932 | setPowerState(ALL_BRIGHT); |
| 2933 | } |
| 2934 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2935 | synchronized (mLocks) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2936 | Slog.d(TAG, "system ready!"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2937 | mDoneBooting = true; |
| Mike Lockwood | b42ab0f | 2010-03-04 08:02:44 -0500 | [diff] [blame] | 2938 | |
| Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 2939 | enableLightSensorLocked(mUseSoftwareAutoBrightness && mAutoBrightessEnabled); |
| 2940 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2941 | long identity = Binder.clearCallingIdentity(); |
| 2942 | try { |
| 2943 | mBatteryStats.noteScreenBrightness(getPreferredBrightness()); |
| 2944 | mBatteryStats.noteScreenOn(); |
| 2945 | } catch (RemoteException e) { |
| 2946 | // Nothing interesting to do. |
| 2947 | } finally { |
| 2948 | Binder.restoreCallingIdentity(identity); |
| 2949 | } |
| Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | void bootCompleted() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2954 | Slog.d(TAG, "bootCompleted"); |
| Mike Lockwood | 2d7bb81 | 2009-11-15 18:12:22 -0500 | [diff] [blame] | 2955 | synchronized (mLocks) { |
| 2956 | mBootCompleted = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2957 | userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true); |
| 2958 | updateWakeLockLocked(); |
| 2959 | mLocks.notifyAll(); |
| 2960 | } |
| 2961 | } |
| 2962 | |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2963 | // for watchdog |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2964 | public void monitor() { |
| 2965 | synchronized (mLocks) { } |
| 2966 | } |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2967 | |
| 2968 | public int getSupportedWakeLockFlags() { |
| 2969 | int result = PowerManager.PARTIAL_WAKE_LOCK |
| 2970 | | PowerManager.FULL_WAKE_LOCK |
| 2971 | | PowerManager.SCREEN_DIM_WAKE_LOCK; |
| 2972 | |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2973 | if (mProximitySensor != null) { |
| 2974 | result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
| 2975 | } |
| 2976 | |
| 2977 | return result; |
| 2978 | } |
| 2979 | |
| Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 2980 | public void setBacklightBrightness(int brightness) { |
| 2981 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 2982 | // Don't let applications turn the screen all the way off |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2983 | synchronized (mLocks) { |
| Mike Lockwood | eb6456b | 2011-09-13 15:24:02 -0400 | [diff] [blame] | 2984 | brightness = Math.max(brightness, mScreenBrightnessDim); |
| Joe Onorato | b08a1af | 2010-10-11 19:28:58 -0700 | [diff] [blame] | 2985 | mLcdLight.setBrightness(brightness); |
| 2986 | mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0); |
| 2987 | mButtonLight.setBrightness(brightness); |
| 2988 | long identity = Binder.clearCallingIdentity(); |
| 2989 | try { |
| 2990 | mBatteryStats.noteScreenBrightness(brightness); |
| 2991 | } catch (RemoteException e) { |
| 2992 | Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e); |
| 2993 | } finally { |
| 2994 | Binder.restoreCallingIdentity(identity); |
| 2995 | } |
| Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 2996 | |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 2997 | mScreenBrightness.targetValue = brightness; |
| 2998 | mScreenBrightness.jumpToTargetLocked(); |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | public void setAutoBrightnessAdjustment(float adj) { |
| 3003 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 3004 | synchronized (mLocks) { |
| 3005 | mLightSensorAdjustSetting = adj; |
| 3006 | if (mSensorManager != null && mLightSensorEnabled) { |
| 3007 | // clear calling identity so sensor manager battery stats are accurate |
| 3008 | long identity = Binder.clearCallingIdentity(); |
| 3009 | try { |
| 3010 | // force recompute of backlight values |
| 3011 | if (mLightSensorValue >= 0) { |
| 3012 | int value = (int)mLightSensorValue; |
| 3013 | mLightSensorValue = -1; |
| 3014 | handleLightSensorValue(value, true); |
| 3015 | } |
| 3016 | } finally { |
| 3017 | Binder.restoreCallingIdentity(identity); |
| 3018 | } |
| Joe Onorato | 3d3db60 | 2010-10-18 16:08:16 -0400 | [diff] [blame] | 3019 | } |
| Mike Lockwood | 237a299 | 2009-09-15 14:42:16 -0400 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | |
| Mike Lockwood | b11832d | 2009-11-25 15:25:55 -0500 | [diff] [blame] | 3023 | public void setAttentionLight(boolean on, int color) { |
| 3024 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 3025 | mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0); |
| Mike Lockwood | b11832d | 2009-11-25 15:25:55 -0500 | [diff] [blame] | 3026 | } |
| 3027 | |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 3028 | private void enableProximityLockLocked() { |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3029 | if (mDebugProximitySensor) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3030 | Slog.d(TAG, "enableProximityLockLocked"); |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 3031 | } |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3032 | if (!mProximitySensorEnabled) { |
| 3033 | // clear calling identity so sensor manager battery stats are accurate |
| 3034 | long identity = Binder.clearCallingIdentity(); |
| 3035 | try { |
| 3036 | mSensorManager.registerListener(mProximityListener, mProximitySensor, |
| 3037 | SensorManager.SENSOR_DELAY_NORMAL); |
| 3038 | mProximitySensorEnabled = true; |
| 3039 | } finally { |
| 3040 | Binder.restoreCallingIdentity(identity); |
| 3041 | } |
| Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3042 | } |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | private void disableProximityLockLocked() { |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3046 | if (mDebugProximitySensor) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3047 | Slog.d(TAG, "disableProximityLockLocked"); |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 3048 | } |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3049 | if (mProximitySensorEnabled) { |
| 3050 | // clear calling identity so sensor manager battery stats are accurate |
| 3051 | long identity = Binder.clearCallingIdentity(); |
| 3052 | try { |
| 3053 | mSensorManager.unregisterListener(mProximityListener); |
| 3054 | mHandler.removeCallbacks(mProximityTask); |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 3055 | if (mProximityPartialLock.isHeld()) { |
| 3056 | mProximityPartialLock.release(); |
| 3057 | } |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3058 | mProximitySensorEnabled = false; |
| 3059 | } finally { |
| 3060 | Binder.restoreCallingIdentity(identity); |
| 3061 | } |
| 3062 | if (mProximitySensorActive) { |
| 3063 | mProximitySensorActive = false; |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 3064 | if (mDebugProximitySensor) { |
| 3065 | Slog.d(TAG, "disableProximityLockLocked mProxIgnoredBecauseScreenTurnedOff=" |
| 3066 | + mProxIgnoredBecauseScreenTurnedOff); |
| 3067 | } |
| 3068 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 3069 | forceUserActivityLocked(); |
| 3070 | } |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3071 | } |
| Mike Lockwood | 200b30b | 2009-09-20 00:23:59 -0400 | [diff] [blame] | 3072 | } |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 3073 | } |
| 3074 | |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3075 | private void proximityChangedLocked(boolean active) { |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3076 | if (mDebugProximitySensor) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3077 | Slog.d(TAG, "proximityChangedLocked, active: " + active); |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3078 | } |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3079 | if (!mProximitySensorEnabled) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3080 | Slog.d(TAG, "Ignoring proximity change after sensor is disabled"); |
| Mike Lockwood | 0d72f7e | 2009-11-05 20:53:00 -0500 | [diff] [blame] | 3081 | return; |
| 3082 | } |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3083 | if (active) { |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 3084 | if (mDebugProximitySensor) { |
| 3085 | Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff=" |
| 3086 | + mProxIgnoredBecauseScreenTurnedOff); |
| 3087 | } |
| 3088 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 3089 | goToSleepLocked(SystemClock.uptimeMillis(), |
| 3090 | WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR); |
| 3091 | } |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3092 | mProximitySensorActive = true; |
| 3093 | } else { |
| 3094 | // proximity sensor negative events trigger as user activity. |
| 3095 | // temporarily set mUserActivityAllowed to true so this will work |
| 3096 | // even when the keyguard is on. |
| 3097 | mProximitySensorActive = false; |
| Joe Onorato | 8274a0e | 2010-10-05 17:38:09 -0400 | [diff] [blame] | 3098 | if (mDebugProximitySensor) { |
| 3099 | Slog.d(TAG, "b mProxIgnoredBecauseScreenTurnedOff=" |
| 3100 | + mProxIgnoredBecauseScreenTurnedOff); |
| 3101 | } |
| 3102 | if (!mProxIgnoredBecauseScreenTurnedOff) { |
| 3103 | forceUserActivityLocked(); |
| 3104 | } |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3105 | |
| 3106 | if (mProximityWakeLockCount == 0) { |
| 3107 | // disable sensor if we have no listeners left after proximity negative |
| 3108 | disableProximityLockLocked(); |
| 3109 | } |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3110 | } |
| 3111 | } |
| 3112 | |
| Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 3113 | private void enableLightSensorLocked(boolean enable) { |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3114 | if (mDebugLightSensor) { |
| Joe Onorato | d28f753 | 2010-11-06 12:56:53 -0700 | [diff] [blame] | 3115 | Slog.d(TAG, "enableLightSensorLocked enable=" + enable |
| 3116 | + " mAutoBrightessEnabled=" + mAutoBrightessEnabled); |
| 3117 | } |
| 3118 | if (!mAutoBrightessEnabled) { |
| 3119 | enable = false; |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3120 | } |
| 3121 | if (mSensorManager != null && mLightSensorEnabled != enable) { |
| 3122 | mLightSensorEnabled = enable; |
| Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3123 | // clear calling identity so sensor manager battery stats are accurate |
| 3124 | long identity = Binder.clearCallingIdentity(); |
| 3125 | try { |
| 3126 | if (enable) { |
| Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame] | 3127 | // reset our highest value when reenabling |
| 3128 | mHighestLightSensorValue = -1; |
| 3129 | // force recompute of backlight values |
| 3130 | if (mLightSensorValue >= 0) { |
| 3131 | int value = (int)mLightSensorValue; |
| 3132 | mLightSensorValue = -1; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 3133 | handleLightSensorValue(value, true); |
| Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame] | 3134 | } |
| Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3135 | mSensorManager.registerListener(mLightListener, mLightSensor, |
| Mathias Agopian | 47f1fe5 | 2011-11-08 17:18:41 -0800 | [diff] [blame] | 3136 | LIGHT_SENSOR_RATE); |
| Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3137 | } else { |
| 3138 | mSensorManager.unregisterListener(mLightListener); |
| 3139 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame] | 3140 | mLightSensorPendingDecrease = false; |
| 3141 | mLightSensorPendingIncrease = false; |
| Mike Lockwood | 809ad0f | 2009-10-26 22:10:33 -0400 | [diff] [blame] | 3142 | } |
| 3143 | } finally { |
| 3144 | Binder.restoreCallingIdentity(identity); |
| Mike Lockwood | 06952d9 | 2009-08-13 16:05:38 -0400 | [diff] [blame] | 3145 | } |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3149 | SensorEventListener mProximityListener = new SensorEventListener() { |
| 3150 | public void onSensorChanged(SensorEvent event) { |
| Mike Lockwood | ba8eb1e | 2009-11-08 19:31:18 -0500 | [diff] [blame] | 3151 | long milliseconds = SystemClock.elapsedRealtime(); |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3152 | synchronized (mLocks) { |
| 3153 | float distance = event.values[0]; |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3154 | long timeSinceLastEvent = milliseconds - mLastProximityEventTime; |
| 3155 | mLastProximityEventTime = milliseconds; |
| 3156 | mHandler.removeCallbacks(mProximityTask); |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 3157 | boolean proximityTaskQueued = false; |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3158 | |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3159 | // compare against getMaximumRange to support sensors that only return 0 or 1 |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3160 | boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD && |
| 3161 | distance < mProximitySensor.getMaximumRange()); |
| 3162 | |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3163 | if (mDebugProximitySensor) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 3164 | Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active); |
| Mike Lockwood | ee2b094 | 2009-11-09 14:09:02 -0500 | [diff] [blame] | 3165 | } |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3166 | if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) { |
| 3167 | // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing |
| 3168 | mProximityPendingValue = (active ? 1 : 0); |
| 3169 | mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent); |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 3170 | proximityTaskQueued = true; |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3171 | } else { |
| Mike Lockwood | 20f87d7 | 2009-11-05 16:08:51 -0500 | [diff] [blame] | 3172 | // process the value immediately |
| 3173 | mProximityPendingValue = -1; |
| 3174 | proximityChangedLocked(active); |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3175 | } |
| Mike Lockwood | 0e5bb7f | 2009-11-14 06:36:31 -0500 | [diff] [blame] | 3176 | |
| 3177 | // update mProximityPartialLock state |
| 3178 | boolean held = mProximityPartialLock.isHeld(); |
| 3179 | if (!held && proximityTaskQueued) { |
| 3180 | // hold wakelock until mProximityTask runs |
| 3181 | mProximityPartialLock.acquire(); |
| 3182 | } else if (held && !proximityTaskQueued) { |
| 3183 | mProximityPartialLock.release(); |
| 3184 | } |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 3189 | // ignore |
| 3190 | } |
| 3191 | }; |
| 3192 | |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 3193 | private void handleLightSensorValue(int value, boolean immediate) { |
| Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame] | 3194 | long milliseconds = SystemClock.elapsedRealtime(); |
| 3195 | if (mLightSensorValue == -1 || |
| 3196 | milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) { |
| 3197 | // process the value immediately if screen has just turned on |
| 3198 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| 3199 | mLightSensorPendingDecrease = false; |
| 3200 | mLightSensorPendingIncrease = false; |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 3201 | lightSensorChangedLocked(value, immediate); |
| Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame] | 3202 | } else { |
| 3203 | if ((value > mLightSensorValue && mLightSensorPendingDecrease) || |
| 3204 | (value < mLightSensorValue && mLightSensorPendingIncrease) || |
| 3205 | (value == mLightSensorValue) || |
| 3206 | (!mLightSensorPendingDecrease && !mLightSensorPendingIncrease)) { |
| 3207 | // delay processing to debounce the sensor |
| 3208 | mHandler.removeCallbacks(mAutoBrightnessTask); |
| 3209 | mLightSensorPendingDecrease = (value < mLightSensorValue); |
| 3210 | mLightSensorPendingIncrease = (value > mLightSensorValue); |
| 3211 | if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) { |
| 3212 | mLightSensorPendingValue = value; |
| 3213 | mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY); |
| 3214 | } |
| 3215 | } else { |
| 3216 | mLightSensorPendingValue = value; |
| 3217 | } |
| 3218 | } |
| 3219 | } |
| 3220 | |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3221 | SensorEventListener mLightListener = new SensorEventListener() { |
| 3222 | public void onSensorChanged(SensorEvent event) { |
| Mike Lockwood | 5dca30a | 2011-10-13 16:29:29 -0400 | [diff] [blame] | 3223 | if (mDebugLightSensor) { |
| 3224 | Slog.d(TAG, "onSensorChanged: light value: " + event.values[0]); |
| 3225 | } |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3226 | synchronized (mLocks) { |
| Mike Lockwood | 497087e3 | 2009-11-08 18:33:03 -0500 | [diff] [blame] | 3227 | // ignore light sensor while screen is turning off |
| 3228 | if (isScreenTurningOffLocked()) { |
| 3229 | return; |
| 3230 | } |
| Dianne Hackborn | d9ea468 | 2012-01-20 18:36:40 -0800 | [diff] [blame] | 3231 | handleLightSensorValue((int)event.values[0], false); |
| Mike Lockwood | 8738e0c | 2009-10-04 08:44:47 -0400 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | |
| 3235 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 3236 | // ignore |
| 3237 | } |
| 3238 | }; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3239 | } |