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