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