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