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