blob: af8d7d42d85d3961d027af312d6a8156689e26c8 [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) {
663 try {
664 if (ws != null) {
665 mBatteryStats.noteStartWakelockFromSource(ws, wl.pid, wl.tag,
666 wl.monitorType);
667 } else {
668 mBatteryStats.noteStartWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
669 }
670 } catch (RemoteException e) {
671 // Ignore
672 }
673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700675 void noteStopWakeLocked(WakeLock wl, WorkSource ws) {
676 try {
677 if (ws != null) {
678 mBatteryStats.noteStopWakelockFromSource(ws, wl.pid, wl.tag,
679 wl.monitorType);
680 } else {
681 mBatteryStats.noteStopWakelock(wl.uid, wl.pid, wl.tag, wl.monitorType);
682 }
683 } catch (RemoteException e) {
684 // Ignore
685 }
686 }
687
688 public void acquireWakeLockLocked(int flags, IBinder lock, int uid, int pid, String tag,
689 WorkSource ws) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800691 Slog.d(TAG, "acquireWakeLock flags=0x" + Integer.toHexString(flags) + " tag=" + tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700694 if (ws != null && ws.size() == 0) {
695 ws = null;
696 }
697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 int index = mLocks.getIndex(lock);
699 WakeLock wl;
700 boolean newlock;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700701 boolean diffsource;
702 WorkSource oldsource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 if (index < 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700704 wl = new WakeLock(flags, lock, tag, uid, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 switch (wl.flags & LOCK_MASK)
706 {
707 case PowerManager.FULL_WAKE_LOCK:
Mike Lockwood4984e732009-11-01 08:16:33 -0500708 if (mUseSoftwareAutoBrightness) {
Mike Lockwood3333fa42009-10-26 14:50:42 -0400709 wl.minState = SCREEN_BRIGHT;
710 } else {
711 wl.minState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 break;
714 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
715 wl.minState = SCREEN_BRIGHT;
716 break;
717 case PowerManager.SCREEN_DIM_WAKE_LOCK:
718 wl.minState = SCREEN_DIM;
719 break;
720 case PowerManager.PARTIAL_WAKE_LOCK:
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700721 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 break;
723 default:
724 // just log and bail. we're in the server, so don't
725 // throw an exception.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800726 Slog.e(TAG, "bad wakelock type for lock '" + tag + "' "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 + " flags=" + flags);
728 return;
729 }
730 mLocks.addLock(wl);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700731 if (ws != null) {
732 wl.ws = new WorkSource(ws);
733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 newlock = true;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700735 diffsource = false;
736 oldsource = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 } else {
738 wl = mLocks.get(index);
739 newlock = false;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700740 oldsource = wl.ws;
741 if (oldsource != null) {
742 if (ws == null) {
743 wl.ws = null;
744 diffsource = true;
745 } else {
746 diffsource = oldsource.diff(ws);
747 }
748 } else if (ws != null) {
749 diffsource = true;
750 } else {
751 diffsource = false;
752 }
753 if (diffsource) {
754 wl.ws = new WorkSource(ws);
755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757 if (isScreenLock(flags)) {
758 // if this causes a wakeup, we reactivate all of the locks and
759 // set it to whatever they want. otherwise, we modulate that
760 // by the current state so we never turn it more on than
761 // it already is.
762 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
Michael Chane96440f2009-05-06 10:27:36 -0700763 int oldWakeLockState = mWakeLockState;
764 mWakeLockState = mLocks.reactivateScreenLocksLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800766 Slog.d(TAG, "wakeup here mUserState=0x" + Integer.toHexString(mUserState)
Michael Chane96440f2009-05-06 10:27:36 -0700767 + " mWakeLockState=0x"
768 + Integer.toHexString(mWakeLockState)
769 + " previous wakeLockState=0x" + Integer.toHexString(oldWakeLockState));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 } else {
772 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800773 Slog.d(TAG, "here mUserState=0x" + Integer.toHexString(mUserState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 + " mLocks.gatherState()=0x"
775 + Integer.toHexString(mLocks.gatherState())
776 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState));
777 }
778 mWakeLockState = (mUserState | mWakeLockState) & mLocks.gatherState();
779 }
780 setPowerState(mWakeLockState | mUserState);
781 }
782 else if ((flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
783 if (newlock) {
784 mPartialCount++;
785 if (mPartialCount == 1) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 1, tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 }
788 }
789 Power.acquireWakeLock(Power.PARTIAL_WAKE_LOCK,PARTIAL_NAME);
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700790 } else if ((flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500791 mProximityWakeLockCount++;
792 if (mProximityWakeLockCount == 1) {
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700793 enableProximityLockLocked();
794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700797 if (diffsource) {
798 // If the lock sources have changed, need to first release the
799 // old ones.
800 noteStopWakeLocked(wl, oldsource);
801 }
802 if (newlock || diffsource) {
803 noteStartWakeLocked(wl, ws);
804 }
805 }
806
807 public void updateWakeLockWorkSource(IBinder lock, WorkSource ws) {
808 int uid = Binder.getCallingUid();
809 int pid = Binder.getCallingPid();
810 if (ws != null && ws.size() == 0) {
811 ws = null;
812 }
813 if (ws != null) {
814 enforceWakeSourcePermission(uid, pid);
815 }
816 long ident = Binder.clearCallingIdentity();
817 try {
818 synchronized (mLocks) {
819 int index = mLocks.getIndex(lock);
820 if (index < 0) {
821 throw new IllegalArgumentException("Wake lock not active");
822 }
823 WakeLock wl = mLocks.get(index);
824 WorkSource oldsource = wl.ws;
825 wl.ws = ws != null ? new WorkSource(ws) : null;
826 noteStopWakeLocked(wl, oldsource);
827 noteStartWakeLocked(wl, ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 }
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700829 } finally {
830 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 }
832 }
833
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500834 public void releaseWakeLock(IBinder lock, int flags) {
Michael Chane96440f2009-05-06 10:27:36 -0700835 int uid = Binder.getCallingUid();
836 if (uid != Process.myUid()) {
837 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.WAKE_LOCK, null);
838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839
840 synchronized (mLocks) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500841 releaseWakeLockLocked(lock, flags, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843 }
844
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500845 private void releaseWakeLockLocked(IBinder lock, int flags, boolean death) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 WakeLock wl = mLocks.removeLock(lock);
847 if (wl == null) {
848 return;
849 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800852 Slog.d(TAG, "releaseWakeLock flags=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 + Integer.toHexString(wl.flags) + " tag=" + wl.tag);
854 }
855
856 if (isScreenLock(wl.flags)) {
857 mWakeLockState = mLocks.gatherState();
858 // goes in the middle to reduce flicker
859 if ((wl.flags & PowerManager.ON_AFTER_RELEASE) != 0) {
860 userActivity(SystemClock.uptimeMillis(), false);
861 }
862 setPowerState(mWakeLockState | mUserState);
863 }
864 else if ((wl.flags & LOCK_MASK) == PowerManager.PARTIAL_WAKE_LOCK) {
865 mPartialCount--;
866 if (mPartialCount == 0) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800867 if (LOG_PARTIAL_WL) EventLog.writeEvent(EventLogTags.POWER_PARTIAL_WAKE_STATE, 0, wl.tag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 Power.releaseWakeLock(PARTIAL_NAME);
869 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700870 } else if ((wl.flags & LOCK_MASK) == PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500871 mProximityWakeLockCount--;
872 if (mProximityWakeLockCount == 0) {
Mike Lockwood0e39ea82009-11-18 15:37:10 -0500873 if (mProximitySensorActive &&
874 ((flags & PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE) != 0)) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500875 // wait for proximity sensor to go negative before disabling sensor
876 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800877 Slog.d(TAG, "waiting for proximity sensor to go negative");
Mike Lockwoodee2b0942009-11-09 14:09:02 -0500878 }
879 } else {
880 disableProximityLockLocked();
881 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -0700882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 }
884 // Unlink the lock from the binder.
885 wl.binder.unlinkToDeath(wl, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700887 if (wl.monitorType >= 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 long origId = Binder.clearCallingIdentity();
889 try {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700890 noteStopWakeLocked(wl, wl.ws);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 } finally {
892 Binder.restoreCallingIdentity(origId);
893 }
894 }
895 }
896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 private class PokeLock implements IBinder.DeathRecipient
898 {
899 PokeLock(int p, IBinder b, String t) {
900 super();
901 this.pokey = p;
902 this.binder = b;
903 this.tag = t;
904 try {
905 b.linkToDeath(this, 0);
906 } catch (RemoteException e) {
907 binderDied();
908 }
909 }
910 public void binderDied() {
911 setPokeLock(0, this.binder, this.tag);
912 }
913 int pokey;
914 IBinder binder;
915 String tag;
916 boolean awakeOnSet;
917 }
918
919 public void setPokeLock(int pokey, IBinder token, String tag) {
920 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
921 if (token == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800922 Slog.e(TAG, "setPokeLock got null token for tag='" + tag + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 return;
924 }
925
926 if ((pokey & POKE_LOCK_TIMEOUT_MASK) == POKE_LOCK_TIMEOUT_MASK) {
927 throw new IllegalArgumentException("setPokeLock can't have both POKE_LOCK_SHORT_TIMEOUT"
928 + " and POKE_LOCK_MEDIUM_TIMEOUT");
929 }
930
931 synchronized (mLocks) {
932 if (pokey != 0) {
933 PokeLock p = mPokeLocks.get(token);
934 int oldPokey = 0;
935 if (p != null) {
936 oldPokey = p.pokey;
937 p.pokey = pokey;
938 } else {
939 p = new PokeLock(pokey, token, tag);
940 mPokeLocks.put(token, p);
941 }
942 int oldTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
943 int newTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
944 if (((mPowerState & SCREEN_ON_BIT) == 0) && (oldTimeout != newTimeout)) {
945 p.awakeOnSet = true;
946 }
947 } else {
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -0700948 PokeLock rLock = mPokeLocks.remove(token);
949 if (rLock != null) {
950 token.unlinkToDeath(rLock, 0);
951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
953
954 int oldPokey = mPokey;
955 int cumulative = 0;
956 boolean oldAwakeOnSet = mPokeAwakeOnSet;
957 boolean awakeOnSet = false;
958 for (PokeLock p: mPokeLocks.values()) {
959 cumulative |= p.pokey;
960 if (p.awakeOnSet) {
961 awakeOnSet = true;
962 }
963 }
964 mPokey = cumulative;
965 mPokeAwakeOnSet = awakeOnSet;
966
967 int oldCumulativeTimeout = oldPokey & POKE_LOCK_TIMEOUT_MASK;
968 int newCumulativeTimeout = pokey & POKE_LOCK_TIMEOUT_MASK;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 if (oldCumulativeTimeout != newCumulativeTimeout) {
971 setScreenOffTimeoutsLocked();
972 // reset the countdown timer, but use the existing nextState so it doesn't
973 // change anything
974 setTimeoutLocked(SystemClock.uptimeMillis(), mTimeoutTask.nextState);
975 }
976 }
977 }
978
979 private static String lockType(int type)
980 {
981 switch (type)
982 {
983 case PowerManager.FULL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700984 return "FULL_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 case PowerManager.SCREEN_BRIGHT_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700986 return "SCREEN_BRIGHT_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 case PowerManager.SCREEN_DIM_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700988 return "SCREEN_DIM_WAKE_LOCK ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 case PowerManager.PARTIAL_WAKE_LOCK:
David Brown251faa62009-08-02 22:04:36 -0700990 return "PARTIAL_WAKE_LOCK ";
991 case PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK:
992 return "PROXIMITY_SCREEN_OFF_WAKE_LOCK";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 default:
David Brown251faa62009-08-02 22:04:36 -0700994 return "??? ";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996 }
997
998 private static String dumpPowerState(int state) {
999 return (((state & KEYBOARD_BRIGHT_BIT) != 0)
1000 ? "KEYBOARD_BRIGHT_BIT " : "")
1001 + (((state & SCREEN_BRIGHT_BIT) != 0)
1002 ? "SCREEN_BRIGHT_BIT " : "")
1003 + (((state & SCREEN_ON_BIT) != 0)
1004 ? "SCREEN_ON_BIT " : "")
1005 + (((state & BATTERY_LOW_BIT) != 0)
1006 ? "BATTERY_LOW_BIT " : "");
1007 }
1008
1009 @Override
1010 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1011 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1012 != PackageManager.PERMISSION_GRANTED) {
1013 pw.println("Permission Denial: can't dump PowerManager from from pid="
1014 + Binder.getCallingPid()
1015 + ", uid=" + Binder.getCallingUid());
1016 return;
1017 }
1018
1019 long now = SystemClock.uptimeMillis();
1020
Mike Lockwoodca44df82010-02-25 13:48:49 -05001021 synchronized (mLocks) {
1022 pw.println("Power Manager State:");
1023 pw.println(" mIsPowered=" + mIsPowered
1024 + " mPowerState=" + mPowerState
1025 + " mScreenOffTime=" + (SystemClock.elapsedRealtime()-mScreenOffTime)
1026 + " ms");
1027 pw.println(" mPartialCount=" + mPartialCount);
1028 pw.println(" mWakeLockState=" + dumpPowerState(mWakeLockState));
1029 pw.println(" mUserState=" + dumpPowerState(mUserState));
1030 pw.println(" mPowerState=" + dumpPowerState(mPowerState));
1031 pw.println(" mLocks.gather=" + dumpPowerState(mLocks.gatherState()));
1032 pw.println(" mNextTimeout=" + mNextTimeout + " now=" + now
1033 + " " + ((mNextTimeout-now)/1000) + "s from now");
1034 pw.println(" mDimScreen=" + mDimScreen
1035 + " mStayOnConditions=" + mStayOnConditions);
1036 pw.println(" mScreenOffReason=" + mScreenOffReason
1037 + " mUserState=" + mUserState);
1038 pw.println(" mBroadcastQueue={" + mBroadcastQueue[0] + ',' + mBroadcastQueue[1]
1039 + ',' + mBroadcastQueue[2] + "}");
1040 pw.println(" mBroadcastWhy={" + mBroadcastWhy[0] + ',' + mBroadcastWhy[1]
1041 + ',' + mBroadcastWhy[2] + "}");
1042 pw.println(" mPokey=" + mPokey + " mPokeAwakeonSet=" + mPokeAwakeOnSet);
1043 pw.println(" mKeyboardVisible=" + mKeyboardVisible
1044 + " mUserActivityAllowed=" + mUserActivityAllowed);
1045 pw.println(" mKeylightDelay=" + mKeylightDelay + " mDimDelay=" + mDimDelay
1046 + " mScreenOffDelay=" + mScreenOffDelay);
1047 pw.println(" mPreventScreenOn=" + mPreventScreenOn
1048 + " mScreenBrightnessOverride=" + mScreenBrightnessOverride
1049 + " mButtonBrightnessOverride=" + mButtonBrightnessOverride);
1050 pw.println(" mScreenOffTimeoutSetting=" + mScreenOffTimeoutSetting
1051 + " mMaximumScreenOffTimeout=" + mMaximumScreenOffTimeout);
1052 pw.println(" mLastScreenOnTime=" + mLastScreenOnTime);
1053 pw.println(" mBroadcastWakeLock=" + mBroadcastWakeLock);
1054 pw.println(" mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
1055 pw.println(" mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
1056 pw.println(" mPreventScreenOnPartialLock=" + mPreventScreenOnPartialLock);
1057 pw.println(" mProximityPartialLock=" + mProximityPartialLock);
1058 pw.println(" mProximityWakeLockCount=" + mProximityWakeLockCount);
1059 pw.println(" mProximitySensorEnabled=" + mProximitySensorEnabled);
1060 pw.println(" mProximitySensorActive=" + mProximitySensorActive);
1061 pw.println(" mProximityPendingValue=" + mProximityPendingValue);
1062 pw.println(" mLastProximityEventTime=" + mLastProximityEventTime);
1063 pw.println(" mLightSensorEnabled=" + mLightSensorEnabled);
1064 pw.println(" mLightSensorValue=" + mLightSensorValue
1065 + " mLightSensorPendingValue=" + mLightSensorPendingValue);
1066 pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
1067 + " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
1068 + " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
1069 pw.println(" mUseSoftwareAutoBrightness=" + mUseSoftwareAutoBrightness);
1070 pw.println(" mAutoBrightessEnabled=" + mAutoBrightessEnabled);
1071 mScreenBrightness.dump(pw, " mScreenBrightness: ");
1072 mKeyboardBrightness.dump(pw, " mKeyboardBrightness: ");
1073 mButtonBrightness.dump(pw, " mButtonBrightness: ");
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001074
Mike Lockwoodca44df82010-02-25 13:48:49 -05001075 int N = mLocks.size();
1076 pw.println();
1077 pw.println("mLocks.size=" + N + ":");
1078 for (int i=0; i<N; i++) {
1079 WakeLock wl = mLocks.get(i);
1080 String type = lockType(wl.flags & LOCK_MASK);
1081 String acquireCausesWakeup = "";
1082 if ((wl.flags & PowerManager.ACQUIRE_CAUSES_WAKEUP) != 0) {
1083 acquireCausesWakeup = "ACQUIRE_CAUSES_WAKEUP ";
1084 }
1085 String activated = "";
1086 if (wl.activated) {
1087 activated = " activated";
1088 }
1089 pw.println(" " + type + " '" + wl.tag + "'" + acquireCausesWakeup
Mike Lockwoodf5bd0922010-03-22 17:10:15 -04001090 + activated + " (minState=" + wl.minState + ", uid=" + wl.uid
1091 + ", pid=" + wl.pid + ")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
Mike Lockwoodca44df82010-02-25 13:48:49 -05001093
1094 pw.println();
1095 pw.println("mPokeLocks.size=" + mPokeLocks.size() + ":");
1096 for (PokeLock p: mPokeLocks.values()) {
1097 pw.println(" poke lock '" + p.tag + "':"
1098 + ((p.pokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0
1099 ? " POKE_LOCK_IGNORE_CHEEK_EVENTS" : "")
1100 + ((p.pokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0
1101 ? " POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS" : "")
1102 + ((p.pokey & POKE_LOCK_SHORT_TIMEOUT) != 0
1103 ? " POKE_LOCK_SHORT_TIMEOUT" : "")
1104 + ((p.pokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0
1105 ? " POKE_LOCK_MEDIUM_TIMEOUT" : ""));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001107
Mike Lockwoodca44df82010-02-25 13:48:49 -05001108 pw.println();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
1111
Joe Onorato7999bff2010-07-24 11:50:05 -04001112 private void setTimeoutLocked(long now, int nextState) {
1113 setTimeoutLocked(now, -1, nextState);
1114 }
1115
1116 // If they gave a timeoutOverride it is the number of seconds
1117 // to screen-off. Figure out where in the countdown cycle we
1118 // should jump to.
Joe Onorato797e6882010-08-26 14:46:01 -04001119 private void setTimeoutLocked(long now, final long originalTimeoutOverride, int nextState) {
1120 long timeoutOverride = originalTimeoutOverride;
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001121 if (mBootCompleted) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001122 synchronized (mLocks) {
Joe Onorato7999bff2010-07-24 11:50:05 -04001123 long when = 0;
1124 if (timeoutOverride <= 0) {
1125 switch (nextState)
1126 {
1127 case SCREEN_BRIGHT:
1128 when = now + mKeylightDelay;
1129 break;
1130 case SCREEN_DIM:
1131 if (mDimDelay >= 0) {
1132 when = now + mDimDelay;
Andreas Huber84047bc2010-07-27 16:49:10 -07001133 break;
Joe Onorato7999bff2010-07-24 11:50:05 -04001134 } else {
1135 Slog.w(TAG, "mDimDelay=" + mDimDelay + " while trying to dim");
1136 }
1137 case SCREEN_OFF:
1138 synchronized (mLocks) {
1139 when = now + mScreenOffDelay;
1140 }
1141 break;
Andreas Huber84047bc2010-07-27 16:49:10 -07001142 default:
1143 when = now;
1144 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001146 } else {
1147 override: {
1148 if (timeoutOverride <= mScreenOffDelay) {
1149 when = now + timeoutOverride;
1150 nextState = SCREEN_OFF;
1151 break override;
1152 }
1153 timeoutOverride -= mScreenOffDelay;
1154
1155 if (mDimDelay >= 0) {
1156 if (timeoutOverride <= mDimDelay) {
1157 when = now + timeoutOverride;
1158 nextState = SCREEN_DIM;
1159 break override;
1160 }
1161 timeoutOverride -= mDimDelay;
1162 }
1163
1164 when = now + timeoutOverride;
1165 nextState = SCREEN_BRIGHT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
Joe Onorato7999bff2010-07-24 11:50:05 -04001167 }
1168 if (mSpew) {
1169 Slog.d(TAG, "setTimeoutLocked now=" + now
1170 + " timeoutOverride=" + timeoutOverride
1171 + " nextState=" + nextState + " when=" + when);
1172 }
Joe Onorato797e6882010-08-26 14:46:01 -04001173
1174 mHandler.removeCallbacks(mTimeoutTask);
1175 mTimeoutTask.nextState = nextState;
1176 mTimeoutTask.remainingTimeoutOverride = timeoutOverride > 0
1177 ? (originalTimeoutOverride - timeoutOverride)
1178 : -1;
Joe Onorato7999bff2010-07-24 11:50:05 -04001179 mHandler.postAtTime(mTimeoutTask, when);
1180 mNextTimeout = when; // for debugging
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 }
1184
1185 private void cancelTimerLocked()
1186 {
1187 mHandler.removeCallbacks(mTimeoutTask);
1188 mTimeoutTask.nextState = -1;
1189 }
1190
1191 private class TimeoutTask implements Runnable
1192 {
1193 int nextState; // access should be synchronized on mLocks
Joe Onorato797e6882010-08-26 14:46:01 -04001194 long remainingTimeoutOverride;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 public void run()
1196 {
1197 synchronized (mLocks) {
1198 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001199 Slog.d(TAG, "user activity timeout timed out nextState=" + this.nextState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 }
1201
1202 if (nextState == -1) {
1203 return;
1204 }
1205
1206 mUserState = this.nextState;
1207 setPowerState(this.nextState | mWakeLockState);
1208
1209 long now = SystemClock.uptimeMillis();
1210
1211 switch (this.nextState)
1212 {
1213 case SCREEN_BRIGHT:
1214 if (mDimDelay >= 0) {
Joe Onorato797e6882010-08-26 14:46:01 -04001215 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_DIM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 break;
1217 }
1218 case SCREEN_DIM:
Joe Onorato797e6882010-08-26 14:46:01 -04001219 setTimeoutLocked(now, remainingTimeoutOverride, SCREEN_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 break;
1221 }
1222 }
1223 }
1224 }
1225
1226 private void sendNotificationLocked(boolean on, int why)
1227 {
Joe Onorato64c62ba2009-03-24 20:13:57 -07001228 if (!on) {
1229 mStillNeedSleepNotification = false;
1230 }
1231
Joe Onorato128e7292009-03-24 18:41:31 -07001232 // Add to the queue.
1233 int index = 0;
1234 while (mBroadcastQueue[index] != -1) {
1235 index++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
Joe Onorato128e7292009-03-24 18:41:31 -07001237 mBroadcastQueue[index] = on ? 1 : 0;
1238 mBroadcastWhy[index] = why;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239
Joe Onorato128e7292009-03-24 18:41:31 -07001240 // If we added it position 2, then there is a pair that can be stripped.
1241 // If we added it position 1 and we're turning the screen off, we can strip
1242 // the pair and do nothing, because the screen is already off, and therefore
1243 // keyguard has already been enabled.
1244 // However, if we added it at position 1 and we're turning it on, then position
1245 // 0 was to turn it off, and we can't strip that, because keyguard needs to come
1246 // on, so have to run the queue then.
1247 if (index == 2) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08001248 // While we're collapsing them, if it's going off, and the new reason
1249 // is more significant than the first, then use the new one.
1250 if (!on && mBroadcastWhy[0] > why) {
1251 mBroadcastWhy[0] = why;
Joe Onorato128e7292009-03-24 18:41:31 -07001252 }
1253 mBroadcastQueue[0] = on ? 1 : 0;
1254 mBroadcastQueue[1] = -1;
1255 mBroadcastQueue[2] = -1;
Mike Lockwood9c90a372010-04-13 15:40:27 -04001256 mBroadcastWakeLock.release();
1257 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001258 index = 0;
1259 }
1260 if (index == 1 && !on) {
1261 mBroadcastQueue[0] = -1;
1262 mBroadcastQueue[1] = -1;
1263 index = -1;
1264 // The wake lock was being held, but we're not actually going to do any
1265 // broadcasts, so release the wake lock.
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001266 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 1, mBroadcastWakeLock.mCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 mBroadcastWakeLock.release();
Joe Onorato128e7292009-03-24 18:41:31 -07001268 }
1269
1270 // Now send the message.
1271 if (index >= 0) {
1272 // Acquire the broadcast wake lock before changing the power
1273 // state. It will be release after the broadcast is sent.
1274 // We always increment the ref count for each notification in the queue
1275 // and always decrement when that notification is handled.
1276 mBroadcastWakeLock.acquire();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001277 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_SEND, mBroadcastWakeLock.mCount);
Joe Onorato128e7292009-03-24 18:41:31 -07001278 mHandler.post(mNotificationTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280 }
1281
1282 private Runnable mNotificationTask = new Runnable()
1283 {
1284 public void run()
1285 {
Joe Onorato128e7292009-03-24 18:41:31 -07001286 while (true) {
1287 int value;
1288 int why;
1289 WindowManagerPolicy policy;
1290 synchronized (mLocks) {
1291 value = mBroadcastQueue[0];
1292 why = mBroadcastWhy[0];
1293 for (int i=0; i<2; i++) {
1294 mBroadcastQueue[i] = mBroadcastQueue[i+1];
1295 mBroadcastWhy[i] = mBroadcastWhy[i+1];
1296 }
1297 policy = getPolicyLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
Joe Onorato128e7292009-03-24 18:41:31 -07001299 if (value == 1) {
1300 mScreenOnStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001301
Joe Onorato128e7292009-03-24 18:41:31 -07001302 policy.screenTurnedOn();
1303 try {
1304 ActivityManagerNative.getDefault().wakingUp();
1305 } catch (RemoteException e) {
1306 // ignore it
1307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308
Joe Onorato128e7292009-03-24 18:41:31 -07001309 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001310 Slog.d(TAG, "mBroadcastWakeLock=" + mBroadcastWakeLock);
Joe Onorato128e7292009-03-24 18:41:31 -07001311 }
1312 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1313 mContext.sendOrderedBroadcast(mScreenOnIntent, null,
1314 mScreenOnBroadcastDone, mHandler, 0, null, null);
1315 } else {
1316 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001317 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 2,
Joe Onorato128e7292009-03-24 18:41:31 -07001318 mBroadcastWakeLock.mCount);
1319 mBroadcastWakeLock.release();
1320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322 }
Joe Onorato128e7292009-03-24 18:41:31 -07001323 else if (value == 0) {
1324 mScreenOffStart = SystemClock.uptimeMillis();
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001325
Joe Onorato128e7292009-03-24 18:41:31 -07001326 policy.screenTurnedOff(why);
1327 try {
1328 ActivityManagerNative.getDefault().goingToSleep();
1329 } catch (RemoteException e) {
1330 // ignore it.
1331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332
Joe Onorato128e7292009-03-24 18:41:31 -07001333 if (mContext != null && ActivityManagerNative.isSystemReady()) {
1334 mContext.sendOrderedBroadcast(mScreenOffIntent, null,
1335 mScreenOffBroadcastDone, mHandler, 0, null, null);
1336 } else {
1337 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001338 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_STOP, 3,
Joe Onorato128e7292009-03-24 18:41:31 -07001339 mBroadcastWakeLock.mCount);
1340 mBroadcastWakeLock.release();
1341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 }
1343 }
Joe Onorato128e7292009-03-24 18:41:31 -07001344 else {
1345 // If we're in this case, then this handler is running for a previous
1346 // paired transaction. mBroadcastWakeLock will already have been released.
1347 break;
1348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 }
1350 }
1351 };
1352
1353 long mScreenOnStart;
1354 private BroadcastReceiver mScreenOnBroadcastDone = new BroadcastReceiver() {
1355 public void onReceive(Context context, Intent intent) {
1356 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001357 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 1,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 SystemClock.uptimeMillis() - mScreenOnStart, mBroadcastWakeLock.mCount);
1359 mBroadcastWakeLock.release();
1360 }
1361 }
1362 };
1363
1364 long mScreenOffStart;
1365 private BroadcastReceiver mScreenOffBroadcastDone = new BroadcastReceiver() {
1366 public void onReceive(Context context, Intent intent) {
1367 synchronized (mLocks) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001368 EventLog.writeEvent(EventLogTags.POWER_SCREEN_BROADCAST_DONE, 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 SystemClock.uptimeMillis() - mScreenOffStart, mBroadcastWakeLock.mCount);
1370 mBroadcastWakeLock.release();
1371 }
1372 }
1373 };
1374
1375 void logPointerUpEvent() {
1376 if (LOG_TOUCH_DOWNS) {
1377 mTotalTouchDownTime += SystemClock.elapsedRealtime() - mLastTouchDown;
1378 mLastTouchDown = 0;
1379 }
1380 }
1381
1382 void logPointerDownEvent() {
1383 if (LOG_TOUCH_DOWNS) {
1384 // If we are not already timing a down/up sequence
1385 if (mLastTouchDown == 0) {
1386 mLastTouchDown = SystemClock.elapsedRealtime();
1387 mTouchCycles++;
1388 }
1389 }
1390 }
1391
1392 /**
1393 * Prevents the screen from turning on even if it *should* turn on due
1394 * to a subsequent full wake lock being acquired.
1395 * <p>
1396 * This is a temporary hack that allows an activity to "cover up" any
1397 * display glitches that happen during the activity's startup
1398 * sequence. (Specifically, this API was added to work around a
1399 * cosmetic bug in the "incoming call" sequence, where the lock screen
1400 * would flicker briefly before the incoming call UI became visible.)
1401 * TODO: There ought to be a more elegant way of doing this,
1402 * probably by having the PowerManager and ActivityManager
1403 * work together to let apps specify that the screen on/off
1404 * state should be synchronized with the Activity lifecycle.
1405 * <p>
1406 * Note that calling preventScreenOn(true) will NOT turn the screen
1407 * off if it's currently on. (This API only affects *future*
1408 * acquisitions of full wake locks.)
1409 * But calling preventScreenOn(false) WILL turn the screen on if
1410 * it's currently off because of a prior preventScreenOn(true) call.
1411 * <p>
1412 * Any call to preventScreenOn(true) MUST be followed promptly by a call
1413 * to preventScreenOn(false). In fact, if the preventScreenOn(false)
1414 * call doesn't occur within 5 seconds, we'll turn the screen back on
1415 * ourselves (and log a warning about it); this prevents a buggy app
1416 * from disabling the screen forever.)
1417 * <p>
1418 * TODO: this feature should really be controlled by a new type of poke
1419 * lock (rather than an IPowerManager call).
1420 */
1421 public void preventScreenOn(boolean prevent) {
1422 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1423
1424 synchronized (mLocks) {
1425 if (prevent) {
1426 // First of all, grab a partial wake lock to
1427 // make sure the CPU stays on during the entire
1428 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1429 mPreventScreenOnPartialLock.acquire();
1430
1431 // Post a forceReenableScreen() call (for 5 seconds in the
1432 // future) to make sure the matching preventScreenOn(false) call
1433 // has happened by then.
1434 mHandler.removeCallbacks(mForceReenableScreenTask);
1435 mHandler.postDelayed(mForceReenableScreenTask, 5000);
1436
1437 // Finally, set the flag that prevents the screen from turning on.
1438 // (Below, in setPowerState(), we'll check mPreventScreenOn and
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001439 // we *won't* call setScreenStateLocked(true) if it's set.)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 mPreventScreenOn = true;
1441 } else {
1442 // (Re)enable the screen.
1443 mPreventScreenOn = false;
1444
1445 // We're "undoing" a the prior preventScreenOn(true) call, so we
1446 // no longer need the 5-second safeguard.
1447 mHandler.removeCallbacks(mForceReenableScreenTask);
1448
1449 // Forcibly turn on the screen if it's supposed to be on. (This
1450 // handles the case where the screen is currently off because of
1451 // a prior preventScreenOn(true) call.)
Mike Lockwoode090281422009-11-14 21:02:56 -05001452 if (!mProximitySensorActive && (mPowerState & SCREEN_ON_BIT) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001454 Slog.d(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 "preventScreenOn: turning on after a prior preventScreenOn(true)!");
1456 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001457 int err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 if (err != 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001459 Slog.w(TAG, "preventScreenOn: error from setScreenStateLocked(): " + err);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 }
1461 }
1462
1463 // Release the partial wake lock that we held during the
1464 // preventScreenOn(true) -> preventScreenOn(false) sequence.
1465 mPreventScreenOnPartialLock.release();
1466 }
1467 }
1468 }
1469
1470 public void setScreenBrightnessOverride(int brightness) {
1471 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1472
Mike Lockwoodf527c712010-06-10 14:12:33 -04001473 if (mSpew) Slog.d(TAG, "setScreenBrightnessOverride " + brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 synchronized (mLocks) {
1475 if (mScreenBrightnessOverride != brightness) {
1476 mScreenBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001477 if (isScreenOn()) {
1478 updateLightsLocked(mPowerState, SCREEN_ON_BIT);
1479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 }
1481 }
1482 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001483
1484 public void setButtonBrightnessOverride(int brightness) {
1485 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
1486
Mike Lockwoodf527c712010-06-10 14:12:33 -04001487 if (mSpew) Slog.d(TAG, "setButtonBrightnessOverride " + brightness);
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001488 synchronized (mLocks) {
1489 if (mButtonBrightnessOverride != brightness) {
1490 mButtonBrightnessOverride = brightness;
Mike Lockwoodf527c712010-06-10 14:12:33 -04001491 if (isScreenOn()) {
1492 updateLightsLocked(mPowerState, BUTTON_BRIGHT_BIT | KEYBOARD_BRIGHT_BIT);
1493 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001494 }
1495 }
1496 }
1497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 /**
1499 * Sanity-check that gets called 5 seconds after any call to
1500 * preventScreenOn(true). This ensures that the original call
1501 * is followed promptly by a call to preventScreenOn(false).
1502 */
1503 private void forceReenableScreen() {
1504 // We shouldn't get here at all if mPreventScreenOn is false, since
1505 // we should have already removed any existing
1506 // mForceReenableScreenTask messages...
1507 if (!mPreventScreenOn) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001508 Slog.w(TAG, "forceReenableScreen: mPreventScreenOn is false, nothing to do");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 return;
1510 }
1511
1512 // Uh oh. It's been 5 seconds since a call to
1513 // preventScreenOn(true) and we haven't re-enabled the screen yet.
1514 // This means the app that called preventScreenOn(true) is either
1515 // slow (i.e. it took more than 5 seconds to call preventScreenOn(false)),
1516 // or buggy (i.e. it forgot to call preventScreenOn(false), or
1517 // crashed before doing so.)
1518
1519 // Log a warning, and forcibly turn the screen back on.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001520 Slog.w(TAG, "App called preventScreenOn(true) but didn't promptly reenable the screen! "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 + "Forcing the screen back on...");
1522 preventScreenOn(false);
1523 }
1524
1525 private Runnable mForceReenableScreenTask = new Runnable() {
1526 public void run() {
1527 forceReenableScreen();
1528 }
1529 };
1530
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001531 private int setScreenStateLocked(boolean on) {
1532 int err = Power.setScreenState(on);
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001533 if (err == 0) {
1534 mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
1535 if (mUseSoftwareAutoBrightness) {
1536 enableLightSensor(on);
1537 if (!on) {
1538 // make sure button and key backlights are off too
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001539 mButtonLight.turnOff();
1540 mKeyboardLight.turnOff();
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001541 // clear current value so we will update based on the new conditions
1542 // when the sensor is reenabled.
1543 mLightSensorValue = -1;
Mike Lockwoodb2865412010-02-02 22:40:33 -05001544 // reset our highest light sensor value when the screen turns off
1545 mHighestLightSensorValue = -1;
Mike Lockwood20ee6f22009-11-07 20:33:47 -05001546 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07001547 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001548 }
1549 return err;
1550 }
1551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 private void setPowerState(int state)
1553 {
Mike Lockwood435eb642009-12-03 08:40:18 -05001554 setPowerState(state, false, WindowManagerPolicy.OFF_BECAUSE_OF_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
1556
Mike Lockwood435eb642009-12-03 08:40:18 -05001557 private void setPowerState(int newState, boolean noChangeLights, int reason)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 {
1559 synchronized (mLocks) {
1560 int err;
1561
1562 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001563 Slog.d(TAG, "setPowerState: mPowerState=0x" + Integer.toHexString(mPowerState)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 + " newState=0x" + Integer.toHexString(newState)
Mike Lockwood435eb642009-12-03 08:40:18 -05001565 + " noChangeLights=" + noChangeLights
1566 + " reason=" + reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 }
1568
1569 if (noChangeLights) {
1570 newState = (newState & ~LIGHTS_MASK) | (mPowerState & LIGHTS_MASK);
1571 }
Mike Lockwood36fc3022009-08-25 16:49:06 -07001572 if (mProximitySensorActive) {
1573 // don't turn on the screen when the proximity sensor lock is held
1574 newState = (newState & ~SCREEN_BRIGHT);
1575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576
1577 if (batteryIsLow()) {
1578 newState |= BATTERY_LOW_BIT;
1579 } else {
1580 newState &= ~BATTERY_LOW_BIT;
1581 }
1582 if (newState == mPowerState) {
1583 return;
1584 }
Mike Lockwood3333fa42009-10-26 14:50:42 -04001585
Mike Lockwood2d7bb812009-11-15 18:12:22 -05001586 if (!mBootCompleted && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 newState |= ALL_BRIGHT;
1588 }
1589
1590 boolean oldScreenOn = (mPowerState & SCREEN_ON_BIT) != 0;
1591 boolean newScreenOn = (newState & SCREEN_ON_BIT) != 0;
1592
Mike Lockwood51b84492009-11-16 21:51:18 -05001593 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001594 Slog.d(TAG, "setPowerState: mPowerState=" + mPowerState
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 + " newState=" + newState + " noChangeLights=" + noChangeLights);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001596 Slog.d(TAG, " oldKeyboardBright=" + ((mPowerState & KEYBOARD_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 + " newKeyboardBright=" + ((newState & KEYBOARD_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001598 Slog.d(TAG, " oldScreenBright=" + ((mPowerState & SCREEN_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 + " newScreenBright=" + ((newState & SCREEN_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001600 Slog.d(TAG, " oldButtonBright=" + ((mPowerState & BUTTON_BRIGHT_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 + " newButtonBright=" + ((newState & BUTTON_BRIGHT_BIT) != 0));
Joe Onorato8a9b2202010-02-26 18:56:32 -08001602 Slog.d(TAG, " oldScreenOn=" + oldScreenOn
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 + " newScreenOn=" + newScreenOn);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001604 Slog.d(TAG, " oldBatteryLow=" + ((mPowerState & BATTERY_LOW_BIT) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 + " newBatteryLow=" + ((newState & BATTERY_LOW_BIT) != 0));
1606 }
1607
1608 if (mPowerState != newState) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001609 updateLightsLocked(newState, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 mPowerState = (mPowerState & ~LIGHTS_MASK) | (newState & LIGHTS_MASK);
1611 }
1612
1613 if (oldScreenOn != newScreenOn) {
1614 if (newScreenOn) {
Joe Onorato128e7292009-03-24 18:41:31 -07001615 // When the user presses the power button, we need to always send out the
1616 // notification that it's going to sleep so the keyguard goes on. But
1617 // we can't do that until the screen fades out, so we don't show the keyguard
1618 // too early.
1619 if (mStillNeedSleepNotification) {
1620 sendNotificationLocked(false, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
1621 }
1622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 // Turn on the screen UNLESS there was a prior
1624 // preventScreenOn(true) request. (Note that the lifetime
1625 // of a single preventScreenOn() request is limited to 5
1626 // seconds to prevent a buggy app from disabling the
1627 // screen forever; see forceReenableScreen().)
1628 boolean reallyTurnScreenOn = true;
1629 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001630 Slog.d(TAG, "- turning screen on... mPreventScreenOn = "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 + mPreventScreenOn);
1632 }
1633
1634 if (mPreventScreenOn) {
1635 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001636 Slog.d(TAG, "- PREVENTING screen from really turning on!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 }
1638 reallyTurnScreenOn = false;
1639 }
1640 if (reallyTurnScreenOn) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001641 err = setScreenStateLocked(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 long identity = Binder.clearCallingIdentity();
1643 try {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001644 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 mBatteryStats.noteScreenOn();
1646 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001647 Slog.w(TAG, "RemoteException calling noteScreenOn on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 } finally {
1649 Binder.restoreCallingIdentity(identity);
1650 }
1651 } else {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001652 setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 // But continue as if we really did turn the screen on...
1654 err = 0;
1655 }
1656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 mLastTouchDown = 0;
1658 mTotalTouchDownTime = 0;
1659 mTouchCycles = 0;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001660 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 1, reason,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 mTotalTouchDownTime, mTouchCycles);
1662 if (err == 0) {
1663 mPowerState |= SCREEN_ON_BIT;
1664 sendNotificationLocked(true, -1);
1665 }
1666 } else {
Mike Lockwood497087e32009-11-08 18:33:03 -05001667 // cancel light sensor task
1668 mHandler.removeCallbacks(mAutoBrightnessTask);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 mScreenOffTime = SystemClock.elapsedRealtime();
1670 long identity = Binder.clearCallingIdentity();
1671 try {
1672 mBatteryStats.noteScreenOff();
1673 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001674 Slog.w(TAG, "RemoteException calling noteScreenOff on BatteryStatsService", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 } finally {
1676 Binder.restoreCallingIdentity(identity);
1677 }
1678 mPowerState &= ~SCREEN_ON_BIT;
Mike Lockwood435eb642009-12-03 08:40:18 -05001679 mScreenOffReason = reason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 if (!mScreenBrightness.animating) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001681 err = screenOffFinishedAnimatingLocked(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 err = 0;
1684 mLastTouchDown = 0;
1685 }
1686 }
1687 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001688
1689 updateNativePowerStateLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
1691 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001692
1693 private void updateNativePowerStateLocked() {
1694 nativeSetPowerState(
1695 (mPowerState & SCREEN_ON_BIT) != 0,
1696 (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT);
1697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001698
Mike Lockwood435eb642009-12-03 08:40:18 -05001699 private int screenOffFinishedAnimatingLocked(int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 // I don't think we need to check the current state here because all of these
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001701 // Power.setScreenState and sendNotificationLocked can both handle being
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 // called multiple times in the same state. -joeo
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001703 EventLog.writeEvent(EventLogTags.POWER_SCREEN_STATE, 0, reason, mTotalTouchDownTime, mTouchCycles);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 mLastTouchDown = 0;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04001705 int err = setScreenStateLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 if (err == 0) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001707 mScreenOffReason = reason;
1708 sendNotificationLocked(false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 }
1710 return err;
1711 }
1712
1713 private boolean batteryIsLow() {
1714 return (!mIsPowered &&
1715 mBatteryService.getBatteryLevel() <= Power.LOW_BATTERY_THRESHOLD);
1716 }
1717
The Android Open Source Project10592532009-03-18 17:39:46 -07001718 private void updateLightsLocked(int newState, int forceState) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001719 final int oldState = mPowerState;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001720 newState = applyButtonState(newState);
1721 newState = applyKeyboardState(newState);
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001722 final int realDifference = (newState ^ oldState);
1723 final int difference = realDifference | forceState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 if (difference == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001725 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 int offMask = 0;
1729 int dimMask = 0;
1730 int onMask = 0;
1731
1732 int preferredBrightness = getPreferredBrightness();
1733 boolean startAnimation = false;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 if ((difference & KEYBOARD_BRIGHT_BIT) != 0) {
1736 if (ANIMATE_KEYBOARD_LIGHTS) {
1737 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
1738 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001739 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001740 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001742 mKeyboardBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001743 ANIM_STEPS, INITIAL_KEYBOARD_BRIGHTNESS,
1744 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 }
1746 startAnimation = true;
1747 } else {
1748 if ((newState & KEYBOARD_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001749 offMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001751 onMask |= KEYBOARD_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 }
1753 }
1754 }
1755
1756 if ((difference & BUTTON_BRIGHT_BIT) != 0) {
1757 if (ANIMATE_BUTTON_LIGHTS) {
1758 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
1759 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_OFF,
Joe Onorato128e7292009-03-24 18:41:31 -07001760 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001761 Power.BRIGHTNESS_ON);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 } else {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05001763 mButtonBrightness.setTargetLocked(Power.BRIGHTNESS_ON,
Joe Onorato128e7292009-03-24 18:41:31 -07001764 ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
1765 Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 }
1767 startAnimation = true;
1768 } else {
1769 if ((newState & BUTTON_BRIGHT_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001770 offMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001772 onMask |= BUTTON_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774 }
1775 }
1776
1777 if ((difference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1778 if (ANIMATE_SCREEN_LIGHTS) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001779 int nominalCurrentValue = -1;
1780 // If there was an actual difference in the light state, then
1781 // figure out the "ideal" current value based on the previous
1782 // state. Otherwise, this is a change due to the brightness
1783 // override, so we want to animate from whatever the current
1784 // value is.
1785 if ((realDifference & (SCREEN_ON_BIT | SCREEN_BRIGHT_BIT)) != 0) {
1786 switch (oldState & (SCREEN_BRIGHT_BIT|SCREEN_ON_BIT)) {
1787 case SCREEN_BRIGHT_BIT | SCREEN_ON_BIT:
1788 nominalCurrentValue = preferredBrightness;
1789 break;
1790 case SCREEN_ON_BIT:
1791 nominalCurrentValue = Power.BRIGHTNESS_DIM;
1792 break;
1793 case 0:
1794 nominalCurrentValue = Power.BRIGHTNESS_OFF;
1795 break;
1796 case SCREEN_BRIGHT_BIT:
1797 default:
1798 // not possible
1799 nominalCurrentValue = (int)mScreenBrightness.curValue;
1800 break;
1801 }
Joe Onorato128e7292009-03-24 18:41:31 -07001802 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001803 int brightness = preferredBrightness;
1804 int steps = ANIM_STEPS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1806 // dim or turn off backlight, depending on if the screen is on
1807 // the scale is because the brightness ramp isn't linear and this biases
1808 // it so the later parts take longer.
1809 final float scale = 1.5f;
1810 float ratio = (((float)Power.BRIGHTNESS_DIM)/preferredBrightness);
1811 if (ratio > 1.0f) ratio = 1.0f;
1812 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 if ((oldState & SCREEN_BRIGHT_BIT) != 0) {
1814 // was bright
1815 steps = ANIM_STEPS;
1816 } else {
1817 // was dim
1818 steps = (int)(ANIM_STEPS*ratio*scale);
1819 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001820 brightness = Power.BRIGHTNESS_OFF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 if ((oldState & SCREEN_ON_BIT) != 0) {
1823 // was bright
1824 steps = (int)(ANIM_STEPS*(1.0f-ratio)*scale);
1825 } else {
1826 // was dim
1827 steps = (int)(ANIM_STEPS*ratio);
1828 }
1829 if (mStayOnConditions != 0 && mBatteryService.isPowered(mStayOnConditions)) {
1830 // If the "stay on while plugged in" option is
1831 // turned on, then the screen will often not
1832 // automatically turn off while plugged in. To
1833 // still have a sense of when it is inactive, we
1834 // will then count going dim as turning off.
1835 mScreenOffTime = SystemClock.elapsedRealtime();
1836 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001837 brightness = Power.BRIGHTNESS_DIM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001840 long identity = Binder.clearCallingIdentity();
1841 try {
1842 mBatteryStats.noteScreenBrightness(brightness);
1843 } catch (RemoteException e) {
1844 // Nothing interesting to do.
1845 } finally {
1846 Binder.restoreCallingIdentity(identity);
1847 }
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001848 if (mScreenBrightness.setTargetLocked(brightness,
1849 steps, INITIAL_SCREEN_BRIGHTNESS, nominalCurrentValue)) {
1850 startAnimation = true;
1851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 } else {
1853 if ((newState & SCREEN_BRIGHT_BIT) == 0) {
1854 // dim or turn off backlight, depending on if the screen is on
1855 if ((newState & SCREEN_ON_BIT) == 0) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001856 offMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001858 dimMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 }
1860 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -07001861 onMask |= SCREEN_BRIGHT_BIT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
1863 }
1864 }
1865
1866 if (startAnimation) {
1867 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001868 Slog.i(TAG, "Scheduling light animator!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
1870 mHandler.removeCallbacks(mLightAnimator);
1871 mHandler.post(mLightAnimator);
1872 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 if (offMask != 0) {
Mike Lockwood48358bd2010-04-17 22:29:20 -04001875 if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001876 setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 }
1878 if (dimMask != 0) {
1879 int brightness = Power.BRIGHTNESS_DIM;
1880 if ((newState & BATTERY_LOW_BIT) != 0 &&
1881 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1882 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1883 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04001884 if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001885 setLightBrightness(dimMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
1887 if (onMask != 0) {
1888 int brightness = getPreferredBrightness();
1889 if ((newState & BATTERY_LOW_BIT) != 0 &&
1890 brightness > Power.BRIGHTNESS_LOW_BATTERY) {
1891 brightness = Power.BRIGHTNESS_LOW_BATTERY;
1892 }
Mike Lockwood48358bd2010-04-17 22:29:20 -04001893 if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001894 setLightBrightness(onMask, brightness);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897
The Android Open Source Project10592532009-03-18 17:39:46 -07001898 private void setLightBrightness(int mask, int value) {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05001899 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05001900 ? LightsService.BRIGHTNESS_MODE_SENSOR
1901 : LightsService.BRIGHTNESS_MODE_USER);
The Android Open Source Project10592532009-03-18 17:39:46 -07001902 if ((mask & SCREEN_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001903 mLcdLight.setBrightness(value, brightnessMode);
The Android Open Source Project10592532009-03-18 17:39:46 -07001904 }
1905 if ((mask & BUTTON_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001906 mButtonLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001907 }
1908 if ((mask & KEYBOARD_BRIGHT_BIT) != 0) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001909 mKeyboardLight.setBrightness(value);
The Android Open Source Project10592532009-03-18 17:39:46 -07001910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
1912
1913 class BrightnessState {
1914 final int mask;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 boolean initialized;
1917 int targetValue;
1918 float curValue;
1919 float delta;
1920 boolean animating;
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 BrightnessState(int m) {
1923 mask = m;
1924 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 public void dump(PrintWriter pw, String prefix) {
1927 pw.println(prefix + "animating=" + animating
1928 + " targetValue=" + targetValue
1929 + " curValue=" + curValue
1930 + " delta=" + delta);
1931 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001932
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001933 boolean setTargetLocked(int target, int stepsToTarget, int initialValue,
Joe Onorato128e7292009-03-24 18:41:31 -07001934 int nominalCurrentValue) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 if (!initialized) {
1936 initialized = true;
1937 curValue = (float)initialValue;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001938 } else if (targetValue == target) {
1939 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 }
1941 targetValue = target;
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001942 delta = (targetValue -
1943 (nominalCurrentValue >= 0 ? nominalCurrentValue : curValue))
1944 / stepsToTarget;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (mSpew) {
Joe Onorato128e7292009-03-24 18:41:31 -07001946 String noticeMe = nominalCurrentValue == curValue ? "" : " ******************";
Joe Onorato8a9b2202010-02-26 18:56:32 -08001947 Slog.i(TAG, "Setting target " + mask + ": cur=" + curValue
Joe Onorato128e7292009-03-24 18:41:31 -07001948 + " target=" + targetValue + " delta=" + delta
1949 + " nominalCurrentValue=" + nominalCurrentValue
1950 + noticeMe);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952 animating = true;
Dianne Hackbornaa80b602009-10-09 17:38:26 -07001953 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 boolean stepLocked() {
1957 if (!animating) return false;
1958 if (false && mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001959 Slog.i(TAG, "Step target " + mask + ": cur=" + curValue
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 + " target=" + targetValue + " delta=" + delta);
1961 }
1962 curValue += delta;
1963 int curIntValue = (int)curValue;
1964 boolean more = true;
1965 if (delta == 0) {
Dianne Hackborn9ed4a4b2009-03-25 17:10:37 -07001966 curValue = curIntValue = targetValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 more = false;
1968 } else if (delta > 0) {
1969 if (curIntValue >= targetValue) {
1970 curValue = curIntValue = targetValue;
1971 more = false;
1972 }
1973 } else {
1974 if (curIntValue <= targetValue) {
1975 curValue = curIntValue = targetValue;
1976 more = false;
1977 }
1978 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001979 //Slog.i(TAG, "Animating brightess " + curIntValue + ": " + mask);
The Android Open Source Project10592532009-03-18 17:39:46 -07001980 setLightBrightness(mask, curIntValue);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 animating = more;
1982 if (!more) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001983 if (mask == SCREEN_BRIGHT_BIT && curIntValue == Power.BRIGHTNESS_OFF) {
Mike Lockwood435eb642009-12-03 08:40:18 -05001984 screenOffFinishedAnimatingLocked(mScreenOffReason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986 }
1987 return more;
1988 }
1989 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 private class LightAnimator implements Runnable {
1992 public void run() {
1993 synchronized (mLocks) {
1994 long now = SystemClock.uptimeMillis();
1995 boolean more = mScreenBrightness.stepLocked();
1996 if (mKeyboardBrightness.stepLocked()) {
1997 more = true;
1998 }
1999 if (mButtonBrightness.stepLocked()) {
2000 more = true;
2001 }
2002 if (more) {
2003 mHandler.postAtTime(mLightAnimator, now+(1000/60));
2004 }
2005 }
2006 }
2007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 private int getPreferredBrightness() {
2010 try {
2011 if (mScreenBrightnessOverride >= 0) {
2012 return mScreenBrightnessOverride;
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002013 } else if (mLightSensorScreenBrightness >= 0 && mUseSoftwareAutoBrightness
Mike Lockwood27c6dd72009-11-04 08:57:07 -05002014 && mAutoBrightessEnabled) {
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002015 return mLightSensorScreenBrightness;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 }
2017 final int brightness = Settings.System.getInt(mContext.getContentResolver(),
2018 SCREEN_BRIGHTNESS);
2019 // Don't let applications turn the screen all the way off
2020 return Math.max(brightness, Power.BRIGHTNESS_DIM);
2021 } catch (SettingNotFoundException snfe) {
2022 return Power.BRIGHTNESS_ON;
2023 }
2024 }
2025
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002026 private int applyButtonState(int state) {
2027 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002028 if ((state & BATTERY_LOW_BIT) != 0) {
2029 // do not override brightness if the battery is low
2030 return state;
2031 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002032 if (mButtonBrightnessOverride >= 0) {
2033 brightness = mButtonBrightnessOverride;
2034 } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
2035 brightness = mLightSensorButtonBrightness;
2036 }
2037 if (brightness > 0) {
2038 return state | BUTTON_BRIGHT_BIT;
2039 } else if (brightness == 0) {
2040 return state & ~BUTTON_BRIGHT_BIT;
2041 } else {
2042 return state;
2043 }
2044 }
2045
2046 private int applyKeyboardState(int state) {
2047 int brightness = -1;
Mike Lockwood48358bd2010-04-17 22:29:20 -04002048 if ((state & BATTERY_LOW_BIT) != 0) {
2049 // do not override brightness if the battery is low
2050 return state;
2051 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002052 if (!mKeyboardVisible) {
2053 brightness = 0;
2054 } else if (mButtonBrightnessOverride >= 0) {
2055 brightness = mButtonBrightnessOverride;
2056 } else if (mLightSensorKeyboardBrightness >= 0 && mUseSoftwareAutoBrightness) {
2057 brightness = mLightSensorKeyboardBrightness;
2058 }
2059 if (brightness > 0) {
2060 return state | KEYBOARD_BRIGHT_BIT;
2061 } else if (brightness == 0) {
2062 return state & ~KEYBOARD_BRIGHT_BIT;
2063 } else {
2064 return state;
2065 }
2066 }
2067
Charles Mendis322591c2009-10-29 11:06:59 -07002068 public boolean isScreenOn() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 synchronized (mLocks) {
2070 return (mPowerState & SCREEN_ON_BIT) != 0;
2071 }
2072 }
2073
Charles Mendis322591c2009-10-29 11:06:59 -07002074 boolean isScreenBright() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 synchronized (mLocks) {
2076 return (mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT;
2077 }
2078 }
2079
Mike Lockwood497087e32009-11-08 18:33:03 -05002080 private boolean isScreenTurningOffLocked() {
2081 return (mScreenBrightness.animating && mScreenBrightness.targetValue == 0);
2082 }
2083
Mike Lockwood200b30b2009-09-20 00:23:59 -04002084 private void forceUserActivityLocked() {
Mike Lockwoode090281422009-11-14 21:02:56 -05002085 if (isScreenTurningOffLocked()) {
2086 // cancel animation so userActivity will succeed
2087 mScreenBrightness.animating = false;
2088 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002089 boolean savedActivityAllowed = mUserActivityAllowed;
2090 mUserActivityAllowed = true;
2091 userActivity(SystemClock.uptimeMillis(), false);
2092 mUserActivityAllowed = savedActivityAllowed;
2093 }
2094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 public void userActivityWithForce(long time, boolean noChangeLights, boolean force) {
2096 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Joe Onorato7999bff2010-07-24 11:50:05 -04002097 userActivity(time, -1, noChangeLights, OTHER_EVENT, force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 }
2099
2100 public void userActivity(long time, boolean noChangeLights) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002101 userActivity(time, -1, noChangeLights, OTHER_EVENT, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 }
2103
2104 public void userActivity(long time, boolean noChangeLights, int eventType) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002105 userActivity(time, -1, noChangeLights, eventType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
2107
2108 public void userActivity(long time, boolean noChangeLights, int eventType, boolean force) {
Joe Onorato7999bff2010-07-24 11:50:05 -04002109 userActivity(time, -1, noChangeLights, eventType, force);
2110 }
2111
2112 /*
2113 * Reset the user activity timeout to now + timeout. This overrides whatever else is going
2114 * on with user activity. Don't use this function.
2115 */
2116 public void clearUserActivityTimeout(long now, long timeout) {
2117 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2118 Slog.i(TAG, "clearUserActivity for " + timeout + "ms from now");
2119 userActivity(now, timeout, false, OTHER_EVENT, false);
2120 }
2121
2122 private void userActivity(long time, long timeoutOverride, boolean noChangeLights,
2123 int eventType, boolean force) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 //mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2125
2126 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002127 && (eventType == CHEEK_EVENT || eventType == TOUCH_EVENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002129 Slog.d(TAG, "dropping cheek or short event mPokey=0x" + Integer.toHexString(mPokey));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 }
2131 return;
2132 }
2133
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002134 if (((mPokey & POKE_LOCK_IGNORE_TOUCH_AND_CHEEK_EVENTS) != 0)
2135 && (eventType == TOUCH_EVENT || eventType == TOUCH_UP_EVENT
2136 || eventType == LONG_TOUCH_EVENT || eventType == CHEEK_EVENT)) {
2137 if (false) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002138 Slog.d(TAG, "dropping touch mPokey=0x" + Integer.toHexString(mPokey));
Joe Onoratoe68ffcb2009-03-24 19:11:13 -07002139 }
2140 return;
2141 }
2142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 if (false) {
2144 if (((mPokey & POKE_LOCK_IGNORE_CHEEK_EVENTS) != 0)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002145 Slog.d(TAG, "userActivity !!!");//, new RuntimeException());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002147 Slog.d(TAG, "mPokey=0x" + Integer.toHexString(mPokey));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
2149 }
2150
2151 synchronized (mLocks) {
2152 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002153 Slog.d(TAG, "userActivity mLastEventTime=" + mLastEventTime + " time=" + time
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 + " mUserActivityAllowed=" + mUserActivityAllowed
2155 + " mUserState=0x" + Integer.toHexString(mUserState)
Mike Lockwood36fc3022009-08-25 16:49:06 -07002156 + " mWakeLockState=0x" + Integer.toHexString(mWakeLockState)
2157 + " mProximitySensorActive=" + mProximitySensorActive
Joe Onorato797e6882010-08-26 14:46:01 -04002158 + " timeoutOverride=" + timeoutOverride
Mike Lockwood36fc3022009-08-25 16:49:06 -07002159 + " force=" + force);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
Mike Lockwood05067122009-10-27 23:07:25 -04002161 // ignore user activity if we are in the process of turning off the screen
Mike Lockwood497087e32009-11-08 18:33:03 -05002162 if (isScreenTurningOffLocked()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002163 Slog.d(TAG, "ignoring user activity while turning off screen");
Mike Lockwood05067122009-10-27 23:07:25 -04002164 return;
2165 }
Mike Lockwood0e39ea82009-11-18 15:37:10 -05002166 // Disable proximity sensor if if user presses power key while we are in the
2167 // "waiting for proximity sensor to go negative" state.
2168 if (mProximitySensorActive && mProximityWakeLockCount == 0) {
2169 mProximitySensorActive = false;
2170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 if (mLastEventTime <= time || force) {
2172 mLastEventTime = time;
Mike Lockwood36fc3022009-08-25 16:49:06 -07002173 if ((mUserActivityAllowed && !mProximitySensorActive) || force) {
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002174 // Only turn on button backlights if a button was pressed
2175 // and auto brightness is disabled
Mike Lockwood4984e732009-11-01 08:16:33 -05002176 if (eventType == BUTTON_EVENT && !mUseSoftwareAutoBrightness) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 mUserState = (mKeyboardVisible ? ALL_BRIGHT : SCREEN_BUTTON_BRIGHT);
2178 } else {
2179 // don't clear button/keyboard backlights when the screen is touched.
2180 mUserState |= SCREEN_BRIGHT;
2181 }
2182
Dianne Hackborn617f8772009-03-31 15:04:46 -07002183 int uid = Binder.getCallingUid();
2184 long ident = Binder.clearCallingIdentity();
2185 try {
2186 mBatteryStats.noteUserActivity(uid, eventType);
2187 } catch (RemoteException e) {
2188 // Ignore
2189 } finally {
2190 Binder.restoreCallingIdentity(ident);
2191 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002192
Michael Chane96440f2009-05-06 10:27:36 -07002193 mWakeLockState = mLocks.reactivateScreenLocksLocked();
Mike Lockwood435eb642009-12-03 08:40:18 -05002194 setPowerState(mUserState | mWakeLockState, noChangeLights,
2195 WindowManagerPolicy.OFF_BECAUSE_OF_USER);
Joe Onorato7999bff2010-07-24 11:50:05 -04002196 setTimeoutLocked(time, timeoutOverride, SCREEN_BRIGHT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 }
2198 }
2199 }
Mike Lockwoodef731622010-01-27 17:51:34 -05002200
2201 if (mPolicy != null) {
2202 mPolicy.userActivity();
2203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
2205
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002206 private int getAutoBrightnessValue(int sensorValue, int[] values) {
2207 try {
2208 int i;
2209 for (i = 0; i < mAutoBrightnessLevels.length; i++) {
2210 if (sensorValue < mAutoBrightnessLevels[i]) {
2211 break;
2212 }
2213 }
2214 return values[i];
2215 } catch (Exception e) {
2216 // guard against null pointer or index out of bounds errors
Joe Onorato8a9b2202010-02-26 18:56:32 -08002217 Slog.e(TAG, "getAutoBrightnessValue", e);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002218 return 255;
2219 }
2220 }
2221
Mike Lockwood20f87d72009-11-05 16:08:51 -05002222 private Runnable mProximityTask = new Runnable() {
2223 public void run() {
2224 synchronized (mLocks) {
2225 if (mProximityPendingValue != -1) {
2226 proximityChangedLocked(mProximityPendingValue == 1);
2227 mProximityPendingValue = -1;
2228 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002229 if (mProximityPartialLock.isHeld()) {
2230 mProximityPartialLock.release();
2231 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002232 }
2233 }
2234 };
2235
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002236 private Runnable mAutoBrightnessTask = new Runnable() {
2237 public void run() {
Mike Lockwoodfa68ab42009-10-20 11:08:49 -04002238 synchronized (mLocks) {
2239 int value = (int)mLightSensorPendingValue;
2240 if (value >= 0) {
2241 mLightSensorPendingValue = -1;
2242 lightSensorChangedLocked(value);
2243 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002244 }
2245 }
2246 };
2247
Mike Lockwoodb2865412010-02-02 22:40:33 -05002248 private void dockStateChanged(int state) {
2249 synchronized (mLocks) {
2250 mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED);
2251 if (mIsDocked) {
2252 mHighestLightSensorValue = -1;
2253 }
2254 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2255 // force lights recalculation
2256 int value = (int)mLightSensorValue;
2257 mLightSensorValue = -1;
2258 lightSensorChangedLocked(value);
2259 }
2260 }
2261 }
2262
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002263 private void lightSensorChangedLocked(int value) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002264 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002265 Slog.d(TAG, "lightSensorChangedLocked " + value);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002266 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002267
Mike Lockwoodb2865412010-02-02 22:40:33 -05002268 // do not allow light sensor value to decrease
2269 if (mHighestLightSensorValue < value) {
2270 mHighestLightSensorValue = value;
2271 }
2272
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002273 if (mLightSensorValue != value) {
2274 mLightSensorValue = value;
2275 if ((mPowerState & BATTERY_LOW_BIT) == 0) {
Mike Lockwoodb2865412010-02-02 22:40:33 -05002276 // use maximum light sensor value seen since screen went on for LCD to avoid flicker
2277 // we only do this if we are undocked, since lighting should be stable when
2278 // stationary in a dock.
2279 int lcdValue = getAutoBrightnessValue(
2280 (mIsDocked ? value : mHighestLightSensorValue),
2281 mLcdBacklightValues);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002282 int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
Mike Lockwooddf024922009-10-29 21:29:15 -04002283 int keyboardValue;
2284 if (mKeyboardVisible) {
2285 keyboardValue = getAutoBrightnessValue(value, mKeyboardBacklightValues);
2286 } else {
2287 keyboardValue = 0;
2288 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002289 mLightSensorScreenBrightness = lcdValue;
2290 mLightSensorButtonBrightness = buttonValue;
2291 mLightSensorKeyboardBrightness = keyboardValue;
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002292
2293 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002294 Slog.d(TAG, "lcdValue " + lcdValue);
2295 Slog.d(TAG, "buttonValue " + buttonValue);
2296 Slog.d(TAG, "keyboardValue " + keyboardValue);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002297 }
2298
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002299 boolean startAnimation = false;
Mike Lockwood4984e732009-11-01 08:16:33 -05002300 if (mAutoBrightessEnabled && mScreenBrightnessOverride < 0) {
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002301 if (ANIMATE_SCREEN_LIGHTS) {
2302 if (mScreenBrightness.setTargetLocked(lcdValue,
2303 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_SCREEN_BRIGHTNESS,
2304 (int)mScreenBrightness.curValue)) {
2305 startAnimation = true;
2306 }
2307 } else {
Mike Lockwoodcc9a63d2009-11-10 07:50:28 -05002308 int brightnessMode = (mAutoBrightessEnabled
Mike Lockwood3a322132009-11-24 00:30:52 -05002309 ? LightsService.BRIGHTNESS_MODE_SENSOR
2310 : LightsService.BRIGHTNESS_MODE_USER);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002311 mLcdLight.setBrightness(lcdValue, brightnessMode);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002312 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002313 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002314 if (mButtonBrightnessOverride < 0) {
2315 if (ANIMATE_BUTTON_LIGHTS) {
2316 if (mButtonBrightness.setTargetLocked(buttonValue,
2317 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2318 (int)mButtonBrightness.curValue)) {
2319 startAnimation = true;
2320 }
2321 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002322 mButtonLight.setBrightness(buttonValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002323 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002324 }
Mike Lockwoodfb73f792009-11-20 11:31:18 -05002325 if (mButtonBrightnessOverride < 0 || !mKeyboardVisible) {
2326 if (ANIMATE_KEYBOARD_LIGHTS) {
2327 if (mKeyboardBrightness.setTargetLocked(keyboardValue,
2328 AUTOBRIGHTNESS_ANIM_STEPS, INITIAL_BUTTON_BRIGHTNESS,
2329 (int)mKeyboardBrightness.curValue)) {
2330 startAnimation = true;
2331 }
2332 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002333 mKeyboardLight.setBrightness(keyboardValue);
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002334 }
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002335 }
2336 if (startAnimation) {
2337 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002338 Slog.i(TAG, "lightSensorChangedLocked scheduling light animator");
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002339 }
2340 mHandler.removeCallbacks(mLightAnimator);
2341 mHandler.post(mLightAnimator);
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002342 }
2343 }
2344 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002345 }
2346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 /**
2348 * The user requested that we go to sleep (probably with the power button).
2349 * This overrides all wake locks that are held.
2350 */
2351 public void goToSleep(long time)
2352 {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002353 goToSleepWithReason(time, WindowManagerPolicy.OFF_BECAUSE_OF_USER);
2354 }
2355
2356 /**
2357 * The user requested that we go to sleep (probably with the power button).
2358 * This overrides all wake locks that are held.
2359 */
2360 public void goToSleepWithReason(long time, int reason)
2361 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2363 synchronized (mLocks) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08002364 goToSleepLocked(time, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 }
2366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 /**
Doug Zongker50a21f42009-11-19 12:49:53 -08002369 * Reboot the device immediately, passing 'reason' (may be null)
2370 * to the underlying __reboot system call. Should not return.
2371 */
2372 public void reboot(String reason)
2373 {
2374 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
San Mehat14e69af2010-01-06 14:58:18 -08002375
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002376 if (mHandler == null || !ActivityManagerNative.isSystemReady()) {
2377 throw new IllegalStateException("Too early to call reboot()");
2378 }
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002379
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002380 final String finalReason = reason;
2381 Runnable runnable = new Runnable() {
2382 public void run() {
2383 synchronized (this) {
2384 ShutdownThread.reboot(mContext, finalReason, false);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002385 }
2386
San Mehat1e512792010-01-07 10:40:29 -08002387 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002388 };
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002389 // ShutdownThread must run on a looper capable of displaying the UI.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002390 mHandler.post(runnable);
2391
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002392 // PowerManager.reboot() is documented not to return so just wait for the inevitable.
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002393 synchronized (runnable) {
Mike Lockwoodb62f9592010-03-12 07:55:23 -05002394 while (true) {
2395 try {
2396 runnable.wait();
2397 } catch (InterruptedException e) {
2398 }
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -08002399 }
Doug Zongker50a21f42009-11-19 12:49:53 -08002400 }
2401 }
2402
Dan Egnor60d87622009-12-16 16:32:58 -08002403 /**
2404 * Crash the runtime (causing a complete restart of the Android framework).
2405 * Requires REBOOT permission. Mostly for testing. Should not return.
2406 */
2407 public void crash(final String message)
2408 {
2409 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
2410 Thread t = new Thread("PowerManagerService.crash()") {
2411 public void run() { throw new RuntimeException(message); }
2412 };
2413 try {
2414 t.start();
2415 t.join();
2416 } catch (InterruptedException e) {
2417 Log.wtf(TAG, e);
2418 }
2419 }
2420
Mike Lockwood435eb642009-12-03 08:40:18 -05002421 private void goToSleepLocked(long time, int reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422
2423 if (mLastEventTime <= time) {
2424 mLastEventTime = time;
2425 // cancel all of the wake locks
2426 mWakeLockState = SCREEN_OFF;
2427 int N = mLocks.size();
2428 int numCleared = 0;
2429 for (int i=0; i<N; i++) {
2430 WakeLock wl = mLocks.get(i);
2431 if (isScreenLock(wl.flags)) {
2432 mLocks.get(i).activated = false;
2433 numCleared++;
2434 }
2435 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002436 EventLog.writeEvent(EventLogTags.POWER_SLEEP_REQUESTED, numCleared);
Joe Onorato128e7292009-03-24 18:41:31 -07002437 mStillNeedSleepNotification = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 mUserState = SCREEN_OFF;
Mike Lockwood435eb642009-12-03 08:40:18 -05002439 setPowerState(SCREEN_OFF, false, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 cancelTimerLocked();
2441 }
2442 }
2443
2444 public long timeSinceScreenOn() {
2445 synchronized (mLocks) {
2446 if ((mPowerState & SCREEN_ON_BIT) != 0) {
2447 return 0;
2448 }
2449 return SystemClock.elapsedRealtime() - mScreenOffTime;
2450 }
2451 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 public void setKeyboardVisibility(boolean visible) {
Mike Lockwooda625b382009-09-12 17:36:03 -07002454 synchronized (mLocks) {
2455 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002456 Slog.d(TAG, "setKeyboardVisibility: " + visible);
Mike Lockwooda625b382009-09-12 17:36:03 -07002457 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002458 if (mKeyboardVisible != visible) {
2459 mKeyboardVisible = visible;
2460 // don't signal user activity if the screen is off; other code
2461 // will take care of turning on due to a true change to the lid
2462 // switch and synchronized with the lock screen.
2463 if ((mPowerState & SCREEN_ON_BIT) != 0) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002464 if (mUseSoftwareAutoBrightness) {
Mike Lockwooddf024922009-10-29 21:29:15 -04002465 // force recompute of backlight values
2466 if (mLightSensorValue >= 0) {
2467 int value = (int)mLightSensorValue;
2468 mLightSensorValue = -1;
2469 lightSensorChangedLocked(value);
2470 }
2471 }
Mike Lockwood3c9435a2009-10-22 15:45:37 -04002472 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2473 }
Mike Lockwooda625b382009-09-12 17:36:03 -07002474 }
2475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
2477
2478 /**
2479 * When the keyguard is up, it manages the power state, and userActivity doesn't do anything.
Mike Lockwood50c548d2009-11-09 16:02:06 -05002480 * When disabling user activity we also reset user power state so the keyguard can reset its
2481 * short screen timeout when keyguard is unhidden.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 */
2483 public void enableUserActivity(boolean enabled) {
Mike Lockwood50c548d2009-11-09 16:02:06 -05002484 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002485 Slog.d(TAG, "enableUserActivity " + enabled);
Mike Lockwood50c548d2009-11-09 16:02:06 -05002486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 synchronized (mLocks) {
2488 mUserActivityAllowed = enabled;
Mike Lockwood50c548d2009-11-09 16:02:06 -05002489 if (!enabled) {
2490 // cancel timeout and clear mUserState so the keyguard can set a short timeout
2491 setTimeoutLocked(SystemClock.uptimeMillis(), 0);
2492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 }
2494 }
2495
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002496 private void setScreenBrightnessMode(int mode) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002497 boolean enabled = (mode == SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
Mike Lockwoodf90ffcc2009-11-03 11:41:27 -05002498 if (mUseSoftwareAutoBrightness && mAutoBrightessEnabled != enabled) {
Mike Lockwood2d155d22009-10-27 09:32:30 -04002499 mAutoBrightessEnabled = enabled;
Charles Mendis322591c2009-10-29 11:06:59 -07002500 if (isScreenOn()) {
Mike Lockwood4984e732009-11-01 08:16:33 -05002501 // force recompute of backlight values
2502 if (mLightSensorValue >= 0) {
2503 int value = (int)mLightSensorValue;
2504 mLightSensorValue = -1;
2505 lightSensorChangedLocked(value);
2506 }
Mike Lockwood2d155d22009-10-27 09:32:30 -04002507 }
Mike Lockwooddc3494e2009-10-14 21:17:09 -07002508 }
2509 }
2510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 /** Sets the screen off timeouts:
2512 * mKeylightDelay
2513 * mDimDelay
2514 * mScreenOffDelay
2515 * */
2516 private void setScreenOffTimeoutsLocked() {
2517 if ((mPokey & POKE_LOCK_SHORT_TIMEOUT) != 0) {
Doug Zongker43866e02010-01-07 12:09:54 -08002518 mKeylightDelay = mShortKeylightDelay; // Configurable via secure settings
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 mDimDelay = -1;
2520 mScreenOffDelay = 0;
2521 } else if ((mPokey & POKE_LOCK_MEDIUM_TIMEOUT) != 0) {
2522 mKeylightDelay = MEDIUM_KEYLIGHT_DELAY;
2523 mDimDelay = -1;
2524 mScreenOffDelay = 0;
2525 } else {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08002526 int totalDelay = mScreenOffTimeoutSetting;
2527 if (totalDelay > mMaximumScreenOffTimeout) {
2528 totalDelay = mMaximumScreenOffTimeout;
2529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 mKeylightDelay = LONG_KEYLIGHT_DELAY;
2531 if (totalDelay < 0) {
2532 mScreenOffDelay = Integer.MAX_VALUE;
2533 } else if (mKeylightDelay < totalDelay) {
2534 // subtract the time that the keylight delay. This will give us the
2535 // remainder of the time that we need to sleep to get the accurate
2536 // screen off timeout.
2537 mScreenOffDelay = totalDelay - mKeylightDelay;
2538 } else {
2539 mScreenOffDelay = 0;
2540 }
2541 if (mDimScreen && totalDelay >= (LONG_KEYLIGHT_DELAY + LONG_DIM_TIME)) {
2542 mDimDelay = mScreenOffDelay - LONG_DIM_TIME;
2543 mScreenOffDelay = LONG_DIM_TIME;
2544 } else {
2545 mDimDelay = -1;
2546 }
2547 }
2548 if (mSpew) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002549 Slog.d(TAG, "setScreenOffTimeouts mKeylightDelay=" + mKeylightDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 + " mDimDelay=" + mDimDelay + " mScreenOffDelay=" + mScreenOffDelay
2551 + " mDimScreen=" + mDimScreen);
2552 }
2553 }
2554
2555 /**
Doug Zongker43866e02010-01-07 12:09:54 -08002556 * Refreshes cached secure settings. Called once on startup, and
2557 * on subsequent changes to secure settings.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 */
Doug Zongker43866e02010-01-07 12:09:54 -08002559 private void updateSettingsValues() {
2560 mShortKeylightDelay = Settings.Secure.getInt(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 mContext.getContentResolver(),
Doug Zongker43866e02010-01-07 12:09:54 -08002562 Settings.Secure.SHORT_KEYLIGHT_DELAY_MS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 SHORT_KEYLIGHT_DELAY_DEFAULT);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002564 // Slog.i(TAG, "updateSettingsValues(): mShortKeylightDelay now " + mShortKeylightDelay);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 }
2566
2567 private class LockList extends ArrayList<WakeLock>
2568 {
2569 void addLock(WakeLock wl)
2570 {
2571 int index = getIndex(wl.binder);
2572 if (index < 0) {
2573 this.add(wl);
2574 }
2575 }
2576
2577 WakeLock removeLock(IBinder binder)
2578 {
2579 int index = getIndex(binder);
2580 if (index >= 0) {
2581 return this.remove(index);
2582 } else {
2583 return null;
2584 }
2585 }
2586
2587 int getIndex(IBinder binder)
2588 {
2589 int N = this.size();
2590 for (int i=0; i<N; i++) {
2591 if (this.get(i).binder == binder) {
2592 return i;
2593 }
2594 }
2595 return -1;
2596 }
2597
2598 int gatherState()
2599 {
2600 int result = 0;
2601 int N = this.size();
2602 for (int i=0; i<N; i++) {
2603 WakeLock wl = this.get(i);
2604 if (wl.activated) {
2605 if (isScreenLock(wl.flags)) {
2606 result |= wl.minState;
2607 }
2608 }
2609 }
2610 return result;
2611 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002612
Michael Chane96440f2009-05-06 10:27:36 -07002613 int reactivateScreenLocksLocked()
2614 {
2615 int result = 0;
2616 int N = this.size();
2617 for (int i=0; i<N; i++) {
2618 WakeLock wl = this.get(i);
2619 if (isScreenLock(wl.flags)) {
2620 wl.activated = true;
2621 result |= wl.minState;
2622 }
2623 }
2624 return result;
2625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 }
2627
2628 void setPolicy(WindowManagerPolicy p) {
2629 synchronized (mLocks) {
2630 mPolicy = p;
2631 mLocks.notifyAll();
2632 }
2633 }
2634
2635 WindowManagerPolicy getPolicyLocked() {
2636 while (mPolicy == null || !mDoneBooting) {
2637 try {
2638 mLocks.wait();
2639 } catch (InterruptedException e) {
2640 // Ignore
2641 }
2642 }
2643 return mPolicy;
2644 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 void systemReady() {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002647 mSensorManager = new SensorManager(mHandlerThread.getLooper());
2648 mProximitySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
2649 // don't bother with the light sensor if auto brightness is handled in hardware
Mike Lockwoodaa66ea82009-10-31 16:31:27 -04002650 if (mUseSoftwareAutoBrightness) {
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002651 mLightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Mike Lockwood4984e732009-11-01 08:16:33 -05002652 enableLightSensor(true);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002653 }
2654
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002655 // wait until sensors are enabled before turning on screen.
2656 // some devices will not activate the light sensor properly on boot
2657 // unless we do this.
2658 if (mUseSoftwareAutoBrightness) {
2659 // turn the screen on
2660 setPowerState(SCREEN_BRIGHT);
2661 } else {
2662 // turn everything on
2663 setPowerState(ALL_BRIGHT);
2664 }
2665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 synchronized (mLocks) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002667 Slog.d(TAG, "system ready!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 mDoneBooting = true;
Mike Lockwoodb42ab0f2010-03-04 08:02:44 -05002669
Dianne Hackborn617f8772009-03-31 15:04:46 -07002670 long identity = Binder.clearCallingIdentity();
2671 try {
2672 mBatteryStats.noteScreenBrightness(getPreferredBrightness());
2673 mBatteryStats.noteScreenOn();
2674 } catch (RemoteException e) {
2675 // Nothing interesting to do.
2676 } finally {
2677 Binder.restoreCallingIdentity(identity);
2678 }
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002679 }
2680 }
2681
2682 void bootCompleted() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002683 Slog.d(TAG, "bootCompleted");
Mike Lockwood2d7bb812009-11-15 18:12:22 -05002684 synchronized (mLocks) {
2685 mBootCompleted = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 userActivity(SystemClock.uptimeMillis(), false, BUTTON_EVENT, true);
2687 updateWakeLockLocked();
2688 mLocks.notifyAll();
2689 }
2690 }
2691
2692 public void monitor() {
2693 synchronized (mLocks) { }
2694 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002695
2696 public int getSupportedWakeLockFlags() {
2697 int result = PowerManager.PARTIAL_WAKE_LOCK
2698 | PowerManager.FULL_WAKE_LOCK
2699 | PowerManager.SCREEN_DIM_WAKE_LOCK;
2700
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002701 if (mProximitySensor != null) {
2702 result |= PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK;
2703 }
2704
2705 return result;
2706 }
2707
Mike Lockwood237a2992009-09-15 14:42:16 -04002708 public void setBacklightBrightness(int brightness) {
2709 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
2710 // Don't let applications turn the screen all the way off
2711 brightness = Math.max(brightness, Power.BRIGHTNESS_DIM);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002712 mLcdLight.setBrightness(brightness);
2713 mKeyboardLight.setBrightness(mKeyboardVisible ? brightness : 0);
2714 mButtonLight.setBrightness(brightness);
Mike Lockwood237a2992009-09-15 14:42:16 -04002715 long identity = Binder.clearCallingIdentity();
2716 try {
2717 mBatteryStats.noteScreenBrightness(brightness);
2718 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002719 Slog.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
Mike Lockwood237a2992009-09-15 14:42:16 -04002720 } finally {
2721 Binder.restoreCallingIdentity(identity);
2722 }
2723
2724 // update our animation state
2725 if (ANIMATE_SCREEN_LIGHTS) {
2726 mScreenBrightness.curValue = brightness;
2727 mScreenBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002728 mScreenBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002729 }
2730 if (ANIMATE_KEYBOARD_LIGHTS) {
2731 mKeyboardBrightness.curValue = brightness;
2732 mKeyboardBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002733 mKeyboardBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002734 }
2735 if (ANIMATE_BUTTON_LIGHTS) {
2736 mButtonBrightness.curValue = brightness;
2737 mButtonBrightness.animating = false;
Mike Lockwooddd9668e2009-10-27 15:47:02 -04002738 mButtonBrightness.targetValue = -1;
Mike Lockwood237a2992009-09-15 14:42:16 -04002739 }
2740 }
2741
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002742 public void setAttentionLight(boolean on, int color) {
2743 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER, null);
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002744 mAttentionLight.setFlashing(color, LightsService.LIGHT_FLASH_HARDWARE, (on ? 3 : 0), 0);
Mike Lockwoodb11832d2009-11-25 15:25:55 -05002745 }
2746
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002747 private void enableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002748 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002749 Slog.d(TAG, "enableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07002750 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002751 if (!mProximitySensorEnabled) {
2752 // clear calling identity so sensor manager battery stats are accurate
2753 long identity = Binder.clearCallingIdentity();
2754 try {
2755 mSensorManager.registerListener(mProximityListener, mProximitySensor,
2756 SensorManager.SENSOR_DELAY_NORMAL);
2757 mProximitySensorEnabled = true;
2758 } finally {
2759 Binder.restoreCallingIdentity(identity);
2760 }
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002761 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002762 }
2763
2764 private void disableProximityLockLocked() {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002765 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002766 Slog.d(TAG, "disableProximityLockLocked");
Mike Lockwood36fc3022009-08-25 16:49:06 -07002767 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002768 if (mProximitySensorEnabled) {
2769 // clear calling identity so sensor manager battery stats are accurate
2770 long identity = Binder.clearCallingIdentity();
2771 try {
2772 mSensorManager.unregisterListener(mProximityListener);
2773 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002774 if (mProximityPartialLock.isHeld()) {
2775 mProximityPartialLock.release();
2776 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002777 mProximitySensorEnabled = false;
2778 } finally {
2779 Binder.restoreCallingIdentity(identity);
2780 }
2781 if (mProximitySensorActive) {
2782 mProximitySensorActive = false;
2783 forceUserActivityLocked();
2784 }
Mike Lockwood200b30b2009-09-20 00:23:59 -04002785 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002786 }
2787
Mike Lockwood20f87d72009-11-05 16:08:51 -05002788 private void proximityChangedLocked(boolean active) {
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002789 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002790 Slog.d(TAG, "proximityChangedLocked, active: " + active);
Mike Lockwood20f87d72009-11-05 16:08:51 -05002791 }
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002792 if (!mProximitySensorEnabled) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002793 Slog.d(TAG, "Ignoring proximity change after sensor is disabled");
Mike Lockwood0d72f7e2009-11-05 20:53:00 -05002794 return;
2795 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002796 if (active) {
Mike Lockwood435eb642009-12-03 08:40:18 -05002797 goToSleepLocked(SystemClock.uptimeMillis(),
2798 WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR);
Mike Lockwood20f87d72009-11-05 16:08:51 -05002799 mProximitySensorActive = true;
2800 } else {
2801 // proximity sensor negative events trigger as user activity.
2802 // temporarily set mUserActivityAllowed to true so this will work
2803 // even when the keyguard is on.
2804 mProximitySensorActive = false;
2805 forceUserActivityLocked();
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002806
2807 if (mProximityWakeLockCount == 0) {
2808 // disable sensor if we have no listeners left after proximity negative
2809 disableProximityLockLocked();
2810 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002811 }
2812 }
2813
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002814 private void enableLightSensor(boolean enable) {
2815 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002816 Slog.d(TAG, "enableLightSensor " + enable);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002817 }
2818 if (mSensorManager != null && mLightSensorEnabled != enable) {
2819 mLightSensorEnabled = enable;
Mike Lockwood809ad0f2009-10-26 22:10:33 -04002820 // clear calling identity so sensor manager battery stats are accurate
2821 long identity = Binder.clearCallingIdentity();
2822 try {
2823 if (enable) {
2824 mSensorManager.registerListener(mLightListener, mLightSensor,
2825 SensorManager.SENSOR_DELAY_NORMAL);
2826 } else {
2827 mSensorManager.unregisterListener(mLightListener);
2828 mHandler.removeCallbacks(mAutoBrightnessTask);
2829 }
2830 } finally {
2831 Binder.restoreCallingIdentity(identity);
Mike Lockwood06952d92009-08-13 16:05:38 -04002832 }
Mike Lockwoodbc706a02009-07-27 13:50:57 -07002833 }
2834 }
2835
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002836 SensorEventListener mProximityListener = new SensorEventListener() {
2837 public void onSensorChanged(SensorEvent event) {
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002838 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002839 synchronized (mLocks) {
2840 float distance = event.values[0];
Mike Lockwood20f87d72009-11-05 16:08:51 -05002841 long timeSinceLastEvent = milliseconds - mLastProximityEventTime;
2842 mLastProximityEventTime = milliseconds;
2843 mHandler.removeCallbacks(mProximityTask);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002844 boolean proximityTaskQueued = false;
Mike Lockwood20f87d72009-11-05 16:08:51 -05002845
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002846 // compare against getMaximumRange to support sensors that only return 0 or 1
Mike Lockwood20f87d72009-11-05 16:08:51 -05002847 boolean active = (distance >= 0.0 && distance < PROXIMITY_THRESHOLD &&
2848 distance < mProximitySensor.getMaximumRange());
2849
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002850 if (mDebugProximitySensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002851 Slog.d(TAG, "mProximityListener.onSensorChanged active: " + active);
Mike Lockwoodee2b0942009-11-09 14:09:02 -05002852 }
Mike Lockwood20f87d72009-11-05 16:08:51 -05002853 if (timeSinceLastEvent < PROXIMITY_SENSOR_DELAY) {
2854 // enforce delaying atleast PROXIMITY_SENSOR_DELAY before processing
2855 mProximityPendingValue = (active ? 1 : 0);
2856 mHandler.postDelayed(mProximityTask, PROXIMITY_SENSOR_DELAY - timeSinceLastEvent);
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002857 proximityTaskQueued = true;
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002858 } else {
Mike Lockwood20f87d72009-11-05 16:08:51 -05002859 // process the value immediately
2860 mProximityPendingValue = -1;
2861 proximityChangedLocked(active);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002862 }
Mike Lockwood0e5bb7f2009-11-14 06:36:31 -05002863
2864 // update mProximityPartialLock state
2865 boolean held = mProximityPartialLock.isHeld();
2866 if (!held && proximityTaskQueued) {
2867 // hold wakelock until mProximityTask runs
2868 mProximityPartialLock.acquire();
2869 } else if (held && !proximityTaskQueued) {
2870 mProximityPartialLock.release();
2871 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002872 }
2873 }
2874
2875 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2876 // ignore
2877 }
2878 };
2879
2880 SensorEventListener mLightListener = new SensorEventListener() {
2881 public void onSensorChanged(SensorEvent event) {
2882 synchronized (mLocks) {
Mike Lockwood497087e32009-11-08 18:33:03 -05002883 // ignore light sensor while screen is turning off
2884 if (isScreenTurningOffLocked()) {
2885 return;
2886 }
2887
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002888 int value = (int)event.values[0];
Mike Lockwoodba8eb1e2009-11-08 19:31:18 -05002889 long milliseconds = SystemClock.elapsedRealtime();
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002890 if (mDebugLightSensor) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002891 Slog.d(TAG, "onSensorChanged: light value: " + value);
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002892 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002893 mHandler.removeCallbacks(mAutoBrightnessTask);
2894 if (mLightSensorValue != value) {
Mike Lockwood20ee6f22009-11-07 20:33:47 -05002895 if (mLightSensorValue == -1 ||
2896 milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
2897 // process the value immediately if screen has just turned on
Mike Lockwood6c97fca2009-10-20 08:10:00 -04002898 lightSensorChangedLocked(value);
2899 } else {
2900 // delay processing to debounce the sensor
2901 mLightSensorPendingValue = value;
2902 mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
2903 }
Mike Lockwoodd7786b42009-10-15 17:09:16 -07002904 } else {
2905 mLightSensorPendingValue = -1;
2906 }
Mike Lockwood8738e0c2009-10-04 08:44:47 -04002907 }
2908 }
2909
2910 public void onAccuracyChanged(Sensor sensor, int accuracy) {
2911 // ignore
2912 }
2913 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914}