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