blob: 023da4675908822d87b79e3b1dd52669655da608 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
19import com.android.internal.app.IBatteryStats;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080020import com.android.internal.app.ShutdownThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.server.am.BatteryStatsService;
22
23import android.app.ActivityManagerNative;
24import android.app.IActivityManager;
25import android.content.BroadcastReceiver;
26import android.content.ContentQueryMap;
27import android.content.ContentResolver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.pm.PackageManager;
Mike Lockwoodd7786b42009-10-15 17:09:16 -070032import android.content.res.Resources;
Doug Zongker43866e02010-01-07 12:09:54 -080033import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.database.Cursor;
Mike Lockwoodbc706a02009-07-27 13:50:57 -070035import android.hardware.Sensor;
36import android.hardware.SensorEvent;
37import android.hardware.SensorEventListener;
38import android.hardware.SensorManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.BatteryStats;
40import android.os.Binder;
Doug Zongker43866e02010-01-07 12:09:54 -080041import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Handler;
43import android.os.HandlerThread;
44import android.os.IBinder;
45import android.os.IPowerManager;
46import android.os.LocalPowerManager;
47import android.os.Power;
48import android.os.PowerManager;
49import android.os.Process;
50import android.os.RemoteException;
San Mehat14e69af2010-01-06 14:58:18 -080051import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.SystemClock;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070053import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.provider.Settings.SettingNotFoundException;
55import android.provider.Settings;
56import android.util.EventLog;
57import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080058import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.WindowManagerPolicy;
60import static android.provider.Settings.System.DIM_SCREEN;
61import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
Dan Murphy951764b2009-08-27 14:59:03 -050062import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
Mike Lockwooddc3494e2009-10-14 21:17:09 -070063import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
65import static android.provider.Settings.System.STAY_ON_WHILE_PLUGGED_IN;
66
67import java.io.FileDescriptor;
Doug Zongker50a21f42009-11-19 12:49:53 -080068import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.io.PrintWriter;
70import java.util.ArrayList;
71import java.util.HashMap;
72import java.util.Observable;
73import java.util.Observer;
74
Mike Lockwoodbc706a02009-07-27 13:50:57 -070075class PowerManagerService extends IPowerManager.Stub
Mike Lockwood8738e0c2009-10-04 08:44:47 -040076 implements LocalPowerManager, Watchdog.Monitor {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78 private static final String TAG = "PowerManagerService";
79 static final String PARTIAL_NAME = "PowerManagerService";
80
81 private static final boolean LOG_PARTIAL_WL = false;
82
83 // Indicates whether touch-down cycles should be logged as part of the
84 // LOG_POWER_SCREEN_STATE log events
85 private static final boolean LOG_TOUCH_DOWNS = true;
86
87 private static final int LOCK_MASK = PowerManager.PARTIAL_WAKE_LOCK
88 | PowerManager.SCREEN_DIM_WAKE_LOCK
89 | PowerManager.SCREEN_BRIGHT_WAKE_LOCK
Mike Lockwoodbc706a02009-07-27 13:50:57 -070090 | PowerManager.FULL_WAKE_LOCK
91 | PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 // time since last state: time since last event:
Doug Zongker43866e02010-01-07 12:09:54 -080094 // The short keylight delay comes from secure settings; this is the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 private static final int SHORT_KEYLIGHT_DELAY_DEFAULT = 6000; // t+6 sec
96 private static final int MEDIUM_KEYLIGHT_DELAY = 15000; // t+15 sec
97 private static final int LONG_KEYLIGHT_DELAY = 6000; // t+6 sec
98 private static final int LONG_DIM_TIME = 7000; // t+N-5 sec
99
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700100 // How long to wait to debounce light sensor changes.
Mike Lockwood9b8136922009-11-06 15:53:59 -0500101 private static final int LIGHT_SENSOR_DELAY = 2000;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700102
Mike Lockwood20f87d72009-11-05 16:08:51 -0500103 // For debouncing the proximity sensor.
104 private static final int PROXIMITY_SENSOR_DELAY = 1000;
105
Mike Lockwoodd20ea362009-09-15 00:13:38 -0400106 // trigger proximity if distance is less than 5 cm
107 private static final float PROXIMITY_THRESHOLD = 5.0f;
108
Doug Zongker43866e02010-01-07 12:09:54 -0800109 // Cached secure settings; see updateSettingsValues()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 private int mShortKeylightDelay = SHORT_KEYLIGHT_DELAY_DEFAULT;
111
112 // flags for setPowerState
113 private static final int SCREEN_ON_BIT = 0x00000001;
114 private static final int SCREEN_BRIGHT_BIT = 0x00000002;
115 private static final int BUTTON_BRIGHT_BIT = 0x00000004;
116 private static final int KEYBOARD_BRIGHT_BIT = 0x00000008;
117 private static final int BATTERY_LOW_BIT = 0x00000010;
118
119 // values for setPowerState
120
121 // SCREEN_OFF == everything off
122 private static final int SCREEN_OFF = 0x00000000;
123
124 // SCREEN_DIM == screen on, screen backlight dim
125 private static final int SCREEN_DIM = SCREEN_ON_BIT;
126
127 // SCREEN_BRIGHT == screen on, screen backlight bright
128 private static final int SCREEN_BRIGHT = SCREEN_ON_BIT | SCREEN_BRIGHT_BIT;
129
130 // SCREEN_BUTTON_BRIGHT == screen on, screen and button backlights bright
131 private static final int SCREEN_BUTTON_BRIGHT = SCREEN_BRIGHT | BUTTON_BRIGHT_BIT;
132
133 // SCREEN_BUTTON_BRIGHT == screen on, screen, button and keyboard backlights bright
134 private static final int ALL_BRIGHT = SCREEN_BUTTON_BRIGHT | KEYBOARD_BRIGHT_BIT;
135
136 // used for noChangeLights in setPowerState()
137 private static final int LIGHTS_MASK = SCREEN_BRIGHT_BIT | BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT;
138
139 static final boolean ANIMATE_SCREEN_LIGHTS = true;
140 static final boolean ANIMATE_BUTTON_LIGHTS = false;
141 static final boolean ANIMATE_KEYBOARD_LIGHTS = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final int ANIM_STEPS = 60/4;
Mike Lockwooddd9668e2009-10-27 15:47:02 -0400144 // Slower animation for autobrightness changes
145 static final int AUTOBRIGHTNESS_ANIM_STEPS = 60;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
147 // These magic numbers are the initial state of the LEDs at boot. Ideally
148 // we should read them from the driver, but our current hardware returns 0
149 // for the initial value. Oops!
150 static final int INITIAL_SCREEN_BRIGHTNESS = 255;
151 static final int INITIAL_BUTTON_BRIGHTNESS = Power.BRIGHTNESS_OFF;
152 static final int INITIAL_KEYBOARD_BRIGHTNESS = Power.BRIGHTNESS_OFF;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 private final int MY_UID;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700155 private final int MY_PID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
157 private boolean mDoneBooting = false;
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500158 private boolean mBootCompleted = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 private int mStayOnConditions = 0;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500160 private final int[] mBroadcastQueue = new int[] { -1, -1, -1 };
161 private final int[] mBroadcastWhy = new int[3];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private int mPartialCount = 0;
163 private int mPowerState;
Mike Lockwood435eb642009-12-03 08:40:18 -0500164 // mScreenOffReason can be WindowManagerPolicy.OFF_BECAUSE_OF_USER,
165 // WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT or WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR
166 private int mScreenOffReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 private int mUserState;
168 private boolean mKeyboardVisible = false;
169 private boolean mUserActivityAllowed = true;
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500170 private int mProximityWakeLockCount = 0;
171 private boolean mProximitySensorEnabled = false;
Mike Lockwood36fc3022009-08-25 16:49:06 -0700172 private boolean mProximitySensorActive = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -0500173 private int mProximityPendingValue = -1; // -1 == nothing, 0 == inactive, 1 == active
174 private long mLastProximityEventTime;
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800175 private int mScreenOffTimeoutSetting;
176 private int mMaximumScreenOffTimeout = Integer.MAX_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 private int mKeylightDelay;
178 private int mDimDelay;
179 private int mScreenOffDelay;
180 private int mWakeLockState;
181 private long mLastEventTime = 0;
182 private long mScreenOffTime;
183 private volatile WindowManagerPolicy mPolicy;
184 private final LockList mLocks = new LockList();
185 private Intent mScreenOffIntent;
186 private Intent mScreenOnIntent;
Mike Lockwood3a322132009-11-24 00:30:52 -0500187 private LightsService mLightsService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 private Context mContext;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500189 private LightsService.Light mLcdLight;
190 private LightsService.Light mButtonLight;
191 private LightsService.Light mKeyboardLight;
192 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 private UnsynchronizedWakeLock mBroadcastWakeLock;
194 private UnsynchronizedWakeLock mStayOnWhilePluggedInScreenDimLock;
195 private UnsynchronizedWakeLock mStayOnWhilePluggedInPartialLock;
196 private UnsynchronizedWakeLock mPreventScreenOnPartialLock;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500197 private UnsynchronizedWakeLock mProximityPartialLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 private HandlerThread mHandlerThread;
199 private Handler mHandler;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500200 private final TimeoutTask mTimeoutTask = new TimeoutTask();
201 private final LightAnimator mLightAnimator = new LightAnimator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 private final BrightnessState mScreenBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700203 = new BrightnessState(SCREEN_BRIGHT_BIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 private final BrightnessState mKeyboardBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700205 = new BrightnessState(KEYBOARD_BRIGHT_BIT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 private final BrightnessState mButtonBrightness
The Android Open Source Project10592532009-03-18 17:39:46 -0700207 = new BrightnessState(BUTTON_BRIGHT_BIT);
Joe Onorato128e7292009-03-24 18:41:31 -0700208 private boolean mStillNeedSleepNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 private boolean mIsPowered = false;
210 private IActivityManager mActivityService;
211 private IBatteryStats mBatteryStats;
212 private BatteryService mBatteryService;
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700213 private SensorManager mSensorManager;
214 private Sensor mProximitySensor;
Mike Lockwood8738e0c2009-10-04 08:44:47 -0400215 private Sensor mLightSensor;
216 private boolean mLightSensorEnabled;
217 private float mLightSensorValue = -1;
Mike Lockwoodb2865412010-02-02 22:40:33 -0500218 private int mHighestLightSensorValue = -1;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700219 private float mLightSensorPendingValue = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500220 private int mLightSensorScreenBrightness = -1;
221 private int mLightSensorButtonBrightness = -1;
222 private int mLightSensorKeyboardBrightness = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 private boolean mDimScreen = true;
Mike Lockwoodb2865412010-02-02 22:40:33 -0500224 private boolean mIsDocked = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 private long mNextTimeout;
226 private volatile int mPokey = 0;
227 private volatile boolean mPokeAwakeOnSet = false;
228 private volatile boolean mInitComplete = false;
Mike Lockwoodca44df82010-02-25 13:48:49 -0500229 private final HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>();
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500230 // mLastScreenOnTime is the time the screen was last turned on
231 private long mLastScreenOnTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 private boolean mPreventScreenOn;
233 private int mScreenBrightnessOverride = -1;
Mike Lockwoodfb73f792009-11-20 11:31:18 -0500234 private int mButtonBrightnessOverride = -1;
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400235 private boolean mUseSoftwareAutoBrightness;
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700236 private boolean mAutoBrightessEnabled;
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700237 private int[] mAutoBrightnessLevels;
238 private int[] mLcdBacklightValues;
239 private int[] mButtonBacklightValues;
240 private int[] mKeyboardBacklightValues;
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500241 private int mLightSensorWarmupTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
243 // Used when logging number and duration of touch-down cycles
244 private long mTotalTouchDownTime;
245 private long mLastTouchDown;
246 private int mTouchCycles;
247
248 // could be either static or controllable at runtime
249 private static final boolean mSpew = false;
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500250 private static final boolean mDebugProximitySensor = (true || mSpew);
Mike Lockwooddd9668e2009-10-27 15:47:02 -0400251 private static final boolean mDebugLightSensor = (false || mSpew);
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700252
253 private native void nativeInit();
254 private native void nativeSetPowerState(boolean screenOn, boolean screenBright);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
256 /*
257 static PrintStream mLog;
258 static {
259 try {
260 mLog = new PrintStream("/data/power.log");
261 }
262 catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800263 android.util.Slog.e(TAG, "Life is hard", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265 }
266 static class Log {
267 static void d(String tag, String s) {
268 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800269 android.util.Slog.d(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271 static void i(String tag, String s) {
272 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800273 android.util.Slog.i(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275 static void w(String tag, String s) {
276 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800277 android.util.Slog.w(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 }
279 static void e(String tag, String s) {
280 mLog.println(s);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800281 android.util.Slog.e(tag, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283 }
284 */
285
286 /**
287 * This class works around a deadlock between the lock in PowerManager.WakeLock
288 * and our synchronizing on mLocks. PowerManager.WakeLock synchronizes on its
289 * mToken object so it can be accessed from any thread, but it calls into here
290 * with its lock held. This class is essentially a reimplementation of
291 * PowerManager.WakeLock, but without that extra synchronized block, because we'll
292 * only call it with our own locks held.
293 */
294 private class UnsynchronizedWakeLock {
295 int mFlags;
296 String mTag;
297 IBinder mToken;
298 int mCount = 0;
299 boolean mRefCounted;
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500300 boolean mHeld;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
302 UnsynchronizedWakeLock(int flags, String tag, boolean refCounted) {
303 mFlags = flags;
304 mTag = tag;
305 mToken = new Binder();
306 mRefCounted = refCounted;
307 }
308
309 public void acquire() {
310 if (!mRefCounted || mCount++ == 0) {
311 long ident = Binder.clearCallingIdentity();
312 try {
313 PowerManagerService.this.acquireWakeLockLocked(mFlags, mToken,
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700314 MY_UID, MY_PID, mTag, null);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500315 mHeld = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 } finally {
317 Binder.restoreCallingIdentity(ident);
318 }
319 }
320 }
321
322 public void release() {
323 if (!mRefCounted || --mCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500324 PowerManagerService.this.releaseWakeLockLocked(mToken, 0, false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500325 mHeld = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327 if (mCount < 0) {
328 throw new RuntimeException("WakeLock under-locked " + mTag);
329 }
330 }
331
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500332 public boolean isHeld()
333 {
334 return mHeld;
335 }
336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 public String toString() {
338 return "UnsynchronizedWakeLock(mFlags=0x" + Integer.toHexString(mFlags)
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500339 + " mCount=" + mCount + " mHeld=" + mHeld + ")";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
341 }
342
343 private final class BatteryReceiver extends BroadcastReceiver {
344 @Override
345 public void onReceive(Context context, Intent intent) {
346 synchronized (mLocks) {
347 boolean wasPowered = mIsPowered;
348 mIsPowered = mBatteryService.isPowered();
349
350 if (mIsPowered != wasPowered) {
351 // update mStayOnWhilePluggedIn wake lock
352 updateWakeLockLocked();
353
354 // treat plugging and unplugging the devices as a user activity.
355 // users find it disconcerting when they unplug the device
356 // and it shuts off right away.
Mike Lockwood84a89342010-03-01 21:28:58 -0500357 // to avoid turning on the screen when unplugging, we only trigger
358 // user activity when screen was already on.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 // temporarily set mUserActivityAllowed to true so this will work
360 // even when the keyguard is on.
361 synchronized (mLocks) {
Mike Lockwood84a89342010-03-01 21:28:58 -0500362 if (!wasPowered || (mPowerState & SCREEN_ON_BIT) != 0) {
363 forceUserActivityLocked();
364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
366 }
367 }
368 }
369 }
370
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500371 private final class BootCompletedReceiver extends BroadcastReceiver {
372 @Override
373 public void onReceive(Context context, Intent intent) {
374 bootCompleted();
375 }
376 }
377
Mike Lockwoodb2865412010-02-02 22:40:33 -0500378 private final class DockReceiver extends BroadcastReceiver {
379 @Override
380 public void onReceive(Context context, Intent intent) {
381 int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
382 Intent.EXTRA_DOCK_STATE_UNDOCKED);
383 dockStateChanged(state);
384 }
385 }
386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 /**
388 * Set the setting that determines whether the device stays on when plugged in.
389 * The argument is a bit string, with each bit specifying a power source that,
390 * when the device is connected to that source, causes the device to stay on.
391 * See {@link android.os.BatteryManager} for the list of power sources that
392 * can be specified. Current values include {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
393 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
394 * @param val an {@code int} containing the bits that specify which power sources
395 * should cause the device to stay on.
396 */
397 public void setStayOnSetting(int val) {
398 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WRITE_SETTINGS, null);
399 Settings.System.putInt(mContext.getContentResolver(),
400 Settings.System.STAY_ON_WHILE_PLUGGED_IN, val);
401 }
402
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800403 public void setMaximumScreenOffTimeount(int timeMs) {
404 mContext.enforceCallingOrSelfPermission(
405 android.Manifest.permission.WRITE_SECURE_SETTINGS, null);
406 synchronized (mLocks) {
407 mMaximumScreenOffTimeout = timeMs;
408 // recalculate everything
409 setScreenOffTimeoutsLocked();
410 }
411 }
412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 private class SettingsObserver implements Observer {
414 private int getInt(String name) {
415 return mSettings.getValues(name).getAsInteger(Settings.System.VALUE);
416 }
417
418 public void update(Observable o, Object arg) {
419 synchronized (mLocks) {
420 // STAY_ON_WHILE_PLUGGED_IN
421 mStayOnConditions = getInt(STAY_ON_WHILE_PLUGGED_IN);
422 updateWakeLockLocked();
423
424 // SCREEN_OFF_TIMEOUT
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800425 mScreenOffTimeoutSetting = getInt(SCREEN_OFF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
427 // DIM_SCREEN
428 //mDimScreen = getInt(DIM_SCREEN) != 0;
429
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700430 // SCREEN_BRIGHTNESS_MODE
431 setScreenBrightnessMode(getInt(SCREEN_BRIGHTNESS_MODE));
432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 // recalculate everything
434 setScreenOffTimeoutsLocked();
435 }
436 }
437 }
438
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700439 PowerManagerService() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 // Hack to get our uid... should have a func for this.
441 long token = Binder.clearCallingIdentity();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700442 MY_UID = Process.myUid();
443 MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 Binder.restoreCallingIdentity(token);
445
446 // XXX remove this when the kernel doesn't timeout wake locks
447 Power.setLastUserActivityTimeout(7*24*3600*1000); // one week
448
449 // assume nothing is on yet
450 mUserState = mPowerState = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 // Add ourself to the Watchdog monitors.
453 Watchdog.getInstance().addMonitor(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455
456 private ContentQueryMap mSettings;
457
Mike Lockwood3a322132009-11-24 00:30:52 -0500458 void init(Context context, LightsService lights, IActivityManager activity,
The Android Open Source Project10592532009-03-18 17:39:46 -0700459 BatteryService battery) {
Mike Lockwood3a322132009-11-24 00:30:52 -0500460 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 mContext = context;
462 mActivityService = activity;
463 mBatteryStats = BatteryStatsService.getService();
464 mBatteryService = battery;
465
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500466 mLcdLight = lights.getLight(LightsService.LIGHT_ID_BACKLIGHT);
467 mButtonLight = lights.getLight(LightsService.LIGHT_ID_BUTTONS);
468 mKeyboardLight = lights.getLight(LightsService.LIGHT_ID_KEYBOARD);
469 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 mHandlerThread = new HandlerThread("PowerManagerService") {
472 @Override
473 protected void onLooperPrepared() {
474 super.onLooperPrepared();
475 initInThread();
476 }
477 };
478 mHandlerThread.start();
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 synchronized (mHandlerThread) {
481 while (!mInitComplete) {
482 try {
483 mHandlerThread.wait();
484 } catch (InterruptedException e) {
485 // Ignore
486 }
487 }
488 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700489
490 nativeInit();
491 synchronized (mLocks) {
492 updateNativePowerStateLocked();
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 void initInThread() {
497 mHandler = new Handler();
498
499 mBroadcastWakeLock = new UnsynchronizedWakeLock(
Joe Onorato128e7292009-03-24 18:41:31 -0700500 PowerManager.PARTIAL_WAKE_LOCK, "sleep_broadcast", true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 mStayOnWhilePluggedInScreenDimLock = new UnsynchronizedWakeLock(
502 PowerManager.SCREEN_DIM_WAKE_LOCK, "StayOnWhilePluggedIn Screen Dim", false);
503 mStayOnWhilePluggedInPartialLock = new UnsynchronizedWakeLock(
504 PowerManager.PARTIAL_WAKE_LOCK, "StayOnWhilePluggedIn Partial", false);
505 mPreventScreenOnPartialLock = new UnsynchronizedWakeLock(
506 PowerManager.PARTIAL_WAKE_LOCK, "PreventScreenOn Partial", false);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -0500507 mProximityPartialLock = new UnsynchronizedWakeLock(
508 PowerManager.PARTIAL_WAKE_LOCK, "Proximity Partial", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509
510 mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
511 mScreenOnIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
512 mScreenOffIntent = new Intent(Intent.ACTION_SCREEN_OFF);
513 mScreenOffIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
514
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700515 Resources resources = mContext.getResources();
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400516
517 // read settings for auto-brightness
518 mUseSoftwareAutoBrightness = resources.getBoolean(
519 com.android.internal.R.bool.config_automatic_brightness_available);
Mike Lockwoodaa66ea82009-10-31 16:31:27 -0400520 if (mUseSoftwareAutoBrightness) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700521 mAutoBrightnessLevels = resources.getIntArray(
522 com.android.internal.R.array.config_autoBrightnessLevels);
523 mLcdBacklightValues = resources.getIntArray(
524 com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
525 mButtonBacklightValues = resources.getIntArray(
526 com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
527 mKeyboardBacklightValues = resources.getIntArray(
528 com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
Mike Lockwood20ee6f22009-11-07 20:33:47 -0500529 mLightSensorWarmupTime = resources.getInteger(
530 com.android.internal.R.integer.config_lightSensorWarmupTime);
Mike Lockwoodd7786b42009-10-15 17:09:16 -0700531 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700532
533 ContentResolver resolver = mContext.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 Cursor settingsCursor = resolver.query(Settings.System.CONTENT_URI, null,
535 "(" + Settings.System.NAME + "=?) or ("
536 + Settings.System.NAME + "=?) or ("
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700537 + Settings.System.NAME + "=?) or ("
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 + Settings.System.NAME + "=?)",
Mike Lockwooddc3494e2009-10-14 21:17:09 -0700539 new String[]{STAY_ON_WHILE_PLUGGED_IN, SCREEN_OFF_TIMEOUT, DIM_SCREEN,
540 SCREEN_BRIGHTNESS_MODE},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 null);
542 mSettings = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, mHandler);
543 SettingsObserver settingsObserver = new SettingsObserver();
544 mSettings.addObserver(settingsObserver);
545
546 // pretend that the settings changed so we will get their initial state
547 settingsObserver.update(mSettings, null);
548
549 // register for the battery changed notifications
550 IntentFilter filter = new IntentFilter();
551 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
552 mContext.registerReceiver(new BatteryReceiver(), filter);
Mike Lockwood2d7bb812009-11-15 18:12:22 -0500553 filter = new IntentFilter();
554 filter.addAction(Intent.ACTION_BOOT_COMPLETED);
555 mContext.registerReceiver(new BootCompletedReceiver(), filter);
Mike Lockwoodb2865412010-02-02 22:40:33 -0500556 filter = new IntentFilter();
557 filter.addAction(Intent.ACTION_DOCK_EVENT);
558 mContext.registerReceiver(new DockReceiver(), filter);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559
Doug Zongker43866e02010-01-07 12:09:54 -0800560 // Listen for secure settings changes
561 mContext.getContentResolver().registerContentObserver(
562 Settings.Secure.CONTENT_URI, true,
563 new ContentObserver(new Handler()) {
564 public void onChange(boolean selfChange) {
565 updateSettingsValues();
566 }
567 });
568 updateSettingsValues();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 synchronized (mHandlerThread) {
571 mInitComplete = true;
572 mHandlerThread.notifyAll();
573 }
574 }
575
576 private class WakeLock implements IBinder.DeathRecipient
577 {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700578 WakeLock(int f, IBinder b, String t, int u, int p) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 super();
580 flags = f;
581 binder = b;
582 tag = t;
583 uid = u == MY_UID ? Process.SYSTEM_UID : u;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700584 pid = p;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 if (u != MY_UID || (
586 !"KEEP_SCREEN_ON_FLAG".equals(tag)
587 && !"KeyInputQueue".equals(tag))) {
588 monitorType = (f & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK
589 ? BatteryStats.WAKE_TYPE_PARTIAL
590 : BatteryStats.WAKE_TYPE_FULL;
591 } else {
592 monitorType = -1;
593 }
594 try {
595 b.linkToDeath(this, 0);
596 } catch (RemoteException e) {
597 binderDied();
598 }
599 }
600 public void binderDied() {
601 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500602 releaseWakeLockLocked(this.binder, 0, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604 }
605 final int flags;
606 final IBinder binder;
607 final String tag;
608 final int uid;
Mike Lockwoodf5bd0922010-03-22 17:10:15 -0400609 final int pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 final int monitorType;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700611 WorkSource ws;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 boolean activated = true;
613 int minState;
614 }
615
616 private void updateWakeLockLocked() {
617 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
618 // keep the device on if we're plugged in and mStayOnWhilePluggedIn is set.
619 mStayOnWhilePluggedInScreenDimLock.acquire();
620 mStayOnWhilePluggedInPartialLock.acquire();
621 } else {
622 mStayOnWhilePluggedInScreenDimLock.release();
623 mStayOnWhilePluggedInPartialLock.release();
624 }
625 }
626
627 private boolean isScreenLock(int flags)
628 {
629 int n = flags & LOCK_MASK;
630 return n == PowerManager.FULL_WAKE_LOCK
631 || n == PowerManager.SCREEN_BRIGHT_WAKE_LOCK
632 || n == PowerManager.SCREEN_DIM_WAKE_LOCK;
633 }
634
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700635 void enforceWakeSourcePermission(int uid, int pid) {
636 if (uid == Process.myUid()) {
637 return;
638 }
639 mContext.enforcePermission(android.Manifest.permission.UPDATE_DEVICE_STATS,
640 pid, uid, null);
641 }
642
643 public void acquireWakeLock(int flags, IBinder lock, String tag, WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 int uid = Binder.getCallingUid();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700645 int pid = Binder.getCallingPid();
Michael Chane96440f2009-05-06 10:27:36 -0700646 if (uid != Process.myUid()) {
647 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
648 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700649 if (ws != null) {
650 enforceWakeSourcePermission(uid, pid);
651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 long ident = Binder.clearCallingIdentity();
653 try {
654 synchronized (mLocks) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700655 acquireWakeLockLocked(flags, lock, uid, pid, tag, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
657 } finally {
658 Binder.restoreCallingIdentity(ident);
659 }
660 }
661
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700662 void noteStartWakeLocked(WakeLock wl, WorkSource ws) {
Dianne Hackborn70be1672010-09-14 11:13:03 -0700663 if (wl.monitorType >= 0) {
664 long origId = Binder.clearCallingIdentity();
665 try {
666 if (ws != null) {
667 mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag,
668 wl.monitorType);
669 } else {
670 mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
671 }
672 } catch (RemoteException e) {
673 // Ignore
674 } finally {
675 Binder.restoreCallingIdentity(origId);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700676 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700677 }
678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700680 void noteStopWakeLocked(WakeLock wl, WorkSource ws) {
Dianne Hackborn70be1672010-09-14 11:13:03 -0700681 if (wl.monitorType >= 0) {
682 long origId = Binder.clearCallingIdentity();
683 try {
684 if (ws != null) {
685 mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag,
686 wl.monitorType);
687 } else {
688 mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
689 }
690 } catch (RemoteException e) {
691 // Ignore
692 } finally {
693 Binder.restoreCallingIdentity(origId);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700694 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700695 }
696 }
697
698 public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag,
699 WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800701 Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700704 if (ws != null && ws.size() == 0) {
705 ws = null;
706 }
707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 int index = mLocks.getIndex(lock);
709 WakeLock wl;
710 boolean newlock;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700711 boolean diffsource;
712 WorkSource oldsource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 if (index < 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700714 wl = new WakeLock(flags, lock, tag, uid, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 switch (wl.flags & LOCK_MASK)
716 {
717 case PowerManager.FULL_WAKE_LOCK:
Mike Lockwood4984e732009-11-01 08:16:33 -0500718 if (mUseSoftwareAutoBrightness) {
Mike Lockwood3333fa42009-10-26 14:50:42 -0400719 wl.minState = SCREEN_BRIGHT;
720 } else {
721 wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 break;
724 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
725 wl.minState = SCREEN_BRIGHT;
726 break;
727 case PowerManager.SCREEN_DIM_WAKE_LOCK:
728 wl.minState = SCREEN_DIM;
729 break;
730 case PowerManager.PARTIAL_WAKE_LOCK:
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700731 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 break;
733 default:
734 // just log and bail. we're in the server, so don't
735 // throw an exception.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800736 Slog.e(TAG, "bad wakelock type for lock '" + tag + "' "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 + " flags=" + flags);
738 return;
739 }
740 mLocks.addLock(wl);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700741 if (ws != null) {
742 wl.ws = new WorkSource(ws);
743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 newlock = true;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700745 diffsource = false;
746 oldsource = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 } else {
748 wl = mLocks.get(index);
749 newlock = false;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700750 oldsource = wl.ws;
751 if (oldsource != null) {
752 if (ws == null) {
753 wl.ws = null;
754 diffsource = true;
755 } else {
756 diffsource = oldsource.diff(ws);
757 }
758 } else if (ws != null) {
759 diffsource = true;
760 } else {
761 diffsource = false;
762 }
763 if (diffsource) {
764 wl.ws = new WorkSource(ws);
765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767 if (isScreenLock(flags)) {
768 // if this causes a wakeup, we reactivate all of the locks and
769 // set it to whatever they want. otherwise, we modulate that
770 // by the current state so we never turn it more on than
771 // it already is.
772 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
Michael Chane96440f2009-05-06 10:27:36 -0700773 int oldWakeLockState = mWakeLockState;
774 mWakeLockState = mLocks.reactivateScreenLocksLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800776 Slog.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
Michael Chane96440f2009-05-06 10:27:36 -0700777 + " mWakeLockState=0x"
778 + Integer.toHexString(mWakeLockState)
779 + " previous wakeLockState=0x" + Integer.toHexString(oldWakeLockState));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 } else {
782 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800783 Slog.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 + " mLocks.gatherState()=0x"
785 + Integer.toHexString(mLocks.gatherState())
786 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
787 }
788 mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState();
789 }
790 setPowerState(mWakeLockState | mUserState);
791 }
792 else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
793 if (newlock) {
794 mPartialCount++;
795 if (mPartialCount == 1) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800796 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798 }
799 Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700800 } else if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500801 mProximityWakeLockCount++;
802 if (mProximityWakeLockCount == 1) {
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700803 enableProximityLockLocked();
804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700807 if (diffsource) {
808 // If the lock sources have changed, need to first release the
809 // old ones.
810 noteStopWakeLocked(wl, oldsource);
811 }
812 if (newlock || diffsource) {
813 noteStartWakeLocked(wl, ws);
814 }
815 }
816
817 public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) {
818 int uid = Binder.getCallingUid();
819 int pid = Binder.getCallingPid();
820 if (ws != null && ws.size() == 0) {
821 ws = null;
822 }
823 if (ws != null) {
824 enforceWakeSourcePermission(uid, pid);
825 }
Dianne Hackborn70be1672010-09-14 11:13:03 -0700826 synchronized (mLocks) {
827 int index = mLocks.getIndex(lock);
828 if (index < 0) {
829 throw new IllegalArgumentException("Wake lock not active");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
Dianne Hackborn70be1672010-09-14 11:13:03 -0700831 WakeLock wl = mLocks.get(index);
832 WorkSource oldsource = wl.ws;
833 wl.ws = ws != null ? new WorkSource(ws) : null;
834 noteStopWakeLocked(wl, oldsource);
835 noteStartWakeLocked(wl, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837 }
838
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500839 public void releaseWakeLock(IBinder lock, int flags) {
Michael Chane96440f2009-05-06 10:27:36 -0700840 int uid = Binder.getCallingUid();
841 if (uid != Process.myUid()) {
842 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844
845 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500846 releaseWakeLockLocked(lock, flags, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 }
848 }
849
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500850 private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 WakeLock wl = mLocks.removeLock(lock);
852 if (wl == null) {
853 return;
854 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800857 Slog.d(TAG, "releaseWakeLock flags=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 + Integer.toHexString(wl.flags) + " tag=" + wl.tag);
859 }
860
861 if (isScreenLock(wl.flags)) {
862 mWakeLockState = mLocks.gatherState();
863 // goes in the middle to reduce flicker
864 if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) {
865 userActivity(SystemClock.uptimeMillis(), false);
866 }
867 setPowerState(mWakeLockState | mUserState);
868 }
869 else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
870 mPartialCount--;
871 if (mPartialCount == 0) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800872 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 Power.releaseWakeLock(PARTIAL_NAME);
874 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700875 } else if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500876 mProximityWakeLockCount--;
877 if (mProximityWakeLockCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500878 if (mProximitySensorActive &&
879 ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500880 // wait for proximity sensor to go negative before disabling sensor
881 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800882 Slog.d(TAG, "waiting for proximity sensor to go negative");
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500883 }
884 } else {
885 disableProximityLockLocked();
886 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889 // Unlink the lock from the binder.
890 wl.binder.unlinkToDeath(wl, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891
Dianne Hackborn70be1672010-09-14 11:13:03 -0700892 noteStopWakeLocked(wl, wl.ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 private class PokeLock implements IBinder.DeathRecipient
896 {
897 PokeLock(int p, IBinder b, String t) {
898 super();
899 this.pokey = p;
900 this.binder = b;
901 this.tag = t;
902 try {
903 b.linkToDeath(this, 0);
904 } catch (RemoteException e) {
905 binderDied();
906 }
907 }
908 public void binderDied() {
909 setPokeLock(0, this.binder, this.tag);
910 }
911 int pokey;
912 IBinder binder;
913 String tag;
914 boolean awakeOnSet;
915 }
916
917 public void setPokeLock(int pokey, IBinder token, String tag) {
918 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
919 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800920 Slog.e(TAG, "setPokeLock got null token for tag='" + tag + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 return;
922 }
923
924 if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) {
925 throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT"
926 + " and POKE_LOCK_MEDIUM_TIMEOUT");
927 }
928
929 synchronized (mLocks) {
930 if (pokey != 0) {
931 PokeLock p = mPokeLocks.get(token);
932 int oldPokey = 0;
933 if (p != null) {
934 oldPokey = p.pokey;
935 p.pokey = pokey;
936 } else {
937 p = new PokeLock(pokey, token, tag);
938 mPokeLocks.put(token, p);
939 }
940 int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
941 int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
942 if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) {
943 p.awakeOnSet = true;
944 }
945 } else {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700946 PokeLock rLock = mPokeLocks.remove(token);
947 if (rLock != null) {
948 token.unlinkToDeath(rLock, 0);
949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951
952 int oldPokey = mPokey;
953 int cumulative = 0;
954 boolean oldAwakeOnSet = mPokeAwakeOnSet;
955 boolean awakeOnSet = false;
956 for (PokeLock p: mPokeLocks.values()) {
957 cumulative |= p.pokey;
958 if (p.awakeOnSet) {
959 awakeOnSet = true;
960 }
961 }
962 mPokey = cumulative;
963 mPokeAwakeOnSet = awakeOnSet;
964
965 int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
966 int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 if (oldCumulativeTimeout != newCumulativeTimeout) {
969 setScreenOffTimeoutsLocked();
970 // reset the countdown timer, but use the existing nextState so it doesn't
971 // change anything
972 setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState);
973 }
974 }
975 }
976
977 private static String lockType(int type)
978 {
979 switch (type)
980 {
981 case PowerManager.FULL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700982 return "FULL_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700984 return "SCREEN_BRIGHT_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 case PowerManager.SCREEN_DIM_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700986 return "SCREEN_DIM_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 case PowerManager.PARTIAL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700988 return "PARTIAL_WAKE_LOCK ";
989 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
990 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 default:
David Brown251faa62009-08-02 22:04:36 -0700992 return "??? ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994 }
995
996 private static String dumpPowerState(int state) {
997 return (((state & KEYBOARD_BRIGHT_BIT) != 0)
998 ? "KEYBOARD_BRIGHT_BIT " : "")
999 + (((state & SCREEN_BRIGHT_BIT) != 0)
1000 ? "SCREEN_BRIGHT_BIT " : "")
1001 + (((state & SCREEN_ON_BIT) != 0)
1002 ? "SCREEN_ON_BIT " : "")
1003 + (((state & BATTERY_LOW_BIT) != 0)
1004 ? "BATTERY_LOW_BIT " : "");
1005 }
1006
1007 @Override
1008 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1009 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1010 != PackageManager.PERMISSION_GRANTED) {
1011 pw.println("Permission Denial: can't dump PowerManager from from pid="
1012 + Binder.getCallingPid()
1013 + ", uid=" + Binder.getCallingUid());
1014 return;
1015 }
1016
1017 long now = SystemClock.uptimeMillis();
1018
Mike Lockwoodca44df82010-02-25 13:48:49 -05001019 synchronized (mLocks) {
1020 pw.println("Power Manager State:");
1021 pw.println(" mIsPowered=" + mIsPowered
1022 + " mPowerState=" + mPowerState
1023 + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime)
1024 + " ms");
1025 pw.println(" mPartialCount=" + mPartialCount);
1026 pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState));
1027 pw.println(" mUserState=" + dumpPowerState(mUserState));
1028 pw.println(" mPowerState=" + dumpPowerState(mPowerState));
1029 pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState()));
1030 pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now
1031 + " " + ((mNextTimeout-now)/1000) + "s from now");
1032 pw.println(" mDimScreen=" + mDimScreen
1033 + " mStayOnConditions=" + mStayOnConditions);
1034 pw.println(" mScreenOffReason=" + mScreenOffReason
1035 + " mUserState=" + mUserState);
1036 pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1]
1037 + ',' + mBroadcastQueue[2] + "}");
1038 pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1]
1039 + ',' + mBroadcastWhy[2] + "}");
1040 pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
1041 pw.println(" mKeyboardVisible=" + mKeyboardVisible
1042 + " mUserActivityAllowed=" + mUserActivityAllowed);
1043 pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay
1044 + " mScreenOffDelay=" + mScreenOffDelay);
1045 pw.println(" mPreventScreenOn=" + mPreventScreenOn
1046 + " mScreenBrightnessOverride=" + mScreenBrightnessOverride
1047 + " mButtonBrightnessOverride=" + mButtonBrightnessOverride);
1048 pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting
1049 + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout);
1050 pw.println(" mLastScreenOnTime=" + mLastScreenOnTime);
1051 pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock);
1052 pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
1053 pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
1054 pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock);
1055 pw.println(" mProximityPartialLock=" + mProximityPartialLock);
1056 pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount);
1057 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1058 pw.println(" mProximitySensorActive=" + mProximitySensorActive);
1059 pw.println(" mProximityPendingValue=" + mProximityPendingValue);
1060 pw.println(" mLastProximityEventTime=" + mLastProximityEventTime);
1061 pw.println(" mLightSensorEnabled=" + mLightSensorEnabled);
1062 pw.println(" mLightSensorValue=" + mLightSensorValue
1063 + " mLightSensorPendingValue=" + mLightSensorPendingValue);
1064 pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
1065 + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
1066 + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
1067 pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness);
1068 pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled);
1069 mScreenBrightness.dump(pw, " mScreenBrightness: ");
1070 mKeyboardBrightness.dump(pw, " mKeyboardBrightness: ");
1071 mButtonBrightness.dump(pw, " mButtonBrightness: ");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001072
Mike Lockwoodca44df82010-02-25 13:48:49 -05001073 int N = mLocks.size();
1074 pw.println();
1075 pw.println("mLocks.size=" + N + ":");
1076 for (int i=0; i<N; i++) {
1077 WakeLock wl = mLocks.get(i);
1078 String type = lockType(wl.flags & LOCK_MASK);
1079 String acquireCausesWakeup = "";
1080 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
1081 acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP ";
1082 }
1083 String activated = "";
1084 if (wl.activated) {
1085 activated = " activated";
1086 }
1087 pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04001088 + activated + " (minState=" + wl.minState + ", uid=" + wl.uid
1089 + ", pid=" + wl.pid + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 }
Mike Lockwoodca44df82010-02-25 13:48:49 -05001091
1092 pw.println();
1093 pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":");
1094 for (PokeLock p: mPokeLocks.values()) {
1095 pw.println(" poke lock '" + p.tag + "':"
1096 + ((p.pokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0
1097 ? " POKE_LOCK_IGNORE_CHEEK_EVENTS" : "")
1098 + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0
1099 ? " POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS" : "")
1100 + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
1101 ? " POKE_LOCK_SHORT_TIMEOUT" : "")
1102 + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
1103 ? " POKE_LOCK_MEDIUM_TIMEOUT" : ""));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001105
Mike Lockwoodca44df82010-02-25 13:48:49 -05001106 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
1109
Joe Onorato7999bff2010-07-24 11:50:05 -04001110 private void setTimeoutLocked(long now, int nextState) {
1111 setTimeoutLocked(now, -1, nextState);
1112 }
1113
1114 // If they gave a timeoutOverride it is the number of seconds
1115 // to screen-off. Figure out where in the countdown cycle we
1116 // should jump to.
Joe Onorato797e6882010-08-26 14:46:01 -04001117 private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) {
1118 long timeoutOverride = originalTimeoutOverride;
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001119 if (mBootCompleted) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001120 synchronized (mLocks) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001121 long when = 0;
1122 if (timeoutOverride <= 0) {
1123 switch (nextState)
1124 {
1125 case SCREEN_BRIGHT:
1126 when = now + mKeylightDelay;
1127 break;
1128 case SCREEN_DIM:
1129 if (mDimDelay >= 0) {
1130 when = now + mDimDelay;
Andreas Huber84047bc2010-07-27 16:49:10 -07001131 break;
Joe Onorato7999bff2010-07-24 11:50:05 -04001132 } else {
1133 Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1134 }
1135 case SCREEN_OFF:
1136 synchronized (mLocks) {
1137 when = now + mScreenOffDelay;
1138 }
1139 break;
Andreas Huber84047bc2010-07-27 16:49:10 -07001140 default:
1141 when = now;
1142 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001144 } else {
1145 override: {
1146 if (timeoutOverride <= mScreenOffDelay) {
1147 when = now + timeoutOverride;
1148 nextState = SCREEN_OFF;
1149 break override;
1150 }
1151 timeoutOverride -= mScreenOffDelay;
1152
1153 if (mDimDelay >= 0) {
1154 if (timeoutOverride <= mDimDelay) {
1155 when = now + timeoutOverride;
1156 nextState = SCREEN_DIM;
1157 break override;
1158 }
1159 timeoutOverride -= mDimDelay;
1160 }
1161
1162 when = now + timeoutOverride;
1163 nextState = SCREEN_BRIGHT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001165 }
1166 if (mSpew) {
1167 Slog.d(TAG, "setTimeoutLocked now=" + now
1168 + " timeoutOverride=" + timeoutOverride
1169 + " nextState=" + nextState + " when=" + when);
1170 }
Joe Onorato797e6882010-08-26 14:46:01 -04001171
1172 mHandler.removeCallbacks(mTimeoutTask);
1173 mTimeoutTask.nextState = nextState;
1174 mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0
1175 ? (originalTimeoutOverride - timeoutOverride)
1176 : -1;
Joe Onorato7999bff2010-07-24 11:50:05 -04001177 mHandler.postAtTime(mTimeoutTask, when);
1178 mNextTimeout = when; // for debugging
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181 }
1182
1183 private void cancelTimerLocked()
1184 {
1185 mHandler.removeCallbacks(mTimeoutTask);
1186 mTimeoutTask.nextState = -1;
1187 }
1188
1189 private class TimeoutTask implements Runnable
1190 {
1191 int nextState; // access should be synchronized on mLocks
Joe Onorato797e6882010-08-26 14:46:01 -04001192 long remainingTimeoutOverride;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 public void run()
1194 {
1195 synchronized (mLocks) {
1196 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001197 Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
1199
1200 if (nextState == -1) {
1201 return;
1202 }
1203
1204 mUserState = this.nextState;
1205 setPowerState(this.nextState | mWakeLockState);
1206
1207 long now = SystemClock.uptimeMillis();
1208
1209 switch (this.nextState)
1210 {
1211 case SCREEN_BRIGHT:
1212 if (mDimDelay >= 0) {
Joe Onorato797e6882010-08-26 14:46:01 -04001213 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 break;
1215 }
1216 case SCREEN_DIM:
Joe Onorato797e6882010-08-26 14:46:01 -04001217 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 break;
1219 }
1220 }
1221 }
1222 }
1223
1224 private void sendNotificationLocked(boolean on, int why)
1225 {
Joe Onorato64c62ba2009-03-24 20:13:57 -07001226 if (!on) {
1227 mStillNeedSleepNotification = false;
1228 }
1229
Joe Onorato128e7292009-03-24 18:41:31 -07001230 // Add to the queue.
1231 int index = 0;
1232 while (mBroadcastQueue[index] != -1) {
1233 index++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 }
Joe Onorato128e7292009-03-24 18:41:31 -07001235 mBroadcastQueue[index] = on ? 1 : 0;
1236 mBroadcastWhy[index] = why;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237
Joe Onorato128e7292009-03-24 18:41:31 -07001238 // If we added it position 2, then there is a pair that can be stripped.
1239 // If we added it position 1 and we're turning the screen off, we can strip
1240 // the pair and do nothing, because the screen is already off, and therefore
1241 // keyguard has already been enabled.
1242 // However, if we added it at position 1 and we're turning it on, then position
1243 // 0 was to turn it off, and we can't strip that, because keyguard needs to come
1244 // on, so have to run the queue then.
1245 if (index == 2) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08001246 // While we're collapsing them, if it's going off, and the new reason
1247 // is more significant than the first, then use the new one.
1248 if (!on && mBroadcastWhy[0] > why) {
1249 mBroadcastWhy[0] = why;
Joe Onorato128e7292009-03-24 18:41:31 -07001250 }
1251 mBroadcastQueue[0] = on ? 1 : 0;
1252 mBroadcastQueue[1] = -1;
1253 mBroadcastQueue[2] = -1;
Mike Lockwood9c90a372010-04-13 15:40:27 -04001254 mBroadcastWakeLock.release();
1255 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001256 index = 0;
1257 }
1258 if (index == 1 && !on) {
1259 mBroadcastQueue[0] = -1;
1260 mBroadcastQueue[1] = -1;
1261 index = -1;
1262 // The wake lock was being held, but we're not actually going to do any
1263 // broadcasts, so release the wake lock.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001266 }
1267
1268 // Now send the message.
1269 if (index >= 0) {
1270 // Acquire the broadcast wake lock before changing the power
1271 // state. It will be release after the broadcast is sent.
1272 // We always increment the ref count for each notification in the queue
1273 // and always decrement when that notification is handled.
1274 mBroadcastWakeLock.acquire();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001275 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
Joe Onorato128e7292009-03-24 18:41:31 -07001276 mHandler.post(mNotificationTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
1278 }
1279
1280 private Runnable mNotificationTask = new Runnable()
1281 {
1282 public void run()
1283 {
Joe Onorato128e7292009-03-24 18:41:31 -07001284 while (true) {
1285 int value;
1286 int why;
1287 WindowManagerPolicy policy;
1288 synchronized (mLocks) {
1289 value = mBroadcastQueue[0];
1290 why = mBroadcastWhy[0];
1291 for (int i=0; i<2; i++) {
1292 mBroadcastQueue[i] = mBroadcastQueue[i+1];
1293 mBroadcastWhy[i] = mBroadcastWhy[i+1];
1294 }
1295 policy = getPolicyLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
Joe Onorato128e7292009-03-24 18:41:31 -07001297 if (value == 1) {
1298 mScreenOnStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001299
Joe Onorato128e7292009-03-24 18:41:31 -07001300 policy.screenTurnedOn();
1301 try {
1302 ActivityManagerNative.getDefault().wakingUp();
1303 } catch (RemoteException e) {
1304 // ignore it
1305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306
Joe Onorato128e7292009-03-24 18:41:31 -07001307 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001308 Slog.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
Joe Onorato128e7292009-03-24 18:41:31 -07001309 }
1310 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1311 mContext.sendOrderedBroadcast(mScreenOnIntent, null,
1312 mScreenOnBroadcastDone, mHandler, 0, null, null);
1313 } else {
1314 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001315 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2,
Joe Onorato128e7292009-03-24 18:41:31 -07001316 mBroadcastWakeLock.mCount);
1317 mBroadcastWakeLock.release();
1318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 }
1320 }
Joe Onorato128e7292009-03-24 18:41:31 -07001321 else if (value == 0) {
1322 mScreenOffStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001323
Joe Onorato128e7292009-03-24 18:41:31 -07001324 policy.screenTurnedOff(why);
1325 try {
1326 ActivityManagerNative.getDefault().goingToSleep();
1327 } catch (RemoteException e) {
1328 // ignore it.
1329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330
Joe Onorato128e7292009-03-24 18:41:31 -07001331 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1332 mContext.sendOrderedBroadcast(mScreenOffIntent, null,
1333 mScreenOffBroadcastDone, mHandler, 0, null, null);
1334 } else {
1335 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001336 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3,
Joe Onorato128e7292009-03-24 18:41:31 -07001337 mBroadcastWakeLock.mCount);
1338 mBroadcastWakeLock.release();
1339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
Joe Onorato128e7292009-03-24 18:41:31 -07001342 else {
1343 // If we're in this case, then this handler is running for a previous
1344 // paired transaction. mBroadcastWakeLock will already have been released.
1345 break;
1346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 }
1348 }
1349 };
1350
1351 long mScreenOnStart;
1352 private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() {
1353 public void onReceive(Context context, Intent intent) {
1354 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001355 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount);
1357 mBroadcastWakeLock.release();
1358 }
1359 }
1360 };
1361
1362 long mScreenOffStart;
1363 private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() {
1364 public void onReceive(Context context, Intent intent) {
1365 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001366 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount);
1368 mBroadcastWakeLock.release();
1369 }
1370 }
1371 };
1372
1373 void logPointerUpEvent() {
1374 if (LOG_TOUCH_DOWNS) {
1375 mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown;
1376 mLastTouchDown = 0;
1377 }
1378 }
1379
1380 void logPointerDownEvent() {
1381 if (LOG_TOUCH_DOWNS) {
1382 // If we are not already timing a down/up sequence
1383 if (mLastTouchDown == 0) {
1384 mLastTouchDown = SystemClock.elapsedRealtime();
1385 mTouchCycles++;
1386 }
1387 }
1388 }
1389
1390 /**
1391 * Prevents the screen from turning on even if it *should* turn on due
1392 * to a subsequent full wake lock being acquired.
1393 * <p>
1394 * This is a temporary hack that allows an activity to "cover up" any
1395 * display glitches that happen during the activity's startup
1396 * sequence. (Specifically, this API was added to work around a
1397 * cosmetic bug in the "incoming call" sequence, where the lock screen
1398 * would flicker briefly before the incoming call UI became visible.)
1399 * TODO: There ought to be a more elegant way of doing this,
1400 * probably by having the PowerManager and ActivityManager
1401 * work together to let apps specify that the screen on/off
1402 * state should be synchronized with the Activity lifecycle.
1403 * <p>
1404 * Note that calling preventScreenOn(true) will NOT turn the screen
1405 * off if it's currently on. (This API only affects *future*
1406 * acquisitions of full wake locks.)
1407 * But calling preventScreenOn(false) WILL turn the screen on if
1408 * it's currently off because of a prior preventScreenOn(true) call.
1409 * <p>
1410 * Any call to preventScreenOn(true) MUST be followed promptly by a call
1411 * to preventScreenOn(false). In fact, if the preventScreenOn(false)
1412 * call doesn't occur within 5 seconds, we'll turn the screen back on
1413 * ourselves (and log a warning about it); this prevents a buggy app
1414 * from disabling the screen forever.)
1415 * <p>
1416 * TODO: this feature should really be controlled by a new type of poke
1417 * lock (rather than an IPowerManager call).
1418 */
1419 public void preventScreenOn(boolean prevent) {
1420 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1421
1422 synchronized (mLocks) {
1423 if (prevent) {
1424 // First of all, grab a partial wake lock to
1425 // make sure the CPU stays on during the entire
1426 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1427 mPreventScreenOnPartialLock.acquire();
1428
1429 // Post a forceReenableScreen() call (for 5 seconds in the
1430 // future) to make sure the matching preventScreenOn(false) call
1431 // has happened by then.
1432 mHandler.removeCallbacks(mForceReenableScreenTask);
1433 mHandler.postDelayed(mForceReenableScreenTask, 5000);
1434
1435 // Finally, set the flag that prevents the screen from turning on.
1436 // (Below, in setPowerState(), we'll check mPreventScreenOn and
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001437 // we *won't* call setScreenStateLocked(true) if it's set.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 mPreventScreenOn = true;
1439 } else {
1440 // (Re)enable the screen.
1441 mPreventScreenOn = false;
1442
1443 // We're "undoing" a the prior preventScreenOn(true) call, so we
1444 // no longer need the 5-second safeguard.
1445 mHandler.removeCallbacks(mForceReenableScreenTask);
1446
1447 // Forcibly turn on the screen if it's supposed to be on. (This
1448 // handles the case where the screen is currently off because of
1449 // a prior preventScreenOn(true) call.)
Mike Lockwoode090281422009-11-14 21:02:56 -05001450 if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001452 Slog.d(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 "preventScreenOn: turning on after a prior preventScreenOn(true)!");
1454 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001455 int err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 if (err != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001457 Slog.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 }
1459 }
1460
1461 // Release the partial wake lock that we held during the
1462 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1463 mPreventScreenOnPartialLock.release();
1464 }
1465 }
1466 }
1467
1468 public void setScreenBrightnessOverride(int brightness) {
1469 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1470
Mike Lockwoodf527c712010-06-10 14:12:33 -04001471 if (mSpew) Slog.d(TAG, "setScreenBrightnessOverride " + brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 synchronized (mLocks) {
1473 if (mScreenBrightnessOverride != brightness) {
1474 mScreenBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001475 if (isScreenOn()) {
1476 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 }
1479 }
1480 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001481
1482 public void setButtonBrightnessOverride(int brightness) {
1483 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1484
Mike Lockwoodf527c712010-06-10 14:12:33 -04001485 if (mSpew) Slog.d(TAG, "setButtonBrightnessOverride " + brightness);
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001486 synchronized (mLocks) {
1487 if (mButtonBrightnessOverride != brightness) {
1488 mButtonBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001489 if (isScreenOn()) {
1490 updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT);
1491 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001492 }
1493 }
1494 }
1495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 /**
1497 * Sanity-check that gets called 5 seconds after any call to
1498 * preventScreenOn(true). This ensures that the original call
1499 * is followed promptly by a call to preventScreenOn(false).
1500 */
1501 private void forceReenableScreen() {
1502 // We shouldn't get here at all if mPreventScreenOn is false, since
1503 // we should have already removed any existing
1504 // mForceReenableScreenTask messages...
1505 if (!mPreventScreenOn) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001506 Slog.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 return;
1508 }
1509
1510 // Uh oh. It's been 5 seconds since a call to
1511 // preventScreenOn(true) and we haven't re-enabled the screen yet.
1512 // This means the app that called preventScreenOn(true) is either
1513 // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)),
1514 // or buggy (i.e. it forgot to call preventScreenOn(false), or
1515 // crashed before doing so.)
1516
1517 // Log a warning, and forcibly turn the screen back on.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001518 Slog.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 + "Forcing the screen back on...");
1520 preventScreenOn(false);
1521 }
1522
1523 private Runnable mForceReenableScreenTask = new Runnable() {
1524 public void run() {
1525 forceReenableScreen();
1526 }
1527 };
1528
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001529 private int setScreenStateLocked(boolean on) {
1530 int err = Power.setScreenState(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001531 if (err == 0) {
1532 mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
1533 if (mUseSoftwareAutoBrightness) {
1534 enableLightSensor(on);
1535 if (!on) {
1536 // make sure button and key backlights are off too
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001537 mButtonLight.turnOff();
1538 mKeyboardLight.turnOff();
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001539 // clear current value so we will update based on the new conditions
1540 // when the sensor is reenabled.
1541 mLightSensorValue = -1;
Mike Lockwoodb2865412010-02-02 22:40:33 -05001542 // reset our highest light sensor value when the screen turns off
1543 mHighestLightSensorValue = -1;
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001544 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001545 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001546 }
1547 return err;
1548 }
1549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 private void setPowerState(int state)
1551 {
Mike Lockwood435eb642009-12-03 08:40:18 -05001552 setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554
Mike Lockwood435eb642009-12-03 08:40:18 -05001555 private void setPowerState(int newState, boolean noChangeLights, int reason)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 {
1557 synchronized (mLocks) {
1558 int err;
1559
1560 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001561 Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 + " newState=0x" + Integer.toHexString(newState)
Mike Lockwood435eb642009-12-03 08:40:18 -05001563 + " noChangeLights=" + noChangeLights
1564 + " reason=" + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
1566
1567 if (noChangeLights) {
1568 newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
1569 }
Mike Lockwood36fc3022009-08-25 16:49:06 -07001570 if (mProximitySensorActive) {
1571 // don't turn on the screen when the proximity sensor lock is held
1572 newState = (newState & ~SCREEN_BRIGHT);
1573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
1575 if (batteryIsLow()) {
1576 newState |= BATTERY_LOW_BIT;
1577 } else {
1578 newState &= ~BATTERY_LOW_BIT;
1579 }
1580 if (newState == mPowerState) {
1581 return;
1582 }
Mike Lockwood3333fa42009-10-26 14:50:42 -04001583
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001584 if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 newState |= ALL_BRIGHT;
1586 }
1587
1588 boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0;
1589 boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0;
1590
Mike Lockwood51b844962009-11-16 21:51:18 -05001591 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001592 Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 + " newState=" + newState + " noChangeLights=" + noChangeLights);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001596 Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001598 Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 Slog.d(TAG, " oldScreenOn=" + oldScreenOn
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 + " newScreenOn=" + newScreenOn);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001602 Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
1604 }
1605
1606 if (mPowerState != newState) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001607 updateLightsLocked(newState, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
1609 }
1610
1611 if (oldScreenOn != newScreenOn) {
1612 if (newScreenOn) {
Joe Onorato128e7292009-03-24 18:41:31 -07001613 // When the user presses the power button, we need to always send out the
1614 // notification that it's going to sleep so the keyguard goes on. But
1615 // we can't do that until the screen fades out, so we don't show the keyguard
1616 // too early.
1617 if (mStillNeedSleepNotification) {
1618 sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
1619 }
1620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 // Turn on the screen UNLESS there was a prior
1622 // preventScreenOn(true) request. (Note that the lifetime
1623 // of a single preventScreenOn() request is limited to 5
1624 // seconds to prevent a buggy app from disabling the
1625 // screen forever; see forceReenableScreen().)
1626 boolean reallyTurnScreenOn = true;
1627 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001628 Slog.d(TAG, "- turning screen on... mPreventScreenOn = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 + mPreventScreenOn);
1630 }
1631
1632 if (mPreventScreenOn) {
1633 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001634 Slog.d(TAG, "- PREVENTING screen from really turning on!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 }
1636 reallyTurnScreenOn = false;
1637 }
1638 if (reallyTurnScreenOn) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001639 err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 long identity = Binder.clearCallingIdentity();
1641 try {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001642 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 mBatteryStats.noteScreenOn();
1644 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001645 Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 } finally {
1647 Binder.restoreCallingIdentity(identity);
1648 }
1649 } else {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001650 setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 // But continue as if we really did turn the screen on...
1652 err = 0;
1653 }
1654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 mLastTouchDown = 0;
1656 mTotalTouchDownTime = 0;
1657 mTouchCycles = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001658 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 mTotalTouchDownTime, mTouchCycles);
1660 if (err == 0) {
1661 mPowerState |= SCREEN_ON_BIT;
1662 sendNotificationLocked(true, -1);
1663 }
1664 } else {
Mike Lockwood497087e32009-11-08 18:33:03 -05001665 // cancel light sensor task
1666 mHandler.removeCallbacks(mAutoBrightnessTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 mScreenOffTime = SystemClock.elapsedRealtime();
1668 long identity = Binder.clearCallingIdentity();
1669 try {
1670 mBatteryStats.noteScreenOff();
1671 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001672 Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 } finally {
1674 Binder.restoreCallingIdentity(identity);
1675 }
1676 mPowerState &= ~SCREEN_ON_BIT;
Mike Lockwood435eb642009-12-03 08:40:18 -05001677 mScreenOffReason = reason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 if (!mScreenBrightness.animating) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001679 err = screenOffFinishedAnimatingLocked(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 err = 0;
1682 mLastTouchDown = 0;
1683 }
1684 }
1685 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001686
1687 updateNativePowerStateLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
1689 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001690
1691 private void updateNativePowerStateLocked() {
1692 nativeSetPowerState(
1693 (mPowerState & SCREEN_ON_BIT) != 0,
1694 (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT);
1695 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001696
Mike Lockwood435eb642009-12-03 08:40:18 -05001697 private int screenOffFinishedAnimatingLocked(int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 // I don't think we need to check the current state here because all of these
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001699 // Power.setScreenState and sendNotificationLocked can both handle being
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 // called multiple times in the same state. -joeo
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001701 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime, mTouchCycles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 mLastTouchDown = 0;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001703 int err = setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 if (err == 0) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001705 mScreenOffReason = reason;
1706 sendNotificationLocked(false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 }
1708 return err;
1709 }
1710
1711 private boolean batteryIsLow() {
1712 return (!mIsPowered &&
1713 mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD);
1714 }
1715
The Android Open Source Project10592532009-03-18 17:39:46 -07001716 private void updateLightsLocked(int newState, int forceState) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001717 final int oldState = mPowerState;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001718 newState = applyButtonState(newState);
1719 newState = applyKeyboardState(newState);
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001720 final int realDifference = (newState ^ oldState);
1721 final int difference = realDifference | forceState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 if (difference == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001723 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 int offMask = 0;
1727 int dimMask = 0;
1728 int onMask = 0;
1729
1730 int preferredBrightness = getPreferredBrightness();
1731 boolean startAnimation = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 if ((difference & KEYBOARD_BRIGHT_BIT) != 0) {
1734 if (ANIMATE_KEYBOARD_LIGHTS) {
1735 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1736 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001737 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001738 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001740 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001741 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
1742 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744 startAnimation = true;
1745 } else {
1746 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001747 offMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001749 onMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751 }
1752 }
1753
1754 if ((difference & BUTTON_BRIGHT_BIT) != 0) {
1755 if (ANIMATE_BUTTON_LIGHTS) {
1756 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1757 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001758 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001759 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001761 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001762 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
1763 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765 startAnimation = true;
1766 } else {
1767 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001768 offMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001770 onMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 }
1772 }
1773 }
1774
1775 if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1776 if (ANIMATE_SCREEN_LIGHTS) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001777 int nominalCurrentValue = -1;
1778 // If there was an actual difference in the light state, then
1779 // figure out the "ideal" current value based on the previous
1780 // state. Otherwise, this is a change due to the brightness
1781 // override, so we want to animate from whatever the current
1782 // value is.
1783 if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1784 switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) {
1785 case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT:
1786 nominalCurrentValue = preferredBrightness;
1787 break;
1788 case SCREEN_ON_BIT:
1789 nominalCurrentValue = Power.BRIGHTNESS_DIM;
1790 break;
1791 case 0:
1792 nominalCurrentValue = Power.BRIGHTNESS_OFF;
1793 break;
1794 case SCREEN_BRIGHT_BIT:
1795 default:
1796 // not possible
1797 nominalCurrentValue = (int)mScreenBrightness.curValue;
1798 break;
1799 }
Joe Onorato128e7292009-03-24 18:41:31 -07001800 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001801 int brightness = preferredBrightness;
1802 int steps = ANIM_STEPS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1804 // dim or turn off backlight, depending on if the screen is on
1805 // the scale is because the brightness ramp isn't linear and this biases
1806 // it so the later parts take longer.
1807 final float scale = 1.5f;
1808 float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
1809 if (ratio > 1.0f) ratio = 1.0f;
1810 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
1812 // was bright
1813 steps = ANIM_STEPS;
1814 } else {
1815 // was dim
1816 steps = (int)(ANIM_STEPS*ratio*scale);
1817 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001818 brightness = Power.BRIGHTNESS_OFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 if ((oldState & SCREEN_ON_BIT) != 0) {
1821 // was bright
1822 steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
1823 } else {
1824 // was dim
1825 steps = (int)(ANIM_STEPS*ratio);
1826 }
1827 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
1828 // If the "stay on while plugged in" option is
1829 // turned on, then the screen will often not
1830 // automatically turn off while plugged in. To
1831 // still have a sense of when it is inactive, we
1832 // will then count going dim as turning off.
1833 mScreenOffTime = SystemClock.elapsedRealtime();
1834 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001835 brightness = Power.BRIGHTNESS_DIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001838 long identity = Binder.clearCallingIdentity();
1839 try {
1840 mBatteryStats.noteScreenBrightness(brightness);
1841 } catch (RemoteException e) {
1842 // Nothing interesting to do.
1843 } finally {
1844 Binder.restoreCallingIdentity(identity);
1845 }
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001846 if (mScreenBrightness.setTargetLocked(brightness,
1847 steps, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue)) {
1848 startAnimation = true;
1849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 } else {
1851 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1852 // dim or turn off backlight, depending on if the screen is on
1853 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001854 offMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001856 dimMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
1858 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001859 onMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
1861 }
1862 }
1863
1864 if (startAnimation) {
1865 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001866 Slog.i(TAG, "Scheduling light animator!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868 mHandler.removeCallbacks(mLightAnimator);
1869 mHandler.post(mLightAnimator);
1870 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 if (offMask != 0) {
Mike Lockwood48358bd2010-04-17 22:29:20 -04001873 if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001874 setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
1876 if (dimMask != 0) {
1877 int brightness = Power.BRIGHTNESS_DIM;
1878 if ((newState & BATTERY_LOW_BIT) != 0 &&
1879 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1880 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1881 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04001882 if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001883 setLightBrightness(dimMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
1885 if (onMask != 0) {
1886 int brightness = getPreferredBrightness();
1887 if ((newState & BATTERY_LOW_BIT) != 0 &&
1888 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1889 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1890 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04001891 if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001892 setLightBrightness(onMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895
The Android Open Source Project10592532009-03-18 17:39:46 -07001896 private void setLightBrightness(int mask, int value) {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05001897 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05001898 ? LightsService.BRIGHTNESS_MODE_SENSOR
1899 : LightsService.BRIGHTNESS_MODE_USER);
The Android Open Source Project10592532009-03-18 17:39:46 -07001900 if ((mask & SCREEN_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001901 mLcdLight.setBrightness(value, brightnessMode);
The Android Open Source Project10592532009-03-18 17:39:46 -07001902 }
1903 if ((mask & BUTTON_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001904 mButtonLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001905 }
1906 if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001907 mKeyboardLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
1910
1911 class BrightnessState {
1912 final int mask;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 boolean initialized;
1915 int targetValue;
1916 float curValue;
1917 float delta;
1918 boolean animating;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 BrightnessState(int m) {
1921 mask = m;
1922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 public void dump(PrintWriter pw, String prefix) {
1925 pw.println(prefix + "animating=" + animating
1926 + " targetValue=" + targetValue
1927 + " curValue=" + curValue
1928 + " delta=" + delta);
1929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001930
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001931 boolean setTargetLocked(int target, int stepsToTarget, int initialValue,
Joe Onorato128e7292009-03-24 18:41:31 -07001932 int nominalCurrentValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 if (!initialized) {
1934 initialized = true;
1935 curValue = (float)initialValue;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001936 } else if (targetValue == target) {
1937 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939 targetValue = target;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001940 delta = (targetValue -
1941 (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue))
1942 / stepsToTarget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 if (mSpew) {
Joe Onorato128e7292009-03-24 18:41:31 -07001944 String noticeMe = nominalCurrentValue == curValue ? "" : " ******************";
Joe Onorato8a9b2202010-02-26 18:56:32 -08001945 Slog.i(TAG, "Setting target " + mask + ": cur=" + curValue
Joe Onorato128e7292009-03-24 18:41:31 -07001946 + " target=" + targetValue + " delta=" + delta
1947 + " nominalCurrentValue=" + nominalCurrentValue
1948 + noticeMe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
1950 animating = true;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001951 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 boolean stepLocked() {
1955 if (!animating) return false;
1956 if (false && mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001957 Slog.i(TAG, "Step target " + mask + ": cur=" + curValue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 + " target=" + targetValue + " delta=" + delta);
1959 }
1960 curValue += delta;
1961 int curIntValue = (int)curValue;
1962 boolean more = true;
1963 if (delta == 0) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001964 curValue = curIntValue = targetValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 more = false;
1966 } else if (delta > 0) {
1967 if (curIntValue >= targetValue) {
1968 curValue = curIntValue = targetValue;
1969 more = false;
1970 }
1971 } else {
1972 if (curIntValue <= targetValue) {
1973 curValue = curIntValue = targetValue;
1974 more = false;
1975 }
1976 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001977 //Slog.i(TAG, "Animating brightess " + curIntValue + ": " + mask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001978 setLightBrightness(mask, curIntValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 animating = more;
1980 if (!more) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001981 if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001982 screenOffFinishedAnimatingLocked(mScreenOffReason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
1984 }
1985 return more;
1986 }
1987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 private class LightAnimator implements Runnable {
1990 public void run() {
1991 synchronized (mLocks) {
1992 long now = SystemClock.uptimeMillis();
1993 boolean more = mScreenBrightness.stepLocked();
1994 if (mKeyboardBrightness.stepLocked()) {
1995 more = true;
1996 }
1997 if (mButtonBrightness.stepLocked()) {
1998 more = true;
1999 }
2000 if (more) {
2001 mHandler.postAtTime(mLightAnimator, now+(1000/60));
2002 }
2003 }
2004 }
2005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 private int getPreferredBrightness() {
2008 try {
2009 if (mScreenBrightnessOverride >= 0) {
2010 return mScreenBrightnessOverride;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002011 } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness
Mike Lockwood27c6dd72009-11-04 08:57:07 -05002012 && mAutoBrightessEnabled) {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002013 return mLightSensorScreenBrightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 }
2015 final int brightness = Settings.System.getInt(mContext.getContentResolver(),
2016 SCREEN_BRIGHTNESS);
2017 // Don't let applications turn the screen all the way off
2018 return Math.max(brightness, Power.BRIGHTNESS_DIM);
2019 } catch (SettingNotFoundException snfe) {
2020 return Power.BRIGHTNESS_ON;
2021 }
2022 }
2023
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002024 private int applyButtonState(int state) {
2025 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002026 if ((state & BATTERY_LOW_BIT) != 0) {
2027 // do not override brightness if the battery is low
2028 return state;
2029 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002030 if (mButtonBrightnessOverride >= 0) {
2031 brightness = mButtonBrightnessOverride;
2032 } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
2033 brightness = mLightSensorButtonBrightness;
2034 }
2035 if (brightness > 0) {
2036 return state | BUTTON_BRIGHT_BIT;
2037 } else if (brightness == 0) {
2038 return state & ~BUTTON_BRIGHT_BIT;
2039 } else {
2040 return state;
2041 }
2042 }
2043
2044 private int applyKeyboardState(int state) {
2045 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002046 if ((state & BATTERY_LOW_BIT) != 0) {
2047 // do not override brightness if the battery is low
2048 return state;
2049 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002050 if (!mKeyboardVisible) {
2051 brightness = 0;
2052 } else if (mButtonBrightnessOverride >= 0) {
2053 brightness = mButtonBrightnessOverride;
2054 } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) {
2055 brightness = mLightSensorKeyboardBrightness;
2056 }
2057 if (brightness > 0) {
2058 return state | KEYBOARD_BRIGHT_BIT;
2059 } else if (brightness == 0) {
2060 return state & ~KEYBOARD_BRIGHT_BIT;
2061 } else {
2062 return state;
2063 }
2064 }
2065
Charles Mendis322591c2009-10-29 11:06:59 -07002066 public boolean isScreenOn() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 synchronized (mLocks) {
2068 return (mPowerState & SCREEN_ON_BIT) != 0;
2069 }
2070 }
2071
Charles Mendis322591c2009-10-29 11:06:59 -07002072 boolean isScreenBright() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 synchronized (mLocks) {
2074 return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT;
2075 }
2076 }
2077
Mike Lockwood497087e32009-11-08 18:33:03 -05002078 private boolean isScreenTurningOffLocked() {
2079 return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0);
2080 }
2081
Mike Lockwood200b30b2009-09-20 00:23:59 -04002082 private void forceUserActivityLocked() {
Mike Lockwoode090281422009-11-14 21:02:56 -05002083 if (isScreenTurningOffLocked()) {
2084 // cancel animation so userActivity will succeed
2085 mScreenBrightness.animating = false;
2086 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002087 boolean savedActivityAllowed = mUserActivityAllowed;
2088 mUserActivityAllowed = true;
2089 userActivity(SystemClock.uptimeMillis(), false);
2090 mUserActivityAllowed = savedActivityAllowed;
2091 }
2092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
2094 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Joe Onorato7999bff2010-07-24 11:50:05 -04002095 userActivity(time, -1, noChangeLights, OTHER_EVENT, force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097
2098 public void userActivity(long time, boolean noChangeLights) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002099 userActivity(time, -1, noChangeLights, OTHER_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
2101
2102 public void userActivity(long time, boolean noChangeLights, int eventType) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002103 userActivity(time, -1, noChangeLights, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
2105
2106 public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002107 userActivity(time, -1, noChangeLights, eventType, force);
2108 }
2109
2110 /*
2111 * Reset the user activity timeout to now + timeout. This overrides whatever else is going
2112 * on with user activity. Don't use this function.
2113 */
2114 public void clearUserActivityTimeout(long now, long timeout) {
2115 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2116 Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now");
2117 userActivity(now, timeout, false, OTHER_EVENT, false);
2118 }
2119
2120 private void userActivity(long time, long timeoutOverride, boolean noChangeLights,
2121 int eventType, boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 //mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2123
2124 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002125 && (eventType == CHEEK_EVENT || eventType == TOUCH_EVENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002127 Slog.d(TAG, "dropping cheek or short event mPokey=0x" + Integer.toHexString(mPokey));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
2129 return;
2130 }
2131
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002132 if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0)
2133 && (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT
2134 || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) {
2135 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002136 Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002137 }
2138 return;
2139 }
2140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 if (false) {
2142 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002143 Slog.d(TAG, "userActivity !!!");//, new RuntimeException());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002145 Slog.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
2147 }
2148
2149 synchronized (mLocks) {
2150 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002151 Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 + " mUserActivityAllowed=" + mUserActivityAllowed
2153 + " mUserState=0x" + Integer.toHexString(mUserState)
Mike Lockwood36fc3022009-08-25 16:49:06 -07002154 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
2155 + " mProximitySensorActive=" + mProximitySensorActive
Joe Onorato797e6882010-08-26 14:46:01 -04002156 + " timeoutOverride=" + timeoutOverride
Mike Lockwood36fc3022009-08-25 16:49:06 -07002157 + " force=" + force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 }
Mike Lockwood05067122009-10-27 23:07:25 -04002159 // ignore user activity if we are in the process of turning off the screen
Mike Lockwood497087e32009-11-08 18:33:03 -05002160 if (isScreenTurningOffLocked()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002161 Slog.d(TAG, "ignoring user activity while turning off screen");
Mike Lockwood05067122009-10-27 23:07:25 -04002162 return;
2163 }
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002164 // Disable proximity sensor if if user presses power key while we are in the
2165 // "waiting for proximity sensor to go negative" state.
2166 if (mProximitySensorActive && mProximityWakeLockCount == 0) {
2167 mProximitySensorActive = false;
2168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 if (mLastEventTime <= time || force) {
2170 mLastEventTime = time;
Mike Lockwood36fc3022009-08-25 16:49:06 -07002171 if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002172 // Only turn on button backlights if a button was pressed
2173 // and auto brightness is disabled
Mike Lockwood4984e732009-11-01 08:16:33 -05002174 if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
2176 } else {
2177 // don't clear button/keyboard backlights when the screen is touched.
2178 mUserState |= SCREEN_BRIGHT;
2179 }
2180
Dianne Hackborn617f8772009-03-31 15:04:46 -07002181 int uid = Binder.getCallingUid();
2182 long ident = Binder.clearCallingIdentity();
2183 try {
2184 mBatteryStats.noteUserActivity(uid, eventType);
2185 } catch (RemoteException e) {
2186 // Ignore
2187 } finally {
2188 Binder.restoreCallingIdentity(ident);
2189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002190
Michael Chane96440f2009-05-06 10:27:36 -07002191 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwood435eb642009-12-03 08:40:18 -05002192 setPowerState(mUserState | mWakeLockState, noChangeLights,
2193 WindowManagerPolicy.OFF_BECAUSE_OF_USER);
Joe Onorato7999bff2010-07-24 11:50:05 -04002194 setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
2196 }
2197 }
Mike Lockwoodef731622010-01-27 17:51:34 -05002198
2199 if (mPolicy != null) {
2200 mPolicy.userActivity();
2201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
2203
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002204 private int getAutoBrightnessValue(int sensorValue, int[] values) {
2205 try {
2206 int i;
2207 for (i = 0; i < mAutoBrightnessLevels.length; i++) {
2208 if (sensorValue < mAutoBrightnessLevels[i]) {
2209 break;
2210 }
2211 }
2212 return values[i];
2213 } catch (Exception e) {
2214 // guard against null pointer or index out of bounds errors
Joe Onorato8a9b2202010-02-26 18:56:32 -08002215 Slog.e(TAG, "getAutoBrightnessValue", e);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002216 return 255;
2217 }
2218 }
2219
Mike Lockwood20f87d72009-11-05 16:08:51 -05002220 private Runnable mProximityTask = new Runnable() {
2221 public void run() {
2222 synchronized (mLocks) {
2223 if (mProximityPendingValue != -1) {
2224 proximityChangedLocked(mProximityPendingValue == 1);
2225 mProximityPendingValue = -1;
2226 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002227 if (mProximityPartialLock.isHeld()) {
2228 mProximityPartialLock.release();
2229 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002230 }
2231 }
2232 };
2233
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002234 private Runnable mAutoBrightnessTask = new Runnable() {
2235 public void run() {
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002236 synchronized (mLocks) {
2237 int value = (int)mLightSensorPendingValue;
2238 if (value >= 0) {
2239 mLightSensorPendingValue = -1;
2240 lightSensorChangedLocked(value);
2241 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002242 }
2243 }
2244 };
2245
Mike Lockwoodb2865412010-02-02 22:40:33 -05002246 private void dockStateChanged(int state) {
2247 synchronized (mLocks) {
2248 mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED);
2249 if (mIsDocked) {
2250 mHighestLightSensorValue = -1;
2251 }
2252 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2253 // force lights recalculation
2254 int value = (int)mLightSensorValue;
2255 mLightSensorValue = -1;
2256 lightSensorChangedLocked(value);
2257 }
2258 }
2259 }
2260
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002261 private void lightSensorChangedLocked(int value) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002262 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002263 Slog.d(TAG, "lightSensorChangedLocked " + value);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002264 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002265
Mike Lockwoodb2865412010-02-02 22:40:33 -05002266 // do not allow light sensor value to decrease
2267 if (mHighestLightSensorValue < value) {
2268 mHighestLightSensorValue = value;
2269 }
2270
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002271 if (mLightSensorValue != value) {
2272 mLightSensorValue = value;
2273 if ((mPowerState & BATTERY_LOW_BIT) == 0) {
Mike Lockwoodb2865412010-02-02 22:40:33 -05002274 // use maximum light sensor value seen since screen went on for LCD to avoid flicker
2275 // we only do this if we are undocked, since lighting should be stable when
2276 // stationary in a dock.
2277 int lcdValue = getAutoBrightnessValue(
2278 (mIsDocked ? value : mHighestLightSensorValue),
2279 mLcdBacklightValues);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002280 int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
Mike Lockwooddf024922009-10-29 21:29:15 -04002281 int keyboardValue;
2282 if (mKeyboardVisible) {
2283 keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
2284 } else {
2285 keyboardValue = 0;
2286 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002287 mLightSensorScreenBrightness = lcdValue;
2288 mLightSensorButtonBrightness = buttonValue;
2289 mLightSensorKeyboardBrightness = keyboardValue;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002290
2291 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002292 Slog.d(TAG, "lcdValue " + lcdValue);
2293 Slog.d(TAG, "buttonValue " + buttonValue);
2294 Slog.d(TAG, "keyboardValue " + keyboardValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002295 }
2296
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002297 boolean startAnimation = false;
Mike Lockwood4984e732009-11-01 08:16:33 -05002298 if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002299 if (ANIMATE_SCREEN_LIGHTS) {
2300 if (mScreenBrightness.setTargetLocked(lcdValue,
2301 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS,
2302 (int)mScreenBrightness.curValue)) {
2303 startAnimation = true;
2304 }
2305 } else {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05002306 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05002307 ? LightsService.BRIGHTNESS_MODE_SENSOR
2308 : LightsService.BRIGHTNESS_MODE_USER);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002309 mLcdLight.setBrightness(lcdValue, brightnessMode);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002310 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002311 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002312 if (mButtonBrightnessOverride < 0) {
2313 if (ANIMATE_BUTTON_LIGHTS) {
2314 if (mButtonBrightness.setTargetLocked(buttonValue,
2315 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2316 (int)mButtonBrightness.curValue)) {
2317 startAnimation = true;
2318 }
2319 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002320 mButtonLight.setBrightness(buttonValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002321 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002322 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002323 if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) {
2324 if (ANIMATE_KEYBOARD_LIGHTS) {
2325 if (mKeyboardBrightness.setTargetLocked(keyboardValue,
2326 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2327 (int)mKeyboardBrightness.curValue)) {
2328 startAnimation = true;
2329 }
2330 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002331 mKeyboardLight.setBrightness(keyboardValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002332 }
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002333 }
2334 if (startAnimation) {
2335 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002336 Slog.i(TAG, "lightSensorChangedLocked scheduling light animator");
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002337 }
2338 mHandler.removeCallbacks(mLightAnimator);
2339 mHandler.post(mLightAnimator);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002340 }
2341 }
2342 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002343 }
2344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 /**
2346 * The user requested that we go to sleep (probably with the power button).
2347 * This overrides all wake locks that are held.
2348 */
2349 public void goToSleep(long time)
2350 {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002351 goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
2352 }
2353
2354 /**
2355 * The user requested that we go to sleep (probably with the power button).
2356 * This overrides all wake locks that are held.
2357 */
2358 public void goToSleepWithReason(long time, int reason)
2359 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2361 synchronized (mLocks) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002362 goToSleepLocked(time, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 }
2364 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 /**
Doug Zongker50a21f42009-11-19 12:49:53 -08002367 * Reboot the device immediately, passing 'reason' (may be null)
2368 * to the underlying __reboot system call. Should not return.
2369 */
2370 public void reboot(String reason)
2371 {
2372 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
San Mehat14e69af2010-01-06 14:58:18 -08002373
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002374 if (mHandler == null || !ActivityManagerNative.isSystemReady()) {
2375 throw new IllegalStateException("Too early to call reboot()");
2376 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002377
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002378 final String finalReason = reason;
2379 Runnable runnable = new Runnable() {
2380 public void run() {
2381 synchronized (this) {
2382 ShutdownThread.reboot(mContext, finalReason, false);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002383 }
2384
San Mehat1e512792010-01-07 10:40:29 -08002385 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002386 };
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002387 // ShutdownThread must run on a looper capable of displaying the UI.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002388 mHandler.post(runnable);
2389
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002390 // PowerManager.reboot() is documented not to return so just wait for the inevitable.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002391 synchronized (runnable) {
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002392 while (true) {
2393 try {
2394 runnable.wait();
2395 } catch (InterruptedException e) {
2396 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002397 }
Doug Zongker50a21f42009-11-19 12:49:53 -08002398 }
2399 }
2400
Dan Egnor60d87622009-12-16 16:32:58 -08002401 /**
2402 * Crash the runtime (causing a complete restart of the Android framework).
2403 * Requires REBOOT permission. Mostly for testing. Should not return.
2404 */
2405 public void crash(final String message)
2406 {
2407 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
2408 Thread t = new Thread("PowerManagerService.crash()") {
2409 public void run() { throw new RuntimeException(message); }
2410 };
2411 try {
2412 t.start();
2413 t.join();
2414 } catch (InterruptedException e) {
2415 Log.wtf(TAG, e);
2416 }
2417 }
2418
Mike Lockwood435eb642009-12-03 08:40:18 -05002419 private void goToSleepLocked(long time, int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420
2421 if (mLastEventTime <= time) {
2422 mLastEventTime = time;
2423 // cancel all of the wake locks
2424 mWakeLockState = SCREEN_OFF;
2425 int N = mLocks.size();
2426 int numCleared = 0;
2427 for (int i=0; i<N; i++) {
2428 WakeLock wl = mLocks.get(i);
2429 if (isScreenLock(wl.flags)) {
2430 mLocks.get(i).activated = false;
2431 numCleared++;
2432 }
2433 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002434 EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared);
Joe Onorato128e7292009-03-24 18:41:31 -07002435 mStillNeedSleepNotification = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 mUserState = SCREEN_OFF;
Mike Lockwood435eb642009-12-03 08:40:18 -05002437 setPowerState(SCREEN_OFF, false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 cancelTimerLocked();
2439 }
2440 }
2441
2442 public long timeSinceScreenOn() {
2443 synchronized (mLocks) {
2444 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2445 return 0;
2446 }
2447 return SystemClock.elapsedRealtime() - mScreenOffTime;
2448 }
2449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 public void setKeyboardVisibility(boolean visible) {
Mike Lockwooda625b382009-09-12 17:36:03 -07002452 synchronized (mLocks) {
2453 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002454 Slog.d(TAG, "setKeyboardVisibility: " + visible);
Mike Lockwooda625b382009-09-12 17:36:03 -07002455 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002456 if (mKeyboardVisible != visible) {
2457 mKeyboardVisible = visible;
2458 // don't signal user activity if the screen is off; other code
2459 // will take care of turning on due to a true change to the lid
2460 // switch and synchronized with the lock screen.
2461 if ((mPowerState & SCREEN_ON_BIT) != 0) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002462 if (mUseSoftwareAutoBrightness) {
Mike Lockwooddf024922009-10-29 21:29:15 -04002463 // force recompute of backlight values
2464 if (mLightSensorValue >= 0) {
2465 int value = (int)mLightSensorValue;
2466 mLightSensorValue = -1;
2467 lightSensorChangedLocked(value);
2468 }
2469 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002470 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2471 }
Mike Lockwooda625b382009-09-12 17:36:03 -07002472 }
2473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 }
2475
2476 /**
2477 * When the keyguard is up, it manages the power state, and userActivity doesn't do anything.
Mike Lockwood50c548d2009-11-09 16:02:06 -05002478 * When disabling user activity we also reset user power state so the keyguard can reset its
2479 * short screen timeout when keyguard is unhidden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002480 */
2481 public void enableUserActivity(boolean enabled) {
Mike Lockwood50c548d2009-11-09 16:02:06 -05002482 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002483 Slog.d(TAG, "enableUserActivity " + enabled);
Mike Lockwood50c548d2009-11-09 16:02:06 -05002484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 synchronized (mLocks) {
2486 mUserActivityAllowed = enabled;
Mike Lockwood50c548d2009-11-09 16:02:06 -05002487 if (!enabled) {
2488 // cancel timeout and clear mUserState so the keyguard can set a short timeout
2489 setTimeoutLocked(SystemClock.uptimeMillis(), 0);
2490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 }
2492 }
2493
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002494 private void setScreenBrightnessMode(int mode) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002495 boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
Mike Lockwoodf90ffcc2009-11-03 11:41:27 -05002496 if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002497 mAutoBrightessEnabled = enabled;
Charles Mendis322591c2009-10-29 11:06:59 -07002498 if (isScreenOn()) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002499 // force recompute of backlight values
2500 if (mLightSensorValue >= 0) {
2501 int value = (int)mLightSensorValue;
2502 mLightSensorValue = -1;
2503 lightSensorChangedLocked(value);
2504 }
Mike Lockwood2d155d22009-10-27 09:32:30 -04002505 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002506 }
2507 }
2508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 /** Sets the screen off timeouts:
2510 * mKeylightDelay
2511 * mDimDelay
2512 * mScreenOffDelay
2513 * */
2514 private void setScreenOffTimeoutsLocked() {
2515 if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
Doug Zongker43866e02010-01-07 12:09:54 -08002516 mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 mDimDelay = -1;
2518 mScreenOffDelay = 0;
2519 } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
2520 mKeylightDelay = MEDIUM_KEYLIGHT_DELAY;
2521 mDimDelay = -1;
2522 mScreenOffDelay = 0;
2523 } else {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08002524 int totalDelay = mScreenOffTimeoutSetting;
2525 if (totalDelay > mMaximumScreenOffTimeout) {
2526 totalDelay = mMaximumScreenOffTimeout;
2527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 mKeylightDelay = LONG_KEYLIGHT_DELAY;
2529 if (totalDelay < 0) {
Jim Millerbc4603b2010-08-30 21:21:34 -07002530 // negative number means stay on as long as possible.
2531 mScreenOffDelay = mMaximumScreenOffTimeout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 } else if (mKeylightDelay < totalDelay) {
2533 // subtract the time that the keylight delay. This will give us the
2534 // remainder of the time that we need to sleep to get the accurate
2535 // screen off timeout.
2536 mScreenOffDelay = totalDelay - mKeylightDelay;
2537 } else {
2538 mScreenOffDelay = 0;
2539 }
2540 if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) {
2541 mDimDelay = mScreenOffDelay - LONG_DIM_TIME;
2542 mScreenOffDelay = LONG_DIM_TIME;
2543 } else {
2544 mDimDelay = -1;
2545 }
2546 }
2547 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002548 Slog.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay
2550 + " mDimScreen=" + mDimScreen);
2551 }
2552 }
2553
2554 /**
Doug Zongker43866e02010-01-07 12:09:54 -08002555 * Refreshes cached secure settings. Called once on startup, and
2556 * on subsequent changes to secure settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 */
Doug Zongker43866e02010-01-07 12:09:54 -08002558 private void updateSettingsValues() {
2559 mShortKeylightDelay = Settings.Secure.getInt(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 mContext.getContentResolver(),
Doug Zongker43866e02010-01-07 12:09:54 -08002561 Settings.Secure.SHORT_KEYLIGHT_DELAY_MS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 SHORT_KEYLIGHT_DELAY_DEFAULT);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002563 // Slog.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 }
2565
2566 private class LockList extends ArrayList<WakeLock>
2567 {
2568 void addLock(WakeLock wl)
2569 {
2570 int index = getIndex(wl.binder);
2571 if (index < 0) {
2572 this.add(wl);
2573 }
2574 }
2575
2576 WakeLock removeLock(IBinder binder)
2577 {
2578 int index = getIndex(binder);
2579 if (index >= 0) {
2580 return this.remove(index);
2581 } else {
2582 return null;
2583 }
2584 }
2585
2586 int getIndex(IBinder binder)
2587 {
2588 int N = this.size();
2589 for (int i=0; i<N; i++) {
2590 if (this.get(i).binder == binder) {
2591 return i;
2592 }
2593 }
2594 return -1;
2595 }
2596
2597 int gatherState()
2598 {
2599 int result = 0;
2600 int N = this.size();
2601 for (int i=0; i<N; i++) {
2602 WakeLock wl = this.get(i);
2603 if (wl.activated) {
2604 if (isScreenLock(wl.flags)) {
2605 result |= wl.minState;
2606 }
2607 }
2608 }
2609 return result;
2610 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002611
Michael Chane96440f2009-05-06 10:27:36 -07002612 int reactivateScreenLocksLocked()
2613 {
2614 int result = 0;
2615 int N = this.size();
2616 for (int i=0; i<N; i++) {
2617 WakeLock wl = this.get(i);
2618 if (isScreenLock(wl.flags)) {
2619 wl.activated = true;
2620 result |= wl.minState;
2621 }
2622 }
2623 return result;
2624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 }
2626
2627 void setPolicy(WindowManagerPolicy p) {
2628 synchronized (mLocks) {
2629 mPolicy = p;
2630 mLocks.notifyAll();
2631 }
2632 }
2633
2634 WindowManagerPolicy getPolicyLocked() {
2635 while (mPolicy == null || !mDoneBooting) {
2636 try {
2637 mLocks.wait();
2638 } catch (InterruptedException e) {
2639 // Ignore
2640 }
2641 }
2642 return mPolicy;
2643 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 void systemReady() {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002646 mSensorManager = new SensorManager(mHandlerThread.getLooper());
2647 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
2648 // don't bother with the light sensor if auto brightness is handled in hardware
Mike Lockwoodaa66ea82009-10-31 16:31:27 -04002649 if (mUseSoftwareAutoBrightness) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002650 mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Mike Lockwood4984e732009-11-01 08:16:33 -05002651 enableLightSensor(true);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002652 }
2653
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002654 // wait until sensors are enabled before turning on screen.
2655 // some devices will not activate the light sensor properly on boot
2656 // unless we do this.
2657 if (mUseSoftwareAutoBrightness) {
2658 // turn the screen on
2659 setPowerState(SCREEN_BRIGHT);
2660 } else {
2661 // turn everything on
2662 setPowerState(ALL_BRIGHT);
2663 }
2664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 synchronized (mLocks) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002666 Slog.d(TAG, "system ready!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 mDoneBooting = true;
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002668
Dianne Hackborn617f8772009-03-31 15:04:46 -07002669 long identity = Binder.clearCallingIdentity();
2670 try {
2671 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
2672 mBatteryStats.noteScreenOn();
2673 } catch (RemoteException e) {
2674 // Nothing interesting to do.
2675 } finally {
2676 Binder.restoreCallingIdentity(identity);
2677 }
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002678 }
2679 }
2680
2681 void bootCompleted() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002682 Slog.d(TAG, "bootCompleted");
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002683 synchronized (mLocks) {
2684 mBootCompleted = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2686 updateWakeLockLocked();
2687 mLocks.notifyAll();
2688 }
2689 }
2690
2691 public void monitor() {
2692 synchronized (mLocks) { }
2693 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002694
2695 public int getSupportedWakeLockFlags() {
2696 int result = PowerManager.PARTIAL_WAKE_LOCK
2697 | PowerManager.FULL_WAKE_LOCK
2698 | PowerManager.SCREEN_DIM_WAKE_LOCK;
2699
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002700 if (mProximitySensor != null) {
2701 result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
2702 }
2703
2704 return result;
2705 }
2706
Mike Lockwood237a2992009-09-15 14:42:16 -04002707 public void setBacklightBrightness(int brightness) {
2708 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2709 // Don't let applications turn the screen all the way off
2710 brightness = Math.max(brightness, Power.BRIGHTNESS_DIM);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002711 mLcdLight.setBrightness(brightness);
2712 mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
2713 mButtonLight.setBrightness(brightness);
Mike Lockwood237a2992009-09-15 14:42:16 -04002714 long identity = Binder.clearCallingIdentity();
2715 try {
2716 mBatteryStats.noteScreenBrightness(brightness);
2717 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002718 Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
Mike Lockwood237a2992009-09-15 14:42:16 -04002719 } finally {
2720 Binder.restoreCallingIdentity(identity);
2721 }
2722
2723 // update our animation state
2724 if (ANIMATE_SCREEN_LIGHTS) {
2725 mScreenBrightness.curValue = brightness;
2726 mScreenBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002727 mScreenBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002728 }
2729 if (ANIMATE_KEYBOARD_LIGHTS) {
2730 mKeyboardBrightness.curValue = brightness;
2731 mKeyboardBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002732 mKeyboardBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002733 }
2734 if (ANIMATE_BUTTON_LIGHTS) {
2735 mButtonBrightness.curValue = brightness;
2736 mButtonBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002737 mButtonBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002738 }
2739 }
2740
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002741 public void setAttentionLight(boolean on, int color) {
2742 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002743 mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002744 }
2745
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002746 private void enableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002747 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002748 Slog.d(TAG, "enableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07002749 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002750 if (!mProximitySensorEnabled) {
2751 // clear calling identity so sensor manager battery stats are accurate
2752 long identity = Binder.clearCallingIdentity();
2753 try {
2754 mSensorManager.registerListener(mProximityListener, mProximitySensor,
2755 SensorManager.SENSOR_DELAY_NORMAL);
2756 mProximitySensorEnabled = true;
2757 } finally {
2758 Binder.restoreCallingIdentity(identity);
2759 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002760 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002761 }
2762
2763 private void disableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002764 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002765 Slog.d(TAG, "disableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07002766 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002767 if (mProximitySensorEnabled) {
2768 // clear calling identity so sensor manager battery stats are accurate
2769 long identity = Binder.clearCallingIdentity();
2770 try {
2771 mSensorManager.unregisterListener(mProximityListener);
2772 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002773 if (mProximityPartialLock.isHeld()) {
2774 mProximityPartialLock.release();
2775 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002776 mProximitySensorEnabled = false;
2777 } finally {
2778 Binder.restoreCallingIdentity(identity);
2779 }
2780 if (mProximitySensorActive) {
2781 mProximitySensorActive = false;
2782 forceUserActivityLocked();
2783 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002784 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002785 }
2786
Mike Lockwood20f87d72009-11-05 16:08:51 -05002787 private void proximityChangedLocked(boolean active) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002788 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002789 Slog.d(TAG, "proximityChangedLocked, active: " + active);
Mike Lockwood20f87d72009-11-05 16:08:51 -05002790 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002791 if (!mProximitySensorEnabled) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002792 Slog.d(TAG, "Ignoring proximity change after sensor is disabled");
Mike Lockwood0d72f7e2009-11-05 20:53:00 -05002793 return;
2794 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002795 if (active) {
Mike Lockwood435eb642009-12-03 08:40:18 -05002796 goToSleepLocked(SystemClock.uptimeMillis(),
2797 WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR);
Mike Lockwood20f87d72009-11-05 16:08:51 -05002798 mProximitySensorActive = true;
2799 } else {
2800 // proximity sensor negative events trigger as user activity.
2801 // temporarily set mUserActivityAllowed to true so this will work
2802 // even when the keyguard is on.
2803 mProximitySensorActive = false;
2804 forceUserActivityLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002805
2806 if (mProximityWakeLockCount == 0) {
2807 // disable sensor if we have no listeners left after proximity negative
2808 disableProximityLockLocked();
2809 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002810 }
2811 }
2812
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002813 private void enableLightSensor(boolean enable) {
2814 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002815 Slog.d(TAG, "enableLightSensor " + enable);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002816 }
2817 if (mSensorManager != null && mLightSensorEnabled != enable) {
2818 mLightSensorEnabled = enable;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002819 // clear calling identity so sensor manager battery stats are accurate
2820 long identity = Binder.clearCallingIdentity();
2821 try {
2822 if (enable) {
2823 mSensorManager.registerListener(mLightListener, mLightSensor,
2824 SensorManager.SENSOR_DELAY_NORMAL);
2825 } else {
2826 mSensorManager.unregisterListener(mLightListener);
2827 mHandler.removeCallbacks(mAutoBrightnessTask);
2828 }
2829 } finally {
2830 Binder.restoreCallingIdentity(identity);
Mike Lockwood06952d92009-08-13 16:05:38 -04002831 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002832 }
2833 }
2834
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002835 SensorEventListener mProximityListener = new SensorEventListener() {
2836 public void onSensorChanged(SensorEvent event) {
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002837 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002838 synchronized (mLocks) {
2839 float distance = event.values[0];
Mike Lockwood20f87d72009-11-05 16:08:51 -05002840 long timeSinceLastEvent = milliseconds - mLastProximityEventTime;
2841 mLastProximityEventTime = milliseconds;
2842 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002843 boolean proximityTaskQueued = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -05002844
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002845 // compare against getMaximumRange to support sensors that only return 0 or 1
Mike Lockwood20f87d72009-11-05 16:08:51 -05002846 boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD &&
2847 distance < mProximitySensor.getMaximumRange());
2848
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002849 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002850 Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active);
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002851 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002852 if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) {
2853 // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing
2854 mProximityPendingValue = (active ? 1 : 0);
2855 mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002856 proximityTaskQueued = true;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002857 } else {
Mike Lockwood20f87d72009-11-05 16:08:51 -05002858 // process the value immediately
2859 mProximityPendingValue = -1;
2860 proximityChangedLocked(active);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002861 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002862
2863 // update mProximityPartialLock state
2864 boolean held = mProximityPartialLock.isHeld();
2865 if (!held && proximityTaskQueued) {
2866 // hold wakelock until mProximityTask runs
2867 mProximityPartialLock.acquire();
2868 } else if (held && !proximityTaskQueued) {
2869 mProximityPartialLock.release();
2870 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002871 }
2872 }
2873
2874 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2875 // ignore
2876 }
2877 };
2878
2879 SensorEventListener mLightListener = new SensorEventListener() {
2880 public void onSensorChanged(SensorEvent event) {
2881 synchronized (mLocks) {
Mike Lockwood497087e32009-11-08 18:33:03 -05002882 // ignore light sensor while screen is turning off
2883 if (isScreenTurningOffLocked()) {
2884 return;
2885 }
2886
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002887 int value = (int)event.values[0];
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002888 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002889 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002890 Slog.d(TAG, "onSensorChanged: light value: " + value);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002891 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002892 mHandler.removeCallbacks(mAutoBrightnessTask);
2893 if (mLightSensorValue != value) {
Mike Lockwood20ee6f22009-11-07 20:33:47 -05002894 if (mLightSensorValue == -1 ||
2895 milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
2896 // process the value immediately if screen has just turned on
Mike Lockwood6c97fca2009-10-20 08:10:00 -04002897 lightSensorChangedLocked(value);
2898 } else {
2899 // delay processing to debounce the sensor
2900 mLightSensorPendingValue = value;
2901 mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
2902 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002903 } else {
2904 mLightSensorPendingValue = -1;
2905 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002906 }
2907 }
2908
2909 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2910 // ignore
2911 }
2912 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913}