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