| 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 | |
| 91 | // Cached Gservices settings; see updateGservicesValues() |
| 92 | private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT; |
| 93 | |
| 94 | // flags for setPowerState |
| 95 | private static final int SCREEN_ON_BIT = 0x00000001; |
| 96 | private static final int SCREEN_BRIGHT_BIT = 0x00000002; |
| 97 | private static final int BUTTON_BRIGHT_BIT = 0x00000004; |
| 98 | private static final int KEYBOARD_BRIGHT_BIT = 0x00000008; |
| 99 | private static final int BATTERY_LOW_BIT = 0x00000010; |
| 100 | |
| 101 | // values for setPowerState |
| 102 | |
| 103 | // SCREEN_OFF == everything off |
| 104 | private static final int SCREEN_OFF = 0x00000000; |
| 105 | |
| 106 | // SCREEN_DIM == screen on, screen backlight dim |
| 107 | private static final int SCREEN_DIM = SCREEN_ON_BIT; |
| 108 | |
| 109 | // SCREEN_BRIGHT == screen on, screen backlight bright |
| 110 | private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT; |
| 111 | |
| 112 | // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright |
| 113 | private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT; |
| 114 | |
| 115 | // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright |
| 116 | private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT; |
| 117 | |
| 118 | // used for noChangeLights in setPowerState() |
| 119 | private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT; |
| 120 | |
| 121 | static final boolean ANIMATE_SCREEN_LIGHTS = true; |
| 122 | static final boolean ANIMATE_BUTTON_LIGHTS = false; |
| 123 | static final boolean ANIMATE_KEYBOARD_LIGHTS = false; |
| 124 | |
| 125 | static final int ANIM_STEPS = 60/4; |
| 126 | |
| 127 | // These magic numbers are the initial state of the LEDs at boot. Ideally |
| 128 | // we should read them from the driver, but our current hardware returns 0 |
| 129 | // for the initial value. Oops! |
| 130 | static final int INITIAL_SCREEN_BRIGHTNESS = 255; |
| 131 | static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF; |
| 132 | static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF; |
| 133 | |
| 134 | static final int LOG_POWER_SLEEP_REQUESTED = 2724; |
| 135 | static final int LOG_POWER_SCREEN_BROADCAST_SEND = 2725; |
| 136 | static final int LOG_POWER_SCREEN_BROADCAST_DONE = 2726; |
| 137 | static final int LOG_POWER_SCREEN_BROADCAST_STOP = 2727; |
| 138 | static final int LOG_POWER_SCREEN_STATE = 2728; |
| 139 | static final int LOG_POWER_PARTIAL_WAKE_STATE = 2729; |
| 140 | |
| 141 | private final int MY_UID; |
| 142 | |
| 143 | private boolean mDoneBooting = false; |
| 144 | private int mStayOnConditions = 0; |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 145 | private int[] mBroadcastQueue = new int[] { -1, -1, -1 }; |
| 146 | private int[] mBroadcastWhy = new int[3]; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | private int mPartialCount = 0; |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 148 | private int mProximityCount = 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | private int mPowerState; |
| 150 | private boolean mOffBecauseOfUser; |
| 151 | private int mUserState; |
| 152 | private boolean mKeyboardVisible = false; |
| 153 | private boolean mUserActivityAllowed = true; |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 154 | private boolean mProximitySensorActive = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | private int mTotalDelaySetting; |
| 156 | private int mKeylightDelay; |
| 157 | private int mDimDelay; |
| 158 | private int mScreenOffDelay; |
| 159 | private int mWakeLockState; |
| 160 | private long mLastEventTime = 0; |
| 161 | private long mScreenOffTime; |
| 162 | private volatile WindowManagerPolicy mPolicy; |
| 163 | private final LockList mLocks = new LockList(); |
| 164 | private Intent mScreenOffIntent; |
| 165 | private Intent mScreenOnIntent; |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 166 | private HardwareService mHardware; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | private Context mContext; |
| 168 | private UnsynchronizedWakeLock mBroadcastWakeLock; |
| 169 | private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock; |
| 170 | private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock; |
| 171 | private UnsynchronizedWakeLock mPreventScreenOnPartialLock; |
| 172 | private HandlerThread mHandlerThread; |
| 173 | private Handler mHandler; |
| 174 | private TimeoutTask mTimeoutTask = new TimeoutTask(); |
| 175 | private LightAnimator mLightAnimator = new LightAnimator(); |
| 176 | private final BrightnessState mScreenBrightness |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 177 | = new BrightnessState(SCREEN_BRIGHT_BIT); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | private final BrightnessState mKeyboardBrightness |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 179 | = new BrightnessState(KEYBOARD_BRIGHT_BIT); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | private final BrightnessState mButtonBrightness |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 181 | = new BrightnessState(BUTTON_BRIGHT_BIT); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 182 | private boolean mStillNeedSleepNotification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | private boolean mIsPowered = false; |
| 184 | private IActivityManager mActivityService; |
| 185 | private IBatteryStats mBatteryStats; |
| 186 | private BatteryService mBatteryService; |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 187 | private SensorManager mSensorManager; |
| 188 | private Sensor mProximitySensor; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | private boolean mDimScreen = true; |
| 190 | private long mNextTimeout; |
| 191 | private volatile int mPokey = 0; |
| 192 | private volatile boolean mPokeAwakeOnSet = false; |
| 193 | private volatile boolean mInitComplete = false; |
| 194 | private HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>(); |
| 195 | private long mScreenOnTime; |
| 196 | private long mScreenOnStartTime; |
| 197 | private boolean mPreventScreenOn; |
| 198 | private int mScreenBrightnessOverride = -1; |
| 199 | |
| 200 | // Used when logging number and duration of touch-down cycles |
| 201 | private long mTotalTouchDownTime; |
| 202 | private long mLastTouchDown; |
| 203 | private int mTouchCycles; |
| 204 | |
| 205 | // could be either static or controllable at runtime |
| 206 | private static final boolean mSpew = false; |
| 207 | |
| 208 | /* |
| 209 | static PrintStream mLog; |
| 210 | static { |
| 211 | try { |
| 212 | mLog = new PrintStream("/data/power.log"); |
| 213 | } |
| 214 | catch (FileNotFoundException e) { |
| 215 | android.util.Log.e(TAG, "Life is hard", e); |
| 216 | } |
| 217 | } |
| 218 | static class Log { |
| 219 | static void d(String tag, String s) { |
| 220 | mLog.println(s); |
| 221 | android.util.Log.d(tag, s); |
| 222 | } |
| 223 | static void i(String tag, String s) { |
| 224 | mLog.println(s); |
| 225 | android.util.Log.i(tag, s); |
| 226 | } |
| 227 | static void w(String tag, String s) { |
| 228 | mLog.println(s); |
| 229 | android.util.Log.w(tag, s); |
| 230 | } |
| 231 | static void e(String tag, String s) { |
| 232 | mLog.println(s); |
| 233 | android.util.Log.e(tag, s); |
| 234 | } |
| 235 | } |
| 236 | */ |
| 237 | |
| 238 | /** |
| 239 | * This class works around a deadlock between the lock in PowerManager.WakeLock |
| 240 | * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its |
| 241 | * mToken object so it can be accessed from any thread, but it calls into here |
| 242 | * with its lock held. This class is essentially a reimplementation of |
| 243 | * PowerManager.WakeLock, but without that extra synchronized block, because we'll |
| 244 | * only call it with our own locks held. |
| 245 | */ |
| 246 | private class UnsynchronizedWakeLock { |
| 247 | int mFlags; |
| 248 | String mTag; |
| 249 | IBinder mToken; |
| 250 | int mCount = 0; |
| 251 | boolean mRefCounted; |
| 252 | |
| 253 | UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) { |
| 254 | mFlags = flags; |
| 255 | mTag = tag; |
| 256 | mToken = new Binder(); |
| 257 | mRefCounted = refCounted; |
| 258 | } |
| 259 | |
| 260 | public void acquire() { |
| 261 | if (!mRefCounted || mCount++ == 0) { |
| 262 | long ident = Binder.clearCallingIdentity(); |
| 263 | try { |
| 264 | PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken, |
| 265 | MY_UID, mTag); |
| 266 | } finally { |
| 267 | Binder.restoreCallingIdentity(ident); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | public void release() { |
| 273 | if (!mRefCounted || --mCount == 0) { |
| 274 | PowerManagerService.this.releaseWakeLockLocked(mToken, false); |
| 275 | } |
| 276 | if (mCount < 0) { |
| 277 | throw new RuntimeException("WakeLock under-locked " + mTag); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | public String toString() { |
| 282 | return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags) |
| 283 | + " mCount=" + mCount + ")"; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | private final class BatteryReceiver extends BroadcastReceiver { |
| 288 | @Override |
| 289 | public void onReceive(Context context, Intent intent) { |
| 290 | synchronized (mLocks) { |
| 291 | boolean wasPowered = mIsPowered; |
| 292 | mIsPowered = mBatteryService.isPowered(); |
| 293 | |
| 294 | if (mIsPowered != wasPowered) { |
| 295 | // update mStayOnWhilePluggedIn wake lock |
| 296 | updateWakeLockLocked(); |
| 297 | |
| 298 | // treat plugging and unplugging the devices as a user activity. |
| 299 | // users find it disconcerting when they unplug the device |
| 300 | // and it shuts off right away. |
| 301 | // temporarily set mUserActivityAllowed to true so this will work |
| 302 | // even when the keyguard is on. |
| 303 | synchronized (mLocks) { |
| 304 | boolean savedActivityAllowed = mUserActivityAllowed; |
| 305 | mUserActivityAllowed = true; |
| 306 | userActivity(SystemClock.uptimeMillis(), false); |
| 307 | mUserActivityAllowed = savedActivityAllowed; |
| 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 | { |
| Mike Lockwood | ddfe879 | 2009-08-27 13:27:08 -0700 | [diff] [blame] | 957 | if (mProximitySensorActive) { |
| 958 | why = WindowManagerPolicy.OFF_BECAUSE_OF_PROXIMITY_SENSOR; |
| 959 | } |
| Joe Onorato | 64c62ba | 2009-03-24 20:13:57 -0700 | [diff] [blame] | 960 | if (!on) { |
| 961 | mStillNeedSleepNotification = false; |
| 962 | } |
| 963 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 964 | // Add to the queue. |
| 965 | int index = 0; |
| 966 | while (mBroadcastQueue[index] != -1) { |
| 967 | index++; |
| 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 | mBroadcastQueue[index] = on ? 1 : 0; |
| 970 | mBroadcastWhy[index] = why; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 972 | // If we added it position 2, then there is a pair that can be stripped. |
| 973 | // If we added it position 1 and we're turning the screen off, we can strip |
| 974 | // the pair and do nothing, because the screen is already off, and therefore |
| 975 | // keyguard has already been enabled. |
| 976 | // However, if we added it at position 1 and we're turning it on, then position |
| 977 | // 0 was to turn it off, and we can't strip that, because keyguard needs to come |
| 978 | // on, so have to run the queue then. |
| 979 | if (index == 2) { |
| 980 | // Also, while we're collapsing them, if it's going to be an "off," and one |
| 981 | // is off because of user, then use that, regardless of whether it's the first |
| 982 | // or second one. |
| 983 | if (!on && why == WindowManagerPolicy.OFF_BECAUSE_OF_USER) { |
| 984 | mBroadcastWhy[0] = WindowManagerPolicy.OFF_BECAUSE_OF_USER; |
| 985 | } |
| 986 | mBroadcastQueue[0] = on ? 1 : 0; |
| 987 | mBroadcastQueue[1] = -1; |
| 988 | mBroadcastQueue[2] = -1; |
| 989 | index = 0; |
| 990 | } |
| 991 | if (index == 1 && !on) { |
| 992 | mBroadcastQueue[0] = -1; |
| 993 | mBroadcastQueue[1] = -1; |
| 994 | index = -1; |
| 995 | // The wake lock was being held, but we're not actually going to do any |
| 996 | // broadcasts, so release the wake lock. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount); |
| 998 | mBroadcastWakeLock.release(); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | // Now send the message. |
| 1002 | if (index >= 0) { |
| 1003 | // Acquire the broadcast wake lock before changing the power |
| 1004 | // state. It will be release after the broadcast is sent. |
| 1005 | // We always increment the ref count for each notification in the queue |
| 1006 | // and always decrement when that notification is handled. |
| 1007 | mBroadcastWakeLock.acquire(); |
| 1008 | EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount); |
| 1009 | mHandler.post(mNotificationTask); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | private Runnable mNotificationTask = new Runnable() |
| 1014 | { |
| 1015 | public void run() |
| 1016 | { |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1017 | while (true) { |
| 1018 | int value; |
| 1019 | int why; |
| 1020 | WindowManagerPolicy policy; |
| 1021 | synchronized (mLocks) { |
| 1022 | value = mBroadcastQueue[0]; |
| 1023 | why = mBroadcastWhy[0]; |
| 1024 | for (int i=0; i<2; i++) { |
| 1025 | mBroadcastQueue[i] = mBroadcastQueue[i+1]; |
| 1026 | mBroadcastWhy[i] = mBroadcastWhy[i+1]; |
| 1027 | } |
| 1028 | policy = getPolicyLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | } |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1030 | if (value == 1) { |
| 1031 | mScreenOnStart = SystemClock.uptimeMillis(); |
| 1032 | |
| 1033 | policy.screenTurnedOn(); |
| 1034 | try { |
| 1035 | ActivityManagerNative.getDefault().wakingUp(); |
| 1036 | } catch (RemoteException e) { |
| 1037 | // ignore it |
| 1038 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1040 | if (mSpew) { |
| 1041 | Log.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock); |
| 1042 | } |
| 1043 | if (mContext != null && ActivityManagerNative.isSystemReady()) { |
| 1044 | mContext.sendOrderedBroadcast(mScreenOnIntent, null, |
| 1045 | mScreenOnBroadcastDone, mHandler, 0, null, null); |
| 1046 | } else { |
| 1047 | synchronized (mLocks) { |
| 1048 | EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 2, |
| 1049 | mBroadcastWakeLock.mCount); |
| 1050 | mBroadcastWakeLock.release(); |
| 1051 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | } |
| 1053 | } |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1054 | else if (value == 0) { |
| 1055 | mScreenOffStart = SystemClock.uptimeMillis(); |
| 1056 | |
| 1057 | policy.screenTurnedOff(why); |
| 1058 | try { |
| 1059 | ActivityManagerNative.getDefault().goingToSleep(); |
| 1060 | } catch (RemoteException e) { |
| 1061 | // ignore it. |
| 1062 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1064 | if (mContext != null && ActivityManagerNative.isSystemReady()) { |
| 1065 | mContext.sendOrderedBroadcast(mScreenOffIntent, null, |
| 1066 | mScreenOffBroadcastDone, mHandler, 0, null, null); |
| 1067 | } else { |
| 1068 | synchronized (mLocks) { |
| 1069 | EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_STOP, 3, |
| 1070 | mBroadcastWakeLock.mCount); |
| 1071 | mBroadcastWakeLock.release(); |
| 1072 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | } |
| 1074 | } |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1075 | else { |
| 1076 | // If we're in this case, then this handler is running for a previous |
| 1077 | // paired transaction. mBroadcastWakeLock will already have been released. |
| 1078 | break; |
| 1079 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | }; |
| 1083 | |
| 1084 | long mScreenOnStart; |
| 1085 | private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() { |
| 1086 | public void onReceive(Context context, Intent intent) { |
| 1087 | synchronized (mLocks) { |
| 1088 | EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_DONE, 1, |
| 1089 | SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount); |
| 1090 | mBroadcastWakeLock.release(); |
| 1091 | } |
| 1092 | } |
| 1093 | }; |
| 1094 | |
| 1095 | long mScreenOffStart; |
| 1096 | private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() { |
| 1097 | public void onReceive(Context context, Intent intent) { |
| 1098 | synchronized (mLocks) { |
| 1099 | EventLog.writeEvent(LOG_POWER_SCREEN_BROADCAST_DONE, 0, |
| 1100 | SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount); |
| 1101 | mBroadcastWakeLock.release(); |
| 1102 | } |
| 1103 | } |
| 1104 | }; |
| 1105 | |
| 1106 | void logPointerUpEvent() { |
| 1107 | if (LOG_TOUCH_DOWNS) { |
| 1108 | mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown; |
| 1109 | mLastTouchDown = 0; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | void logPointerDownEvent() { |
| 1114 | if (LOG_TOUCH_DOWNS) { |
| 1115 | // If we are not already timing a down/up sequence |
| 1116 | if (mLastTouchDown == 0) { |
| 1117 | mLastTouchDown = SystemClock.elapsedRealtime(); |
| 1118 | mTouchCycles++; |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * Prevents the screen from turning on even if it *should* turn on due |
| 1125 | * to a subsequent full wake lock being acquired. |
| 1126 | * <p> |
| 1127 | * This is a temporary hack that allows an activity to "cover up" any |
| 1128 | * display glitches that happen during the activity's startup |
| 1129 | * sequence. (Specifically, this API was added to work around a |
| 1130 | * cosmetic bug in the "incoming call" sequence, where the lock screen |
| 1131 | * would flicker briefly before the incoming call UI became visible.) |
| 1132 | * TODO: There ought to be a more elegant way of doing this, |
| 1133 | * probably by having the PowerManager and ActivityManager |
| 1134 | * work together to let apps specify that the screen on/off |
| 1135 | * state should be synchronized with the Activity lifecycle. |
| 1136 | * <p> |
| 1137 | * Note that calling preventScreenOn(true) will NOT turn the screen |
| 1138 | * off if it's currently on. (This API only affects *future* |
| 1139 | * acquisitions of full wake locks.) |
| 1140 | * But calling preventScreenOn(false) WILL turn the screen on if |
| 1141 | * it's currently off because of a prior preventScreenOn(true) call. |
| 1142 | * <p> |
| 1143 | * Any call to preventScreenOn(true) MUST be followed promptly by a call |
| 1144 | * to preventScreenOn(false). In fact, if the preventScreenOn(false) |
| 1145 | * call doesn't occur within 5 seconds, we'll turn the screen back on |
| 1146 | * ourselves (and log a warning about it); this prevents a buggy app |
| 1147 | * from disabling the screen forever.) |
| 1148 | * <p> |
| 1149 | * TODO: this feature should really be controlled by a new type of poke |
| 1150 | * lock (rather than an IPowerManager call). |
| 1151 | */ |
| 1152 | public void preventScreenOn(boolean prevent) { |
| 1153 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1154 | |
| 1155 | synchronized (mLocks) { |
| 1156 | if (prevent) { |
| 1157 | // First of all, grab a partial wake lock to |
| 1158 | // make sure the CPU stays on during the entire |
| 1159 | // preventScreenOn(true) -> preventScreenOn(false) sequence. |
| 1160 | mPreventScreenOnPartialLock.acquire(); |
| 1161 | |
| 1162 | // Post a forceReenableScreen() call (for 5 seconds in the |
| 1163 | // future) to make sure the matching preventScreenOn(false) call |
| 1164 | // has happened by then. |
| 1165 | mHandler.removeCallbacks(mForceReenableScreenTask); |
| 1166 | mHandler.postDelayed(mForceReenableScreenTask, 5000); |
| 1167 | |
| 1168 | // Finally, set the flag that prevents the screen from turning on. |
| 1169 | // (Below, in setPowerState(), we'll check mPreventScreenOn and |
| 1170 | // we *won't* call Power.setScreenState(true) if it's set.) |
| 1171 | mPreventScreenOn = true; |
| 1172 | } else { |
| 1173 | // (Re)enable the screen. |
| 1174 | mPreventScreenOn = false; |
| 1175 | |
| 1176 | // We're "undoing" a the prior preventScreenOn(true) call, so we |
| 1177 | // no longer need the 5-second safeguard. |
| 1178 | mHandler.removeCallbacks(mForceReenableScreenTask); |
| 1179 | |
| 1180 | // Forcibly turn on the screen if it's supposed to be on. (This |
| 1181 | // handles the case where the screen is currently off because of |
| 1182 | // a prior preventScreenOn(true) call.) |
| 1183 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 1184 | if (mSpew) { |
| 1185 | Log.d(TAG, |
| 1186 | "preventScreenOn: turning on after a prior preventScreenOn(true)!"); |
| 1187 | } |
| 1188 | int err = Power.setScreenState(true); |
| 1189 | if (err != 0) { |
| 1190 | Log.w(TAG, "preventScreenOn: error from Power.setScreenState(): " + err); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | // Release the partial wake lock that we held during the |
| 1195 | // preventScreenOn(true) -> preventScreenOn(false) sequence. |
| 1196 | mPreventScreenOnPartialLock.release(); |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | public void setScreenBrightnessOverride(int brightness) { |
| 1202 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1203 | |
| 1204 | synchronized (mLocks) { |
| 1205 | if (mScreenBrightnessOverride != brightness) { |
| 1206 | mScreenBrightnessOverride = brightness; |
| 1207 | updateLightsLocked(mPowerState, SCREEN_ON_BIT); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * Sanity-check that gets called 5 seconds after any call to |
| 1214 | * preventScreenOn(true). This ensures that the original call |
| 1215 | * is followed promptly by a call to preventScreenOn(false). |
| 1216 | */ |
| 1217 | private void forceReenableScreen() { |
| 1218 | // We shouldn't get here at all if mPreventScreenOn is false, since |
| 1219 | // we should have already removed any existing |
| 1220 | // mForceReenableScreenTask messages... |
| 1221 | if (!mPreventScreenOn) { |
| 1222 | Log.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do"); |
| 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | // Uh oh. It's been 5 seconds since a call to |
| 1227 | // preventScreenOn(true) and we haven't re-enabled the screen yet. |
| 1228 | // This means the app that called preventScreenOn(true) is either |
| 1229 | // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)), |
| 1230 | // or buggy (i.e. it forgot to call preventScreenOn(false), or |
| 1231 | // crashed before doing so.) |
| 1232 | |
| 1233 | // Log a warning, and forcibly turn the screen back on. |
| 1234 | Log.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! " |
| 1235 | + "Forcing the screen back on..."); |
| 1236 | preventScreenOn(false); |
| 1237 | } |
| 1238 | |
| 1239 | private Runnable mForceReenableScreenTask = new Runnable() { |
| 1240 | public void run() { |
| 1241 | forceReenableScreen(); |
| 1242 | } |
| 1243 | }; |
| 1244 | |
| 1245 | private void setPowerState(int state) |
| 1246 | { |
| 1247 | setPowerState(state, false, false); |
| 1248 | } |
| 1249 | |
| 1250 | private void setPowerState(int newState, boolean noChangeLights, boolean becauseOfUser) |
| 1251 | { |
| 1252 | synchronized (mLocks) { |
| 1253 | int err; |
| 1254 | |
| 1255 | if (mSpew) { |
| 1256 | Log.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState) |
| 1257 | + " newState=0x" + Integer.toHexString(newState) |
| 1258 | + " noChangeLights=" + noChangeLights); |
| 1259 | } |
| 1260 | |
| 1261 | if (noChangeLights) { |
| 1262 | newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK); |
| 1263 | } |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 1264 | if (mProximitySensorActive) { |
| 1265 | // don't turn on the screen when the proximity sensor lock is held |
| 1266 | newState = (newState & ~SCREEN_BRIGHT); |
| 1267 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1268 | |
| 1269 | if (batteryIsLow()) { |
| 1270 | newState |= BATTERY_LOW_BIT; |
| 1271 | } else { |
| 1272 | newState &= ~BATTERY_LOW_BIT; |
| 1273 | } |
| 1274 | if (newState == mPowerState) { |
| 1275 | return; |
| 1276 | } |
| 1277 | |
| 1278 | if (!mDoneBooting) { |
| 1279 | newState |= ALL_BRIGHT; |
| 1280 | } |
| 1281 | |
| 1282 | boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0; |
| 1283 | boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0; |
| 1284 | |
| 1285 | if (mSpew) { |
| 1286 | Log.d(TAG, "setPowerState: mPowerState=" + mPowerState |
| 1287 | + " newState=" + newState + " noChangeLights=" + noChangeLights); |
| 1288 | Log.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0) |
| 1289 | + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0)); |
| 1290 | Log.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0) |
| 1291 | + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0)); |
| 1292 | Log.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0) |
| 1293 | + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0)); |
| 1294 | Log.d(TAG, " oldScreenOn=" + oldScreenOn |
| 1295 | + " newScreenOn=" + newScreenOn); |
| 1296 | Log.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0) |
| 1297 | + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0)); |
| 1298 | } |
| 1299 | |
| 1300 | if (mPowerState != newState) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1301 | updateLightsLocked(newState, 0); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1302 | mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK); |
| 1303 | } |
| 1304 | |
| 1305 | if (oldScreenOn != newScreenOn) { |
| 1306 | if (newScreenOn) { |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1307 | // When the user presses the power button, we need to always send out the |
| 1308 | // notification that it's going to sleep so the keyguard goes on. But |
| 1309 | // we can't do that until the screen fades out, so we don't show the keyguard |
| 1310 | // too early. |
| 1311 | if (mStillNeedSleepNotification) { |
| 1312 | sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER); |
| 1313 | } |
| 1314 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | // Turn on the screen UNLESS there was a prior |
| 1316 | // preventScreenOn(true) request. (Note that the lifetime |
| 1317 | // of a single preventScreenOn() request is limited to 5 |
| 1318 | // seconds to prevent a buggy app from disabling the |
| 1319 | // screen forever; see forceReenableScreen().) |
| 1320 | boolean reallyTurnScreenOn = true; |
| 1321 | if (mSpew) { |
| 1322 | Log.d(TAG, "- turning screen on... mPreventScreenOn = " |
| 1323 | + mPreventScreenOn); |
| 1324 | } |
| 1325 | |
| 1326 | if (mPreventScreenOn) { |
| 1327 | if (mSpew) { |
| 1328 | Log.d(TAG, "- PREVENTING screen from really turning on!"); |
| 1329 | } |
| 1330 | reallyTurnScreenOn = false; |
| 1331 | } |
| 1332 | if (reallyTurnScreenOn) { |
| 1333 | err = Power.setScreenState(true); |
| 1334 | long identity = Binder.clearCallingIdentity(); |
| 1335 | try { |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1336 | mBatteryStats.noteScreenBrightness( |
| 1337 | getPreferredBrightness()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | mBatteryStats.noteScreenOn(); |
| 1339 | } catch (RemoteException e) { |
| 1340 | Log.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e); |
| 1341 | } finally { |
| 1342 | Binder.restoreCallingIdentity(identity); |
| 1343 | } |
| 1344 | } else { |
| 1345 | Power.setScreenState(false); |
| 1346 | // But continue as if we really did turn the screen on... |
| 1347 | err = 0; |
| 1348 | } |
| 1349 | |
| 1350 | mScreenOnStartTime = SystemClock.elapsedRealtime(); |
| 1351 | mLastTouchDown = 0; |
| 1352 | mTotalTouchDownTime = 0; |
| 1353 | mTouchCycles = 0; |
| 1354 | EventLog.writeEvent(LOG_POWER_SCREEN_STATE, 1, becauseOfUser ? 1 : 0, |
| 1355 | mTotalTouchDownTime, mTouchCycles); |
| 1356 | if (err == 0) { |
| 1357 | mPowerState |= SCREEN_ON_BIT; |
| 1358 | sendNotificationLocked(true, -1); |
| 1359 | } |
| 1360 | } else { |
| 1361 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 1362 | long identity = Binder.clearCallingIdentity(); |
| 1363 | try { |
| 1364 | mBatteryStats.noteScreenOff(); |
| 1365 | } catch (RemoteException e) { |
| 1366 | Log.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e); |
| 1367 | } finally { |
| 1368 | Binder.restoreCallingIdentity(identity); |
| 1369 | } |
| 1370 | mPowerState &= ~SCREEN_ON_BIT; |
| 1371 | if (!mScreenBrightness.animating) { |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1372 | err = screenOffFinishedAnimatingLocked(becauseOfUser); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | } else { |
| 1374 | mOffBecauseOfUser = becauseOfUser; |
| 1375 | err = 0; |
| 1376 | mLastTouchDown = 0; |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1383 | private int screenOffFinishedAnimatingLocked(boolean becauseOfUser) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | // I don't think we need to check the current state here because all of these |
| 1385 | // Power.setScreenState and sendNotificationLocked can both handle being |
| 1386 | // called multiple times in the same state. -joeo |
| 1387 | EventLog.writeEvent(LOG_POWER_SCREEN_STATE, 0, becauseOfUser ? 1 : 0, |
| 1388 | mTotalTouchDownTime, mTouchCycles); |
| 1389 | mLastTouchDown = 0; |
| 1390 | int err = Power.setScreenState(false); |
| 1391 | if (mScreenOnStartTime != 0) { |
| 1392 | mScreenOnTime += SystemClock.elapsedRealtime() - mScreenOnStartTime; |
| 1393 | mScreenOnStartTime = 0; |
| 1394 | } |
| 1395 | if (err == 0) { |
| 1396 | int why = becauseOfUser |
| 1397 | ? WindowManagerPolicy.OFF_BECAUSE_OF_USER |
| 1398 | : WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT; |
| 1399 | sendNotificationLocked(false, why); |
| 1400 | } |
| 1401 | return err; |
| 1402 | } |
| 1403 | |
| 1404 | private boolean batteryIsLow() { |
| 1405 | return (!mIsPowered && |
| 1406 | mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD); |
| 1407 | } |
| 1408 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1409 | private void updateLightsLocked(int newState, int forceState) { |
| Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1410 | final int oldState = mPowerState; |
| 1411 | final int realDifference = (newState ^ oldState); |
| 1412 | final int difference = realDifference | forceState; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | if (difference == 0) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1414 | return; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | int offMask = 0; |
| 1418 | int dimMask = 0; |
| 1419 | int onMask = 0; |
| 1420 | |
| 1421 | int preferredBrightness = getPreferredBrightness(); |
| 1422 | boolean startAnimation = false; |
| 1423 | |
| 1424 | if ((difference & KEYBOARD_BRIGHT_BIT) != 0) { |
| 1425 | if (ANIMATE_KEYBOARD_LIGHTS) { |
| 1426 | if ((newState & KEYBOARD_BRIGHT_BIT) == 0) { |
| 1427 | mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_OFF, |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1428 | ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS, |
| 1429 | preferredBrightness); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | } else { |
| 1431 | mKeyboardBrightness.setTargetLocked(preferredBrightness, |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1432 | ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS, |
| 1433 | Power.BRIGHTNESS_OFF); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | } |
| 1435 | startAnimation = true; |
| 1436 | } else { |
| 1437 | if ((newState & KEYBOARD_BRIGHT_BIT) == 0) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1438 | offMask |= KEYBOARD_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | } else { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1440 | onMask |= KEYBOARD_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if ((difference & BUTTON_BRIGHT_BIT) != 0) { |
| 1446 | if (ANIMATE_BUTTON_LIGHTS) { |
| 1447 | if ((newState & BUTTON_BRIGHT_BIT) == 0) { |
| 1448 | mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_OFF, |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1449 | ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS, |
| 1450 | preferredBrightness); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | } else { |
| 1452 | mButtonBrightness.setTargetLocked(preferredBrightness, |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1453 | ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS, |
| 1454 | Power.BRIGHTNESS_OFF); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1455 | } |
| 1456 | startAnimation = true; |
| 1457 | } else { |
| 1458 | if ((newState & BUTTON_BRIGHT_BIT) == 0) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1459 | offMask |= BUTTON_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | } else { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1461 | onMask |= BUTTON_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) { |
| 1467 | if (ANIMATE_SCREEN_LIGHTS) { |
| Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1468 | int nominalCurrentValue = -1; |
| 1469 | // If there was an actual difference in the light state, then |
| 1470 | // figure out the "ideal" current value based on the previous |
| 1471 | // state. Otherwise, this is a change due to the brightness |
| 1472 | // override, so we want to animate from whatever the current |
| 1473 | // value is. |
| 1474 | if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) { |
| 1475 | switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) { |
| 1476 | case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT: |
| 1477 | nominalCurrentValue = preferredBrightness; |
| 1478 | break; |
| 1479 | case SCREEN_ON_BIT: |
| 1480 | nominalCurrentValue = Power.BRIGHTNESS_DIM; |
| 1481 | break; |
| 1482 | case 0: |
| 1483 | nominalCurrentValue = Power.BRIGHTNESS_OFF; |
| 1484 | break; |
| 1485 | case SCREEN_BRIGHT_BIT: |
| 1486 | default: |
| 1487 | // not possible |
| 1488 | nominalCurrentValue = (int)mScreenBrightness.curValue; |
| 1489 | break; |
| 1490 | } |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1491 | } |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1492 | int brightness = preferredBrightness; |
| 1493 | int steps = ANIM_STEPS; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | if ((newState & SCREEN_BRIGHT_BIT) == 0) { |
| 1495 | // dim or turn off backlight, depending on if the screen is on |
| 1496 | // the scale is because the brightness ramp isn't linear and this biases |
| 1497 | // it so the later parts take longer. |
| 1498 | final float scale = 1.5f; |
| 1499 | float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness); |
| 1500 | if (ratio > 1.0f) ratio = 1.0f; |
| 1501 | if ((newState & SCREEN_ON_BIT) == 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | if ((oldState & SCREEN_BRIGHT_BIT) != 0) { |
| 1503 | // was bright |
| 1504 | steps = ANIM_STEPS; |
| 1505 | } else { |
| 1506 | // was dim |
| 1507 | steps = (int)(ANIM_STEPS*ratio*scale); |
| 1508 | } |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1509 | brightness = Power.BRIGHTNESS_OFF; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | } else { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1511 | if ((oldState & SCREEN_ON_BIT) != 0) { |
| 1512 | // was bright |
| 1513 | steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale); |
| 1514 | } else { |
| 1515 | // was dim |
| 1516 | steps = (int)(ANIM_STEPS*ratio); |
| 1517 | } |
| 1518 | if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) { |
| 1519 | // If the "stay on while plugged in" option is |
| 1520 | // turned on, then the screen will often not |
| 1521 | // automatically turn off while plugged in. To |
| 1522 | // still have a sense of when it is inactive, we |
| 1523 | // will then count going dim as turning off. |
| 1524 | mScreenOffTime = SystemClock.elapsedRealtime(); |
| 1525 | } |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1526 | brightness = Power.BRIGHTNESS_DIM; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | } |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1529 | long identity = Binder.clearCallingIdentity(); |
| 1530 | try { |
| 1531 | mBatteryStats.noteScreenBrightness(brightness); |
| 1532 | } catch (RemoteException e) { |
| 1533 | // Nothing interesting to do. |
| 1534 | } finally { |
| 1535 | Binder.restoreCallingIdentity(identity); |
| 1536 | } |
| 1537 | mScreenBrightness.setTargetLocked(brightness, |
| 1538 | steps, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | startAnimation = true; |
| 1540 | } else { |
| 1541 | if ((newState & SCREEN_BRIGHT_BIT) == 0) { |
| 1542 | // dim or turn off backlight, depending on if the screen is on |
| 1543 | if ((newState & SCREEN_ON_BIT) == 0) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1544 | offMask |= SCREEN_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | } else { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1546 | dimMask |= SCREEN_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | } |
| 1548 | } else { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1549 | onMask |= SCREEN_BRIGHT_BIT; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1550 | } |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | if (startAnimation) { |
| 1555 | if (mSpew) { |
| 1556 | Log.i(TAG, "Scheduling light animator!"); |
| 1557 | } |
| 1558 | mHandler.removeCallbacks(mLightAnimator); |
| 1559 | mHandler.post(mLightAnimator); |
| 1560 | } |
| 1561 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1562 | if (offMask != 0) { |
| 1563 | //Log.i(TAG, "Setting brightess off: " + offMask); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1564 | setLightBrightness(offMask, Power.BRIGHTNESS_OFF); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | } |
| 1566 | if (dimMask != 0) { |
| 1567 | int brightness = Power.BRIGHTNESS_DIM; |
| 1568 | if ((newState & BATTERY_LOW_BIT) != 0 && |
| 1569 | brightness > Power.BRIGHTNESS_LOW_BATTERY) { |
| 1570 | brightness = Power.BRIGHTNESS_LOW_BATTERY; |
| 1571 | } |
| 1572 | //Log.i(TAG, "Setting brightess dim " + brightness + ": " + offMask); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1573 | setLightBrightness(dimMask, brightness); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | } |
| 1575 | if (onMask != 0) { |
| 1576 | int brightness = getPreferredBrightness(); |
| 1577 | if ((newState & BATTERY_LOW_BIT) != 0 && |
| 1578 | brightness > Power.BRIGHTNESS_LOW_BATTERY) { |
| 1579 | brightness = Power.BRIGHTNESS_LOW_BATTERY; |
| 1580 | } |
| 1581 | //Log.i(TAG, "Setting brightess on " + brightness + ": " + onMask); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1582 | setLightBrightness(onMask, brightness); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1583 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1584 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1586 | private void setLightBrightness(int mask, int value) { |
| 1587 | if ((mask & SCREEN_BRIGHT_BIT) != 0) { |
| 1588 | mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_BACKLIGHT, value); |
| 1589 | } |
| 1590 | if ((mask & BUTTON_BRIGHT_BIT) != 0) { |
| 1591 | mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_BUTTONS, value); |
| 1592 | } |
| 1593 | if ((mask & KEYBOARD_BRIGHT_BIT) != 0) { |
| 1594 | mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_KEYBOARD, value); |
| 1595 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | class BrightnessState { |
| 1599 | final int mask; |
| 1600 | |
| 1601 | boolean initialized; |
| 1602 | int targetValue; |
| 1603 | float curValue; |
| 1604 | float delta; |
| 1605 | boolean animating; |
| 1606 | |
| 1607 | BrightnessState(int m) { |
| 1608 | mask = m; |
| 1609 | } |
| 1610 | |
| 1611 | public void dump(PrintWriter pw, String prefix) { |
| 1612 | pw.println(prefix + "animating=" + animating |
| 1613 | + " targetValue=" + targetValue |
| 1614 | + " curValue=" + curValue |
| 1615 | + " delta=" + delta); |
| 1616 | } |
| 1617 | |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1618 | void setTargetLocked(int target, int stepsToTarget, int initialValue, |
| 1619 | int nominalCurrentValue) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | if (!initialized) { |
| 1621 | initialized = true; |
| 1622 | curValue = (float)initialValue; |
| 1623 | } |
| 1624 | targetValue = target; |
| Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1625 | delta = (targetValue - |
| 1626 | (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue)) |
| 1627 | / stepsToTarget; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | if (mSpew) { |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1629 | String noticeMe = nominalCurrentValue == curValue ? "" : " ******************"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | Log.i(TAG, "Setting target " + mask + ": cur=" + curValue |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1631 | + " target=" + targetValue + " delta=" + delta |
| 1632 | + " nominalCurrentValue=" + nominalCurrentValue |
| 1633 | + noticeMe); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1634 | } |
| 1635 | animating = true; |
| 1636 | } |
| 1637 | |
| 1638 | boolean stepLocked() { |
| 1639 | if (!animating) return false; |
| 1640 | if (false && mSpew) { |
| 1641 | Log.i(TAG, "Step target " + mask + ": cur=" + curValue |
| 1642 | + " target=" + targetValue + " delta=" + delta); |
| 1643 | } |
| 1644 | curValue += delta; |
| 1645 | int curIntValue = (int)curValue; |
| 1646 | boolean more = true; |
| 1647 | if (delta == 0) { |
| Dianne Hackborn | 9ed4a4b | 2009-03-25 17:10:37 -0700 | [diff] [blame] | 1648 | curValue = curIntValue = targetValue; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | more = false; |
| 1650 | } else if (delta > 0) { |
| 1651 | if (curIntValue >= targetValue) { |
| 1652 | curValue = curIntValue = targetValue; |
| 1653 | more = false; |
| 1654 | } |
| 1655 | } else { |
| 1656 | if (curIntValue <= targetValue) { |
| 1657 | curValue = curIntValue = targetValue; |
| 1658 | more = false; |
| 1659 | } |
| 1660 | } |
| 1661 | //Log.i(TAG, "Animating brightess " + curIntValue + ": " + mask); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1662 | setLightBrightness(mask, curIntValue); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | animating = more; |
| 1664 | if (!more) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1665 | if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) { |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1666 | screenOffFinishedAnimatingLocked(mOffBecauseOfUser); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | } |
| 1668 | } |
| 1669 | return more; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | private class LightAnimator implements Runnable { |
| 1674 | public void run() { |
| 1675 | synchronized (mLocks) { |
| 1676 | long now = SystemClock.uptimeMillis(); |
| 1677 | boolean more = mScreenBrightness.stepLocked(); |
| 1678 | if (mKeyboardBrightness.stepLocked()) { |
| 1679 | more = true; |
| 1680 | } |
| 1681 | if (mButtonBrightness.stepLocked()) { |
| 1682 | more = true; |
| 1683 | } |
| 1684 | if (more) { |
| 1685 | mHandler.postAtTime(mLightAnimator, now+(1000/60)); |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | private int getPreferredBrightness() { |
| 1692 | try { |
| 1693 | if (mScreenBrightnessOverride >= 0) { |
| 1694 | return mScreenBrightnessOverride; |
| 1695 | } |
| 1696 | final int brightness = Settings.System.getInt(mContext.getContentResolver(), |
| 1697 | SCREEN_BRIGHTNESS); |
| 1698 | // Don't let applications turn the screen all the way off |
| 1699 | return Math.max(brightness, Power.BRIGHTNESS_DIM); |
| 1700 | } catch (SettingNotFoundException snfe) { |
| 1701 | return Power.BRIGHTNESS_ON; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | boolean screenIsOn() { |
| 1706 | synchronized (mLocks) { |
| 1707 | return (mPowerState & SCREEN_ON_BIT) != 0; |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | boolean screenIsBright() { |
| 1712 | synchronized (mLocks) { |
| 1713 | return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | public void userActivityWithForce(long time, boolean noChangeLights, boolean force) { |
| 1718 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1719 | userActivity(time, noChangeLights, OTHER_EVENT, force); |
| 1720 | } |
| 1721 | |
| 1722 | public void userActivity(long time, boolean noChangeLights) { |
| 1723 | userActivity(time, noChangeLights, OTHER_EVENT, false); |
| 1724 | } |
| 1725 | |
| 1726 | public void userActivity(long time, boolean noChangeLights, int eventType) { |
| 1727 | userActivity(time, noChangeLights, eventType, false); |
| 1728 | } |
| 1729 | |
| 1730 | public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) { |
| 1731 | //mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1732 | |
| 1733 | if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0) |
| Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 1734 | && (eventType == CHEEK_EVENT || eventType == TOUCH_EVENT)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1735 | if (false) { |
| Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 1736 | 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] | 1737 | } |
| 1738 | return; |
| 1739 | } |
| 1740 | |
| Joe Onorato | e68ffcb | 2009-03-24 19:11:13 -0700 | [diff] [blame] | 1741 | if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0) |
| 1742 | && (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT |
| 1743 | || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) { |
| 1744 | if (false) { |
| 1745 | Log.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey)); |
| 1746 | } |
| 1747 | return; |
| 1748 | } |
| 1749 | |
| 1750 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | if (false) { |
| 1752 | if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) { |
| 1753 | Log.d(TAG, "userActivity !!!");//, new RuntimeException()); |
| 1754 | } else { |
| 1755 | Log.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey)); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | synchronized (mLocks) { |
| 1760 | if (mSpew) { |
| 1761 | Log.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time |
| 1762 | + " mUserActivityAllowed=" + mUserActivityAllowed |
| 1763 | + " mUserState=0x" + Integer.toHexString(mUserState) |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 1764 | + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState) |
| 1765 | + " mProximitySensorActive=" + mProximitySensorActive |
| 1766 | + " force=" + force); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1767 | } |
| 1768 | if (mLastEventTime <= time || force) { |
| 1769 | mLastEventTime = time; |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 1770 | if ((mUserActivityAllowed && !mProximitySensorActive) || force) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | // Only turn on button backlights if a button was pressed. |
| 1772 | if (eventType == BUTTON_EVENT) { |
| 1773 | mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT); |
| 1774 | } else { |
| 1775 | // don't clear button/keyboard backlights when the screen is touched. |
| 1776 | mUserState |= SCREEN_BRIGHT; |
| 1777 | } |
| 1778 | |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 1779 | int uid = Binder.getCallingUid(); |
| 1780 | long ident = Binder.clearCallingIdentity(); |
| 1781 | try { |
| 1782 | mBatteryStats.noteUserActivity(uid, eventType); |
| 1783 | } catch (RemoteException e) { |
| 1784 | // Ignore |
| 1785 | } finally { |
| 1786 | Binder.restoreCallingIdentity(ident); |
| 1787 | } |
| 1788 | |
| Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 1789 | mWakeLockState = mLocks.reactivateScreenLocksLocked(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1790 | setPowerState(mUserState | mWakeLockState, noChangeLights, true); |
| 1791 | setTimeoutLocked(time, SCREEN_BRIGHT); |
| 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * The user requested that we go to sleep (probably with the power button). |
| 1799 | * This overrides all wake locks that are held. |
| 1800 | */ |
| 1801 | public void goToSleep(long time) |
| 1802 | { |
| 1803 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null); |
| 1804 | synchronized (mLocks) { |
| 1805 | goToSleepLocked(time); |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | /** |
| 1810 | * Returns the time the screen has been on since boot, in millis. |
| 1811 | * @return screen on time |
| 1812 | */ |
| 1813 | public long getScreenOnTime() { |
| 1814 | synchronized (mLocks) { |
| 1815 | if (mScreenOnStartTime == 0) { |
| 1816 | return mScreenOnTime; |
| 1817 | } else { |
| 1818 | return SystemClock.elapsedRealtime() - mScreenOnStartTime + mScreenOnTime; |
| 1819 | } |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | private void goToSleepLocked(long time) { |
| 1824 | |
| 1825 | if (mLastEventTime <= time) { |
| 1826 | mLastEventTime = time; |
| 1827 | // cancel all of the wake locks |
| 1828 | mWakeLockState = SCREEN_OFF; |
| 1829 | int N = mLocks.size(); |
| 1830 | int numCleared = 0; |
| 1831 | for (int i=0; i<N; i++) { |
| 1832 | WakeLock wl = mLocks.get(i); |
| 1833 | if (isScreenLock(wl.flags)) { |
| 1834 | mLocks.get(i).activated = false; |
| 1835 | numCleared++; |
| 1836 | } |
| 1837 | } |
| 1838 | EventLog.writeEvent(LOG_POWER_SLEEP_REQUESTED, numCleared); |
| Joe Onorato | 128e729 | 2009-03-24 18:41:31 -0700 | [diff] [blame] | 1839 | mStillNeedSleepNotification = true; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1840 | mUserState = SCREEN_OFF; |
| 1841 | setPowerState(SCREEN_OFF, false, true); |
| 1842 | cancelTimerLocked(); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | public long timeSinceScreenOn() { |
| 1847 | synchronized (mLocks) { |
| 1848 | if ((mPowerState & SCREEN_ON_BIT) != 0) { |
| 1849 | return 0; |
| 1850 | } |
| 1851 | return SystemClock.elapsedRealtime() - mScreenOffTime; |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | public void setKeyboardVisibility(boolean visible) { |
| Mike Lockwood | a625b38 | 2009-09-12 17:36:03 -0700 | [diff] [blame] | 1856 | synchronized (mLocks) { |
| 1857 | if (mSpew) { |
| 1858 | Log.d(TAG, "setKeyboardVisibility: " + visible); |
| 1859 | } |
| 1860 | mKeyboardVisible = visible; |
| 1861 | // don't signal user activity when closing keyboard if the screen is off. |
| 1862 | // otherwise, we want to make sure the backlights are adjusted. |
| 1863 | if (visible || (mPowerState & SCREEN_ON_BIT) != 0) { |
| 1864 | userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true); |
| 1865 | } |
| 1866 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | /** |
| 1870 | * When the keyguard is up, it manages the power state, and userActivity doesn't do anything. |
| 1871 | */ |
| 1872 | public void enableUserActivity(boolean enabled) { |
| 1873 | synchronized (mLocks) { |
| 1874 | mUserActivityAllowed = enabled; |
| 1875 | mLastEventTime = SystemClock.uptimeMillis(); // we might need to pass this in |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | /** Sets the screen off timeouts: |
| 1880 | * mKeylightDelay |
| 1881 | * mDimDelay |
| 1882 | * mScreenOffDelay |
| 1883 | * */ |
| 1884 | private void setScreenOffTimeoutsLocked() { |
| 1885 | if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) { |
| 1886 | mKeylightDelay = mShortKeylightDelay; // Configurable via Gservices |
| 1887 | mDimDelay = -1; |
| 1888 | mScreenOffDelay = 0; |
| 1889 | } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) { |
| 1890 | mKeylightDelay = MEDIUM_KEYLIGHT_DELAY; |
| 1891 | mDimDelay = -1; |
| 1892 | mScreenOffDelay = 0; |
| 1893 | } else { |
| 1894 | int totalDelay = mTotalDelaySetting; |
| 1895 | mKeylightDelay = LONG_KEYLIGHT_DELAY; |
| 1896 | if (totalDelay < 0) { |
| 1897 | mScreenOffDelay = Integer.MAX_VALUE; |
| 1898 | } else if (mKeylightDelay < totalDelay) { |
| 1899 | // subtract the time that the keylight delay. This will give us the |
| 1900 | // remainder of the time that we need to sleep to get the accurate |
| 1901 | // screen off timeout. |
| 1902 | mScreenOffDelay = totalDelay - mKeylightDelay; |
| 1903 | } else { |
| 1904 | mScreenOffDelay = 0; |
| 1905 | } |
| 1906 | if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) { |
| 1907 | mDimDelay = mScreenOffDelay - LONG_DIM_TIME; |
| 1908 | mScreenOffDelay = LONG_DIM_TIME; |
| 1909 | } else { |
| 1910 | mDimDelay = -1; |
| 1911 | } |
| 1912 | } |
| 1913 | if (mSpew) { |
| 1914 | Log.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay |
| 1915 | + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay |
| 1916 | + " mDimScreen=" + mDimScreen); |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | /** |
| 1921 | * Refreshes cached Gservices settings. Called once on startup, and |
| 1922 | * on subsequent Settings.Gservices.CHANGED_ACTION broadcasts (see |
| 1923 | * GservicesChangedReceiver). |
| 1924 | */ |
| 1925 | private void updateGservicesValues() { |
| 1926 | mShortKeylightDelay = Settings.Gservices.getInt( |
| 1927 | mContext.getContentResolver(), |
| 1928 | Settings.Gservices.SHORT_KEYLIGHT_DELAY_MS, |
| 1929 | SHORT_KEYLIGHT_DELAY_DEFAULT); |
| 1930 | // Log.i(TAG, "updateGservicesValues(): mShortKeylightDelay now " + mShortKeylightDelay); |
| 1931 | } |
| 1932 | |
| 1933 | /** |
| 1934 | * Receiver for the Gservices.CHANGED_ACTION broadcast intent, |
| 1935 | * which tells us we need to refresh our cached Gservices settings. |
| 1936 | */ |
| 1937 | private class GservicesChangedReceiver extends BroadcastReceiver { |
| 1938 | @Override |
| 1939 | public void onReceive(Context context, Intent intent) { |
| 1940 | // Log.i(TAG, "GservicesChangedReceiver.onReceive(): " + intent); |
| 1941 | updateGservicesValues(); |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | private class LockList extends ArrayList<WakeLock> |
| 1946 | { |
| 1947 | void addLock(WakeLock wl) |
| 1948 | { |
| 1949 | int index = getIndex(wl.binder); |
| 1950 | if (index < 0) { |
| 1951 | this.add(wl); |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | WakeLock removeLock(IBinder binder) |
| 1956 | { |
| 1957 | int index = getIndex(binder); |
| 1958 | if (index >= 0) { |
| 1959 | return this.remove(index); |
| 1960 | } else { |
| 1961 | return null; |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | int getIndex(IBinder binder) |
| 1966 | { |
| 1967 | int N = this.size(); |
| 1968 | for (int i=0; i<N; i++) { |
| 1969 | if (this.get(i).binder == binder) { |
| 1970 | return i; |
| 1971 | } |
| 1972 | } |
| 1973 | return -1; |
| 1974 | } |
| 1975 | |
| 1976 | int gatherState() |
| 1977 | { |
| 1978 | int result = 0; |
| 1979 | int N = this.size(); |
| 1980 | for (int i=0; i<N; i++) { |
| 1981 | WakeLock wl = this.get(i); |
| 1982 | if (wl.activated) { |
| 1983 | if (isScreenLock(wl.flags)) { |
| 1984 | result |= wl.minState; |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | return result; |
| 1989 | } |
| Michael Chan | e96440f | 2009-05-06 10:27:36 -0700 | [diff] [blame] | 1990 | |
| 1991 | int reactivateScreenLocksLocked() |
| 1992 | { |
| 1993 | int result = 0; |
| 1994 | int N = this.size(); |
| 1995 | for (int i=0; i<N; i++) { |
| 1996 | WakeLock wl = this.get(i); |
| 1997 | if (isScreenLock(wl.flags)) { |
| 1998 | wl.activated = true; |
| 1999 | result |= wl.minState; |
| 2000 | } |
| 2001 | } |
| 2002 | return result; |
| 2003 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | void setPolicy(WindowManagerPolicy p) { |
| 2007 | synchronized (mLocks) { |
| 2008 | mPolicy = p; |
| 2009 | mLocks.notifyAll(); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | WindowManagerPolicy getPolicyLocked() { |
| 2014 | while (mPolicy == null || !mDoneBooting) { |
| 2015 | try { |
| 2016 | mLocks.wait(); |
| 2017 | } catch (InterruptedException e) { |
| 2018 | // Ignore |
| 2019 | } |
| 2020 | } |
| 2021 | return mPolicy; |
| 2022 | } |
| 2023 | |
| 2024 | void systemReady() { |
| 2025 | synchronized (mLocks) { |
| 2026 | Log.d(TAG, "system ready!"); |
| 2027 | mDoneBooting = true; |
| Dianne Hackborn | 617f877 | 2009-03-31 15:04:46 -0700 | [diff] [blame] | 2028 | long identity = Binder.clearCallingIdentity(); |
| 2029 | try { |
| 2030 | mBatteryStats.noteScreenBrightness(getPreferredBrightness()); |
| 2031 | mBatteryStats.noteScreenOn(); |
| 2032 | } catch (RemoteException e) { |
| 2033 | // Nothing interesting to do. |
| 2034 | } finally { |
| 2035 | Binder.restoreCallingIdentity(identity); |
| 2036 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2037 | userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true); |
| 2038 | updateWakeLockLocked(); |
| 2039 | mLocks.notifyAll(); |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | public void monitor() { |
| 2044 | synchronized (mLocks) { } |
| 2045 | } |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2046 | |
| 2047 | public int getSupportedWakeLockFlags() { |
| 2048 | int result = PowerManager.PARTIAL_WAKE_LOCK |
| 2049 | | PowerManager.FULL_WAKE_LOCK |
| 2050 | | PowerManager.SCREEN_DIM_WAKE_LOCK; |
| 2051 | |
| 2052 | // call getSensorManager() to make sure mProximitySensor is initialized |
| 2053 | getSensorManager(); |
| 2054 | if (mProximitySensor != null) { |
| 2055 | result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK; |
| 2056 | } |
| 2057 | |
| 2058 | return result; |
| 2059 | } |
| 2060 | |
| 2061 | private SensorManager getSensorManager() { |
| 2062 | if (mSensorManager == null) { |
| 2063 | mSensorManager = new SensorManager(mHandlerThread.getLooper()); |
| 2064 | mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); |
| 2065 | } |
| 2066 | return mSensorManager; |
| 2067 | } |
| 2068 | |
| 2069 | private void enableProximityLockLocked() { |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2070 | if (mSpew) { |
| 2071 | Log.d(TAG, "enableProximityLockLocked"); |
| 2072 | } |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2073 | mSensorManager.registerListener(this, mProximitySensor, SensorManager.SENSOR_DELAY_NORMAL); |
| 2074 | } |
| 2075 | |
| 2076 | private void disableProximityLockLocked() { |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2077 | if (mSpew) { |
| 2078 | Log.d(TAG, "disableProximityLockLocked"); |
| 2079 | } |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2080 | mSensorManager.unregisterListener(this); |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2081 | mProximitySensorActive = false; |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | public void onSensorChanged(SensorEvent event) { |
| 2085 | long milliseconds = event.timestamp / 1000000; |
| Mike Lockwood | 36fc302 | 2009-08-25 16:49:06 -0700 | [diff] [blame] | 2086 | synchronized (mLocks) { |
| 2087 | if (event.values[0] == 0.0) { |
| 2088 | if (mSpew) { |
| 2089 | Log.d(TAG, "onSensorChanged: proximity active"); |
| 2090 | } |
| 2091 | goToSleepLocked(milliseconds); |
| 2092 | mProximitySensorActive = true; |
| 2093 | } else { |
| 2094 | // proximity sensor negative events user activity. |
| 2095 | // temporarily set mUserActivityAllowed to true so this will work |
| 2096 | // even when the keyguard is on. |
| 2097 | if (mSpew) { |
| 2098 | Log.d(TAG, "onSensorChanged: proximity inactive"); |
| 2099 | } |
| 2100 | mProximitySensorActive = false; |
| Mike Lockwood | 06952d9 | 2009-08-13 16:05:38 -0400 | [diff] [blame] | 2101 | boolean savedActivityAllowed = mUserActivityAllowed; |
| 2102 | mUserActivityAllowed = true; |
| 2103 | userActivity(milliseconds, false); |
| 2104 | mUserActivityAllowed = savedActivityAllowed; |
| 2105 | } |
| Mike Lockwood | bc706a0 | 2009-07-27 13:50:57 -0700 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 2110 | // ignore |
| 2111 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2112 | } |