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