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